summaryrefslogtreecommitdiffstats
path: root/media/libmediaplayerservice/nuplayer
diff options
context:
space:
mode:
Diffstat (limited to 'media/libmediaplayerservice/nuplayer')
-rw-r--r--media/libmediaplayerservice/nuplayer/GenericSource.cpp32
-rw-r--r--media/libmediaplayerservice/nuplayer/GenericSource.h8
-rw-r--r--media/libmediaplayerservice/nuplayer/HTTPLiveSource.cpp244
-rw-r--r--media/libmediaplayerservice/nuplayer/HTTPLiveSource.h21
-rw-r--r--media/libmediaplayerservice/nuplayer/NuPlayer.cpp821
-rw-r--r--media/libmediaplayerservice/nuplayer/NuPlayer.h55
-rw-r--r--media/libmediaplayerservice/nuplayer/NuPlayerDriver.cpp334
-rw-r--r--media/libmediaplayerservice/nuplayer/NuPlayerDriver.h37
-rw-r--r--media/libmediaplayerservice/nuplayer/NuPlayerRenderer.cpp80
-rw-r--r--media/libmediaplayerservice/nuplayer/NuPlayerRenderer.h17
-rw-r--r--media/libmediaplayerservice/nuplayer/NuPlayerSource.h53
-rw-r--r--media/libmediaplayerservice/nuplayer/RTSPSource.cpp224
-rw-r--r--media/libmediaplayerservice/nuplayer/RTSPSource.h19
-rw-r--r--media/libmediaplayerservice/nuplayer/StreamingSource.cpp33
-rw-r--r--media/libmediaplayerservice/nuplayer/StreamingSource.h7
-rw-r--r--media/libmediaplayerservice/nuplayer/mp4/MP4Source.cpp14
-rw-r--r--media/libmediaplayerservice/nuplayer/mp4/MP4Source.h3
17 files changed, 1659 insertions, 343 deletions
diff --git a/media/libmediaplayerservice/nuplayer/GenericSource.cpp b/media/libmediaplayerservice/nuplayer/GenericSource.cpp
index f0c3240..b04e7a6 100644
--- a/media/libmediaplayerservice/nuplayer/GenericSource.cpp
+++ b/media/libmediaplayerservice/nuplayer/GenericSource.cpp
@@ -32,11 +32,13 @@
namespace android {
NuPlayer::GenericSource::GenericSource(
+ const sp<AMessage> &notify,
const char *url,
const KeyedVector<String8, String8> *headers,
bool uidValid,
uid_t uid)
- : mDurationUs(0ll),
+ : Source(notify),
+ mDurationUs(0ll),
mAudioIsVorbis(false) {
DataSource::RegisterDefaultSniffers();
@@ -48,8 +50,10 @@ NuPlayer::GenericSource::GenericSource(
}
NuPlayer::GenericSource::GenericSource(
+ const sp<AMessage> &notify,
int fd, int64_t offset, int64_t length)
- : mDurationUs(0ll),
+ : Source(notify),
+ mDurationUs(0ll),
mAudioIsVorbis(false) {
DataSource::RegisterDefaultSniffers();
@@ -102,6 +106,26 @@ void NuPlayer::GenericSource::initFromDataSource(
NuPlayer::GenericSource::~GenericSource() {
}
+void NuPlayer::GenericSource::prepareAsync() {
+ if (mVideoTrack.mSource != NULL) {
+ sp<MetaData> meta = mVideoTrack.mSource->getFormat();
+
+ int32_t width, height;
+ CHECK(meta->findInt32(kKeyWidth, &width));
+ CHECK(meta->findInt32(kKeyHeight, &height));
+
+ notifyVideoSizeChanged(width, height);
+ }
+
+ notifyFlagsChanged(
+ FLAG_CAN_PAUSE
+ | FLAG_CAN_SEEK_BACKWARD
+ | FLAG_CAN_SEEK_FORWARD
+ | FLAG_CAN_SEEK);
+
+ notifyPrepared();
+}
+
void NuPlayer::GenericSource::start() {
ALOGI("start");
@@ -258,8 +282,4 @@ void NuPlayer::GenericSource::readBuffer(
}
}
-bool NuPlayer::GenericSource::isSeekable() {
- return true;
-}
-
} // namespace android
diff --git a/media/libmediaplayerservice/nuplayer/GenericSource.h b/media/libmediaplayerservice/nuplayer/GenericSource.h
index e50b855..2da680c 100644
--- a/media/libmediaplayerservice/nuplayer/GenericSource.h
+++ b/media/libmediaplayerservice/nuplayer/GenericSource.h
@@ -32,12 +32,17 @@ struct MediaSource;
struct NuPlayer::GenericSource : public NuPlayer::Source {
GenericSource(
+ const sp<AMessage> &notify,
const char *url,
const KeyedVector<String8, String8> *headers,
bool uidValid = false,
uid_t uid = 0);
- GenericSource(int fd, int64_t offset, int64_t length);
+ GenericSource(
+ const sp<AMessage> &notify,
+ int fd, int64_t offset, int64_t length);
+
+ virtual void prepareAsync();
virtual void start();
@@ -47,7 +52,6 @@ struct NuPlayer::GenericSource : public NuPlayer::Source {
virtual status_t getDuration(int64_t *durationUs);
virtual status_t seekTo(int64_t seekTimeUs);
- virtual bool isSeekable();
protected:
virtual ~GenericSource();
diff --git a/media/libmediaplayerservice/nuplayer/HTTPLiveSource.cpp b/media/libmediaplayerservice/nuplayer/HTTPLiveSource.cpp
index 1e98f35..f1782cc 100644
--- a/media/libmediaplayerservice/nuplayer/HTTPLiveSource.cpp
+++ b/media/libmediaplayerservice/nuplayer/HTTPLiveSource.cpp
@@ -20,7 +20,6 @@
#include "HTTPLiveSource.h"
-#include "ATSParser.h"
#include "AnotherPacketSource.h"
#include "LiveDataSource.h"
#include "LiveSession.h"
@@ -34,15 +33,18 @@
namespace android {
NuPlayer::HTTPLiveSource::HTTPLiveSource(
+ const sp<AMessage> &notify,
const char *url,
const KeyedVector<String8, String8> *headers,
bool uidValid, uid_t uid)
- : mURL(url),
+ : Source(notify),
+ mURL(url),
mUIDValid(uidValid),
mUID(uid),
mFlags(0),
mFinalResult(OK),
- mOffset(0) {
+ mOffset(0),
+ mFetchSubtitleDataGeneration(0) {
if (headers) {
mExtraHeaders = *headers;
@@ -60,129 +62,211 @@ NuPlayer::HTTPLiveSource::HTTPLiveSource(
NuPlayer::HTTPLiveSource::~HTTPLiveSource() {
if (mLiveSession != NULL) {
mLiveSession->disconnect();
+ mLiveSession.clear();
+
mLiveLooper->stop();
+ mLiveLooper.clear();
}
}
-void NuPlayer::HTTPLiveSource::start() {
+void NuPlayer::HTTPLiveSource::prepareAsync() {
mLiveLooper = new ALooper;
mLiveLooper->setName("http live");
mLiveLooper->start();
+ sp<AMessage> notify = new AMessage(kWhatSessionNotify, id());
+
mLiveSession = new LiveSession(
+ notify,
(mFlags & kFlagIncognito) ? LiveSession::kFlagIncognito : 0,
- mUIDValid, mUID);
+ mUIDValid,
+ mUID);
mLiveLooper->registerHandler(mLiveSession);
- mLiveSession->connect(
+ mLiveSession->connectAsync(
mURL.c_str(), mExtraHeaders.isEmpty() ? NULL : &mExtraHeaders);
-
- mTSParser = new ATSParser;
}
-sp<MetaData> NuPlayer::HTTPLiveSource::getFormatMeta(bool audio) {
- ATSParser::SourceType type =
- audio ? ATSParser::AUDIO : ATSParser::VIDEO;
+void NuPlayer::HTTPLiveSource::start() {
+}
- sp<AnotherPacketSource> source =
- static_cast<AnotherPacketSource *>(mTSParser->getSource(type).get());
+sp<AMessage> NuPlayer::HTTPLiveSource::getFormat(bool audio) {
+ sp<AMessage> format;
+ status_t err = mLiveSession->getStreamFormat(
+ audio ? LiveSession::STREAMTYPE_AUDIO
+ : LiveSession::STREAMTYPE_VIDEO,
+ &format);
- if (source == NULL) {
+ if (err != OK) {
return NULL;
}
- return source->getFormat();
+ return format;
}
status_t NuPlayer::HTTPLiveSource::feedMoreTSData() {
- if (mFinalResult != OK) {
- return mFinalResult;
+ return OK;
+}
+
+status_t NuPlayer::HTTPLiveSource::dequeueAccessUnit(
+ bool audio, sp<ABuffer> *accessUnit) {
+ return mLiveSession->dequeueAccessUnit(
+ audio ? LiveSession::STREAMTYPE_AUDIO
+ : LiveSession::STREAMTYPE_VIDEO,
+ accessUnit);
+}
+
+status_t NuPlayer::HTTPLiveSource::getDuration(int64_t *durationUs) {
+ return mLiveSession->getDuration(durationUs);
+}
+
+status_t NuPlayer::HTTPLiveSource::getTrackInfo(Parcel *reply) const {
+ return mLiveSession->getTrackInfo(reply);
+}
+
+status_t NuPlayer::HTTPLiveSource::selectTrack(size_t trackIndex, bool select) {
+ status_t err = mLiveSession->selectTrack(trackIndex, select);
+
+ if (err == OK) {
+ mFetchSubtitleDataGeneration++;
+ if (select) {
+ sp<AMessage> msg = new AMessage(kWhatFetchSubtitleData, id());
+ msg->setInt32("generation", mFetchSubtitleDataGeneration);
+ msg->post();
+ }
}
- sp<LiveDataSource> source =
- static_cast<LiveDataSource *>(mLiveSession->getDataSource().get());
+ // LiveSession::selectTrack returns BAD_VALUE when selecting the currently
+ // selected track, or unselecting a non-selected track. In this case it's an
+ // no-op so we return OK.
+ return (err == OK || err == BAD_VALUE) ? OK : err;
+}
- for (int32_t i = 0; i < 50; ++i) {
- char buffer[188];
- ssize_t n = source->readAtNonBlocking(mOffset, buffer, sizeof(buffer));
+status_t NuPlayer::HTTPLiveSource::seekTo(int64_t seekTimeUs) {
+ return mLiveSession->seekTo(seekTimeUs);
+}
- if (n == -EWOULDBLOCK) {
+void NuPlayer::HTTPLiveSource::onMessageReceived(const sp<AMessage> &msg) {
+ switch (msg->what()) {
+ case kWhatSessionNotify:
+ {
+ onSessionNotify(msg);
break;
- } else if (n < 0) {
- if (n != ERROR_END_OF_STREAM) {
- ALOGI("input data EOS reached, error %ld", n);
- } else {
- ALOGI("input data EOS reached.");
+ }
+
+ case kWhatFetchSubtitleData:
+ {
+ int32_t generation;
+ CHECK(msg->findInt32("generation", &generation));
+
+ if (generation != mFetchSubtitleDataGeneration) {
+ // stale
+ break;
}
- mTSParser->signalEOS(n);
- mFinalResult = n;
- break;
- } else {
- if (buffer[0] == 0x00) {
- // XXX legacy
- sp<AMessage> extra;
- mTSParser->signalDiscontinuity(
- buffer[1] == 0x00
- ? ATSParser::DISCONTINUITY_SEEK
- : ATSParser::DISCONTINUITY_FORMATCHANGE,
- extra);
+
+ sp<ABuffer> buffer;
+ if (mLiveSession->dequeueAccessUnit(
+ LiveSession::STREAMTYPE_SUBTITLES, &buffer) == OK) {
+ sp<AMessage> notify = dupNotify();
+ notify->setInt32("what", kWhatSubtitleData);
+ notify->setBuffer("buffer", buffer);
+ notify->post();
+
+ int64_t timeUs, baseUs, durationUs, delayUs;
+ CHECK(buffer->meta()->findInt64("baseUs", &baseUs));
+ CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
+ CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
+ delayUs = baseUs + timeUs - ALooper::GetNowUs();
+
+ msg->post(delayUs > 0ll ? delayUs : 0ll);
} else {
- status_t err = mTSParser->feedTSPacket(buffer, sizeof(buffer));
-
- if (err != OK) {
- ALOGE("TS Parser returned error %d", err);
- mTSParser->signalEOS(err);
- mFinalResult = err;
- break;
- }
+ // try again in 1 second
+ msg->post(1000000ll);
}
- mOffset += n;
+ break;
}
- }
- return OK;
+ default:
+ Source::onMessageReceived(msg);
+ break;
+ }
}
-status_t NuPlayer::HTTPLiveSource::dequeueAccessUnit(
- bool audio, sp<ABuffer> *accessUnit) {
- ATSParser::SourceType type =
- audio ? ATSParser::AUDIO : ATSParser::VIDEO;
+void NuPlayer::HTTPLiveSource::onSessionNotify(const sp<AMessage> &msg) {
+ int32_t what;
+ CHECK(msg->findInt32("what", &what));
+
+ switch (what) {
+ case LiveSession::kWhatPrepared:
+ {
+ // notify the current size here if we have it, otherwise report an initial size of (0,0)
+ sp<AMessage> format = getFormat(false /* audio */);
+ int32_t width;
+ int32_t height;
+ if (format != NULL &&
+ format->findInt32("width", &width) && format->findInt32("height", &height)) {
+ notifyVideoSizeChanged(width, height);
+ } else {
+ notifyVideoSizeChanged(0, 0);
+ }
- sp<AnotherPacketSource> source =
- static_cast<AnotherPacketSource *>(mTSParser->getSource(type).get());
+ uint32_t flags = FLAG_CAN_PAUSE;
+ if (mLiveSession->isSeekable()) {
+ flags |= FLAG_CAN_SEEK;
+ flags |= FLAG_CAN_SEEK_BACKWARD;
+ flags |= FLAG_CAN_SEEK_FORWARD;
+ }
- if (source == NULL) {
- return -EWOULDBLOCK;
- }
+ if (mLiveSession->hasDynamicDuration()) {
+ flags |= FLAG_DYNAMIC_DURATION;
+ }
- status_t finalResult;
- if (!source->hasBufferAvailable(&finalResult)) {
- return finalResult == OK ? -EWOULDBLOCK : finalResult;
- }
+ notifyFlagsChanged(flags);
- return source->dequeueAccessUnit(accessUnit);
-}
+ notifyPrepared();
+ break;
+ }
-status_t NuPlayer::HTTPLiveSource::getDuration(int64_t *durationUs) {
- return mLiveSession->getDuration(durationUs);
-}
+ case LiveSession::kWhatPreparationFailed:
+ {
+ status_t err;
+ CHECK(msg->findInt32("err", &err));
-status_t NuPlayer::HTTPLiveSource::seekTo(int64_t seekTimeUs) {
- // We need to make sure we're not seeking until we have seen the very first
- // PTS timestamp in the whole stream (from the beginning of the stream).
- while (!mTSParser->PTSTimeDeltaEstablished() && feedMoreTSData() == OK) {
- usleep(100000);
- }
+ notifyPrepared(err);
+ break;
+ }
- mLiveSession->seekTo(seekTimeUs);
+ case LiveSession::kWhatStreamsChanged:
+ {
+ uint32_t changedMask;
+ CHECK(msg->findInt32(
+ "changedMask", (int32_t *)&changedMask));
- return OK;
-}
+ bool audio = changedMask & LiveSession::STREAMTYPE_AUDIO;
+ bool video = changedMask & LiveSession::STREAMTYPE_VIDEO;
+
+ sp<AMessage> reply;
+ CHECK(msg->findMessage("reply", &reply));
-bool NuPlayer::HTTPLiveSource::isSeekable() {
- return mLiveSession->isSeekable();
+ sp<AMessage> notify = dupNotify();
+ notify->setInt32("what", kWhatQueueDecoderShutdown);
+ notify->setInt32("audio", audio);
+ notify->setInt32("video", video);
+ notify->setMessage("reply", reply);
+ notify->post();
+ break;
+ }
+
+ case LiveSession::kWhatError:
+ {
+ break;
+ }
+
+ default:
+ TRESPASS();
+ }
}
} // namespace android
diff --git a/media/libmediaplayerservice/nuplayer/HTTPLiveSource.h b/media/libmediaplayerservice/nuplayer/HTTPLiveSource.h
index 9950a9e..bcc3f8b 100644
--- a/media/libmediaplayerservice/nuplayer/HTTPLiveSource.h
+++ b/media/libmediaplayerservice/nuplayer/HTTPLiveSource.h
@@ -23,30 +23,32 @@
namespace android {
-struct ATSParser;
struct LiveSession;
struct NuPlayer::HTTPLiveSource : public NuPlayer::Source {
HTTPLiveSource(
+ const sp<AMessage> &notify,
const char *url,
const KeyedVector<String8, String8> *headers,
bool uidValid = false,
uid_t uid = 0);
+ virtual void prepareAsync();
virtual void start();
- virtual status_t feedMoreTSData();
-
virtual status_t dequeueAccessUnit(bool audio, sp<ABuffer> *accessUnit);
+ virtual sp<AMessage> getFormat(bool audio);
+ virtual status_t feedMoreTSData();
virtual status_t getDuration(int64_t *durationUs);
+ virtual status_t getTrackInfo(Parcel *reply) const;
+ virtual status_t selectTrack(size_t trackIndex, bool select);
virtual status_t seekTo(int64_t seekTimeUs);
- virtual bool isSeekable();
protected:
virtual ~HTTPLiveSource();
- virtual sp<MetaData> getFormatMeta(bool audio);
+ virtual void onMessageReceived(const sp<AMessage> &msg);
private:
enum Flags {
@@ -54,6 +56,11 @@ private:
kFlagIncognito = 1,
};
+ enum {
+ kWhatSessionNotify,
+ kWhatFetchSubtitleData,
+ };
+
AString mURL;
KeyedVector<String8, String8> mExtraHeaders;
bool mUIDValid;
@@ -63,7 +70,9 @@ private:
off64_t mOffset;
sp<ALooper> mLiveLooper;
sp<LiveSession> mLiveSession;
- sp<ATSParser> mTSParser;
+ int32_t mFetchSubtitleDataGeneration;
+
+ void onSessionNotify(const sp<AMessage> &msg);
DISALLOW_EVIL_CONSTRUCTORS(HTTPLiveSource);
};
diff --git a/media/libmediaplayerservice/nuplayer/NuPlayer.cpp b/media/libmediaplayerservice/nuplayer/NuPlayer.cpp
index 1ddf775..3669a5b 100644
--- a/media/libmediaplayerservice/nuplayer/NuPlayer.cpp
+++ b/media/libmediaplayerservice/nuplayer/NuPlayer.cpp
@@ -32,6 +32,8 @@
#include "ATSParser.h"
+#include "SoftwareRenderer.h"
+
#include <cutils/properties.h> // for property_get
#include <media/stagefright/foundation/hexdump.h>
#include <media/stagefright/foundation/ABuffer.h>
@@ -41,7 +43,7 @@
#include <media/stagefright/MediaDefs.h>
#include <media/stagefright/MediaErrors.h>
#include <media/stagefright/MetaData.h>
-#include <gui/ISurfaceTexture.h>
+#include <gui/IGraphicBufferProducer.h>
#include "avc_utils.h"
@@ -50,26 +52,118 @@
namespace android {
+struct NuPlayer::Action : public RefBase {
+ Action() {}
+
+ virtual void execute(NuPlayer *player) = 0;
+
+private:
+ DISALLOW_EVIL_CONSTRUCTORS(Action);
+};
+
+struct NuPlayer::SeekAction : public Action {
+ SeekAction(int64_t seekTimeUs)
+ : mSeekTimeUs(seekTimeUs) {
+ }
+
+ virtual void execute(NuPlayer *player) {
+ player->performSeek(mSeekTimeUs);
+ }
+
+private:
+ int64_t mSeekTimeUs;
+
+ DISALLOW_EVIL_CONSTRUCTORS(SeekAction);
+};
+
+struct NuPlayer::SetSurfaceAction : public Action {
+ SetSurfaceAction(const sp<NativeWindowWrapper> &wrapper)
+ : mWrapper(wrapper) {
+ }
+
+ virtual void execute(NuPlayer *player) {
+ player->performSetSurface(mWrapper);
+ }
+
+private:
+ sp<NativeWindowWrapper> mWrapper;
+
+ DISALLOW_EVIL_CONSTRUCTORS(SetSurfaceAction);
+};
+
+struct NuPlayer::ShutdownDecoderAction : public Action {
+ ShutdownDecoderAction(bool audio, bool video)
+ : mAudio(audio),
+ mVideo(video) {
+ }
+
+ virtual void execute(NuPlayer *player) {
+ player->performDecoderShutdown(mAudio, mVideo);
+ }
+
+private:
+ bool mAudio;
+ bool mVideo;
+
+ DISALLOW_EVIL_CONSTRUCTORS(ShutdownDecoderAction);
+};
+
+struct NuPlayer::PostMessageAction : public Action {
+ PostMessageAction(const sp<AMessage> &msg)
+ : mMessage(msg) {
+ }
+
+ virtual void execute(NuPlayer *) {
+ mMessage->post();
+ }
+
+private:
+ sp<AMessage> mMessage;
+
+ DISALLOW_EVIL_CONSTRUCTORS(PostMessageAction);
+};
+
+// Use this if there's no state necessary to save in order to execute
+// the action.
+struct NuPlayer::SimpleAction : public Action {
+ typedef void (NuPlayer::*ActionFunc)();
+
+ SimpleAction(ActionFunc func)
+ : mFunc(func) {
+ }
+
+ virtual void execute(NuPlayer *player) {
+ (player->*mFunc)();
+ }
+
+private:
+ ActionFunc mFunc;
+
+ DISALLOW_EVIL_CONSTRUCTORS(SimpleAction);
+};
+
////////////////////////////////////////////////////////////////////////////////
NuPlayer::NuPlayer()
: mUIDValid(false),
+ mSourceFlags(0),
mVideoIsAVC(false),
+ mNeedsSwRenderer(false),
mAudioEOS(false),
mVideoEOS(false),
mScanSourcesPending(false),
mScanSourcesGeneration(0),
+ mPollDurationGeneration(0),
mTimeDiscontinuityPending(false),
mFlushingAudio(NONE),
mFlushingVideo(NONE),
- mResetInProgress(false),
- mResetPostponed(false),
mSkipRenderingAudioUntilMediaTimeUs(-1ll),
mSkipRenderingVideoUntilMediaTimeUs(-1ll),
mVideoLateByUs(0ll),
mNumFramesTotal(0ll),
mNumFramesDropped(0ll),
- mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW) {
+ mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW),
+ mStarted(false) {
}
NuPlayer::~NuPlayer() {
@@ -84,15 +178,17 @@ void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) {
mDriver = driver;
}
-void NuPlayer::setDataSource(const sp<IStreamSource> &source) {
+void NuPlayer::setDataSourceAsync(const sp<IStreamSource> &source) {
sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
+ sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
+
char prop[PROPERTY_VALUE_MAX];
if (property_get("media.stagefright.use-mp4source", prop, NULL)
&& (!strcmp(prop, "1") || !strcasecmp(prop, "true"))) {
- msg->setObject("source", new MP4Source(source));
+ msg->setObject("source", new MP4Source(notify, source));
} else {
- msg->setObject("source", new StreamingSource(source));
+ msg->setObject("source", new StreamingSource(notify, source));
}
msg->post();
@@ -100,7 +196,8 @@ void NuPlayer::setDataSource(const sp<IStreamSource> &source) {
static bool IsHTTPLiveURL(const char *url) {
if (!strncasecmp("http://", url, 7)
- || !strncasecmp("https://", url, 8)) {
+ || !strncasecmp("https://", url, 8)
+ || !strncasecmp("file://", url, 7)) {
size_t len = strlen(url);
if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
return true;
@@ -114,36 +211,58 @@ static bool IsHTTPLiveURL(const char *url) {
return false;
}
-void NuPlayer::setDataSource(
+void NuPlayer::setDataSourceAsync(
const char *url, const KeyedVector<String8, String8> *headers) {
sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
+ size_t len = strlen(url);
+
+ sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
sp<Source> source;
if (IsHTTPLiveURL(url)) {
- source = new HTTPLiveSource(url, headers, mUIDValid, mUID);
+ source = new HTTPLiveSource(notify, url, headers, mUIDValid, mUID);
} else if (!strncasecmp(url, "rtsp://", 7)) {
- source = new RTSPSource(url, headers, mUIDValid, mUID);
+ source = new RTSPSource(notify, url, headers, mUIDValid, mUID);
+ } else if ((!strncasecmp(url, "http://", 7)
+ || !strncasecmp(url, "https://", 8))
+ && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4]))
+ || strstr(url, ".sdp?"))) {
+ source = new RTSPSource(notify, url, headers, mUIDValid, mUID, true);
} else {
- source = new GenericSource(url, headers, mUIDValid, mUID);
+ source = new GenericSource(notify, url, headers, mUIDValid, mUID);
}
msg->setObject("source", source);
msg->post();
}
-void NuPlayer::setDataSource(int fd, int64_t offset, int64_t length) {
+void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) {
sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
- sp<Source> source = new GenericSource(fd, offset, length);
+ sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
+
+ sp<Source> source = new GenericSource(notify, fd, offset, length);
msg->setObject("source", source);
msg->post();
}
-void NuPlayer::setVideoSurfaceTexture(const sp<ISurfaceTexture> &surfaceTexture) {
+void NuPlayer::prepareAsync() {
+ (new AMessage(kWhatPrepare, id()))->post();
+}
+
+void NuPlayer::setVideoSurfaceTextureAsync(
+ const sp<IGraphicBufferProducer> &bufferProducer) {
sp<AMessage> msg = new AMessage(kWhatSetVideoNativeWindow, id());
- sp<SurfaceTextureClient> surfaceTextureClient(surfaceTexture != NULL ?
- new SurfaceTextureClient(surfaceTexture) : NULL);
- msg->setObject("native-window", new NativeWindowWrapper(surfaceTextureClient));
+
+ if (bufferProducer == NULL) {
+ msg->setObject("native-window", NULL);
+ } else {
+ msg->setObject(
+ "native-window",
+ new NativeWindowWrapper(
+ new Surface(bufferProducer)));
+ }
+
msg->post();
}
@@ -207,6 +326,82 @@ void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
CHECK(msg->findObject("source", &obj));
mSource = static_cast<Source *>(obj.get());
+
+ looper()->registerHandler(mSource);
+
+ CHECK(mDriver != NULL);
+ sp<NuPlayerDriver> driver = mDriver.promote();
+ if (driver != NULL) {
+ driver->notifySetDataSourceCompleted(OK);
+ }
+ break;
+ }
+
+ case kWhatPrepare:
+ {
+ mSource->prepareAsync();
+ break;
+ }
+
+ case kWhatGetTrackInfo:
+ {
+ uint32_t replyID;
+ CHECK(msg->senderAwaitsResponse(&replyID));
+
+ status_t err = INVALID_OPERATION;
+ if (mSource != NULL) {
+ Parcel* reply;
+ CHECK(msg->findPointer("reply", (void**)&reply));
+ err = mSource->getTrackInfo(reply);
+ }
+
+ sp<AMessage> response = new AMessage;
+ response->setInt32("err", err);
+
+ response->postReply(replyID);
+ break;
+ }
+
+ case kWhatSelectTrack:
+ {
+ uint32_t replyID;
+ CHECK(msg->senderAwaitsResponse(&replyID));
+
+ status_t err = INVALID_OPERATION;
+ if (mSource != NULL) {
+ size_t trackIndex;
+ int32_t select;
+ CHECK(msg->findSize("trackIndex", &trackIndex));
+ CHECK(msg->findInt32("select", &select));
+ err = mSource->selectTrack(trackIndex, select);
+ }
+
+ sp<AMessage> response = new AMessage;
+ response->setInt32("err", err);
+
+ response->postReply(replyID);
+ break;
+ }
+
+ case kWhatPollDuration:
+ {
+ int32_t generation;
+ CHECK(msg->findInt32("generation", &generation));
+
+ if (generation != mPollDurationGeneration) {
+ // stale
+ break;
+ }
+
+ int64_t durationUs;
+ if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
+ sp<NuPlayerDriver> driver = mDriver.promote();
+ if (driver != NULL) {
+ driver->notifyDuration(durationUs);
+ }
+ }
+
+ msg->post(1000000ll); // poll again in a second.
break;
}
@@ -214,13 +409,25 @@ void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
{
ALOGV("kWhatSetVideoNativeWindow");
+ mDeferredActions.push_back(
+ new ShutdownDecoderAction(
+ false /* audio */, true /* video */));
+
sp<RefBase> obj;
CHECK(msg->findObject("native-window", &obj));
- mNativeWindow = static_cast<NativeWindowWrapper *>(obj.get());
+ mDeferredActions.push_back(
+ new SetSurfaceAction(
+ static_cast<NativeWindowWrapper *>(obj.get())));
+
+ if (obj != NULL) {
+ // If there is a new surface texture, instantiate decoders
+ // again if possible.
+ mDeferredActions.push_back(
+ new SimpleAction(&NuPlayer::performScanSources));
+ }
- // XXX - ignore error from setVideoScalingMode for now
- setVideoScalingMode(mVideoScalingMode);
+ processDeferredActions();
break;
}
@@ -240,6 +447,7 @@ void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
ALOGV("kWhatStart");
mVideoIsAVC = false;
+ mNeedsSwRenderer = false;
mAudioEOS = false;
mVideoEOS = false;
mSkipRenderingAudioUntilMediaTimeUs = -1;
@@ -247,12 +455,20 @@ void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
mVideoLateByUs = 0;
mNumFramesTotal = 0;
mNumFramesDropped = 0;
+ mStarted = true;
mSource->start();
+ uint32_t flags = 0;
+
+ if (mSource->isRealTime()) {
+ flags |= Renderer::FLAG_REAL_TIME;
+ }
+
mRenderer = new Renderer(
mAudioSink,
- new AMessage(kWhatRendererNotify, id()));
+ new AMessage(kWhatRendererNotify, id()),
+ flags);
looper()->registerHandler(mRenderer);
@@ -274,6 +490,9 @@ void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
mAudioDecoder != NULL, mVideoDecoder != NULL);
+ bool mHadAnySourcesBefore =
+ (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
+
if (mNativeWindow != NULL) {
instantiateDecoder(false, &mVideoDecoder);
}
@@ -282,6 +501,15 @@ void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
instantiateDecoder(true, &mAudioDecoder);
}
+ if (!mHadAnySourcesBefore
+ && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
+ // This is the first time we've found anything playable.
+
+ if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
+ schedulePollDuration();
+ }
+ }
+
status_t err;
if ((err = mSource->feedMoreTSData()) != OK) {
if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
@@ -370,7 +598,8 @@ void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
} else if (what == ACodec::kWhatOutputFormatChanged) {
if (audio) {
int32_t numChannels;
- CHECK(codecRequest->findInt32("channel-count", &numChannels));
+ CHECK(codecRequest->findInt32(
+ "channel-count", &numChannels));
int32_t sampleRate;
CHECK(codecRequest->findInt32("sample-rate", &sampleRate));
@@ -382,13 +611,15 @@ void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
audio_output_flags_t flags;
int64_t durationUs;
- // FIXME: we should handle the case where the video decoder is created after
- // we receive the format change indication. Current code will just make that
- // we select deep buffer with video which should not be a problem as it should
+ // FIXME: we should handle the case where the video decoder
+ // is created after we receive the format change indication.
+ // Current code will just make that we select deep buffer
+ // with video which should not be a problem as it should
// not prevent from keeping A/V sync.
if (mVideoDecoder == NULL &&
mSource->getDuration(&durationUs) == OK &&
- durationUs > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US) {
+ durationUs
+ > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US) {
flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
} else {
flags = AUDIO_OUTPUT_FLAG_NONE;
@@ -424,17 +655,49 @@ void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
"crop",
&cropLeft, &cropTop, &cropRight, &cropBottom));
+ int32_t displayWidth = cropRight - cropLeft + 1;
+ int32_t displayHeight = cropBottom - cropTop + 1;
+
ALOGV("Video output format changed to %d x %d "
"(crop: %d x %d @ (%d, %d))",
width, height,
- (cropRight - cropLeft + 1),
- (cropBottom - cropTop + 1),
+ displayWidth,
+ displayHeight,
cropLeft, cropTop);
+ sp<AMessage> videoInputFormat =
+ mSource->getFormat(false /* audio */);
+
+ // Take into account sample aspect ratio if necessary:
+ int32_t sarWidth, sarHeight;
+ if (videoInputFormat->findInt32("sar-width", &sarWidth)
+ && videoInputFormat->findInt32(
+ "sar-height", &sarHeight)) {
+ ALOGV("Sample aspect ratio %d : %d",
+ sarWidth, sarHeight);
+
+ displayWidth = (displayWidth * sarWidth) / sarHeight;
+
+ ALOGV("display dimensions %d x %d",
+ displayWidth, displayHeight);
+ }
+
notifyListener(
- MEDIA_SET_VIDEO_SIZE,
- cropRight - cropLeft + 1,
- cropBottom - cropTop + 1);
+ MEDIA_SET_VIDEO_SIZE, displayWidth, displayHeight);
+
+ if (mNeedsSwRenderer && mNativeWindow != NULL) {
+ int32_t colorFormat;
+ CHECK(codecRequest->findInt32("color-format", &colorFormat));
+
+ sp<MetaData> meta = new MetaData;
+ meta->setInt32(kKeyWidth, width);
+ meta->setInt32(kKeyHeight, height);
+ meta->setRect(kKeyCropRect, cropLeft, cropTop, cropRight, cropBottom);
+ meta->setInt32(kKeyColorFormat, colorFormat);
+
+ mRenderer->setSoftRenderer(
+ new SoftwareRenderer(mNativeWindow->getNativeWindow(), meta));
+ }
}
} else if (what == ACodec::kWhatShutdownCompleted) {
ALOGV("%s shutdown completed", audio ? "audio" : "video");
@@ -458,8 +721,20 @@ void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
mRenderer->queueEOS(audio, UNKNOWN_ERROR);
} else if (what == ACodec::kWhatDrainThisBuffer) {
renderBuffer(audio, codecRequest);
- } else {
- ALOGV("Unhandled codec notification %d.", what);
+ } else if (what == ACodec::kWhatComponentAllocated) {
+ if (!audio) {
+ AString name;
+ CHECK(codecRequest->findString("componentName", &name));
+ mNeedsSwRenderer = name.startsWith("OMX.google.");
+ }
+ } else if (what != ACodec::kWhatComponentConfigured
+ && what != ACodec::kWhatBuffersAllocated) {
+ ALOGV("Unhandled codec notification %d '%c%c%c%c'.",
+ what,
+ what >> 24,
+ (what >> 16) & 0xff,
+ (what >> 8) & 0xff,
+ what & 0xff);
}
break;
@@ -513,14 +788,15 @@ void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
}
}
} else if (what == Renderer::kWhatFlushComplete) {
- CHECK_EQ(what, (int32_t)Renderer::kWhatFlushComplete);
-
int32_t audio;
CHECK(msg->findInt32("audio", &audio));
ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
} else if (what == Renderer::kWhatVideoRenderingStart) {
notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
+ } else if (what == Renderer::kWhatMediaRenderingStart) {
+ ALOGV("media rendering started");
+ notifyListener(MEDIA_STARTED, 0, 0);
}
break;
}
@@ -534,45 +810,14 @@ void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
{
ALOGV("kWhatReset");
- if (mRenderer != NULL) {
- // There's an edge case where the renderer owns all output
- // buffers and is paused, therefore the decoder will not read
- // more input data and will never encounter the matching
- // discontinuity. To avoid this, we resume the renderer.
+ mDeferredActions.push_back(
+ new ShutdownDecoderAction(
+ true /* audio */, true /* video */));
- if (mFlushingAudio == AWAITING_DISCONTINUITY
- || mFlushingVideo == AWAITING_DISCONTINUITY) {
- mRenderer->resume();
- }
- }
-
- if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
- // We're currently flushing, postpone the reset until that's
- // completed.
-
- ALOGV("postponing reset mFlushingAudio=%d, mFlushingVideo=%d",
- mFlushingAudio, mFlushingVideo);
-
- mResetPostponed = true;
- break;
- }
-
- if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
- finishReset();
- break;
- }
-
- mTimeDiscontinuityPending = true;
+ mDeferredActions.push_back(
+ new SimpleAction(&NuPlayer::performReset));
- if (mAudioDecoder != NULL) {
- flushDecoder(true /* audio */, true /* needShutdown */);
- }
-
- if (mVideoDecoder != NULL) {
- flushDecoder(false /* audio */, true /* needShutdown */);
- }
-
- mResetInProgress = true;
+ processDeferredActions();
break;
}
@@ -581,24 +826,21 @@ void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
int64_t seekTimeUs;
CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
- ALOGV("kWhatSeek seekTimeUs=%lld us (%.2f secs)",
- seekTimeUs, seekTimeUs / 1E6);
+ ALOGV("kWhatSeek seekTimeUs=%lld us", seekTimeUs);
- mSource->seekTo(seekTimeUs);
+ mDeferredActions.push_back(
+ new SimpleAction(&NuPlayer::performDecoderFlush));
- if (mDriver != NULL) {
- sp<NuPlayerDriver> driver = mDriver.promote();
- if (driver != NULL) {
- driver->notifySeekComplete();
- }
- }
+ mDeferredActions.push_back(new SeekAction(seekTimeUs));
+ processDeferredActions();
break;
}
case kWhatPause:
{
CHECK(mRenderer != NULL);
+ mSource->pause();
mRenderer->pause();
break;
}
@@ -606,10 +848,17 @@ void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
case kWhatResume:
{
CHECK(mRenderer != NULL);
+ mSource->resume();
mRenderer->resume();
break;
}
+ case kWhatSourceNotify:
+ {
+ onSourceNotify(msg);
+ break;
+ }
+
default:
TRESPASS();
break;
@@ -643,39 +892,7 @@ void NuPlayer::finishFlushIfPossible() {
mFlushingAudio = NONE;
mFlushingVideo = NONE;
- if (mResetInProgress) {
- ALOGV("reset completed");
-
- mResetInProgress = false;
- finishReset();
- } else if (mResetPostponed) {
- (new AMessage(kWhatReset, id()))->post();
- mResetPostponed = false;
- } else if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
- postScanSources();
- }
-}
-
-void NuPlayer::finishReset() {
- CHECK(mAudioDecoder == NULL);
- CHECK(mVideoDecoder == NULL);
-
- ++mScanSourcesGeneration;
- mScanSourcesPending = false;
-
- mRenderer.clear();
-
- if (mSource != NULL) {
- mSource->stop();
- mSource.clear();
- }
-
- if (mDriver != NULL) {
- sp<NuPlayerDriver> driver = mDriver.promote();
- if (driver != NULL) {
- driver->notifyResetComplete();
- }
- }
+ processDeferredActions();
}
void NuPlayer::postScanSources() {
@@ -717,14 +934,6 @@ status_t NuPlayer::instantiateDecoder(bool audio, sp<Decoder> *decoder) {
(*decoder)->configure(format);
- int64_t durationUs;
- if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
- sp<NuPlayerDriver> driver = mDriver.promote();
- if (driver != NULL) {
- driver->notifyDuration(durationUs);
- }
- }
-
return OK;
}
@@ -794,6 +1003,14 @@ status_t NuPlayer::feedDecoderInputData(bool audio, const sp<AMessage> &msg) {
mTimeDiscontinuityPending || timeChange;
if (formatChange || timeChange) {
+ if (mFlushingAudio == NONE && mFlushingVideo == NONE) {
+ // And we'll resume scanning sources once we're done
+ // flushing.
+ mDeferredActions.push_front(
+ new SimpleAction(
+ &NuPlayer::performScanSources));
+ }
+
flushDecoder(audio, formatChange);
} else {
// This stream is unaffected by the discontinuity
@@ -891,7 +1108,7 @@ void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) {
mRenderer->queueBuffer(audio, buffer, reply);
}
-void NuPlayer::notifyListener(int msg, int ext1, int ext2) {
+void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
if (mDriver == NULL) {
return;
}
@@ -902,10 +1119,13 @@ void NuPlayer::notifyListener(int msg, int ext1, int ext2) {
return;
}
- driver->notifyListener(msg, ext1, ext2);
+ driver->notifyListener(msg, ext1, ext2, in);
}
void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
+ ALOGV("[%s] flushDecoder needShutdown=%d",
+ audio ? "audio" : "video", needShutdown);
+
if ((audio && mAudioDecoder == NULL) || (!audio && mVideoDecoder == NULL)) {
ALOGI("flushDecoder %s without decoder present",
audio ? "audio" : "video");
@@ -963,8 +1183,7 @@ sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
status_t NuPlayer::setVideoScalingMode(int32_t mode) {
mVideoScalingMode = mode;
- if (mNativeWindow != NULL
- && mNativeWindow->getNativeWindow() != NULL) {
+ if (mNativeWindow != NULL) {
status_t ret = native_window_set_scaling_mode(
mNativeWindow->getNativeWindow().get(), mVideoScalingMode);
if (ret != OK) {
@@ -976,4 +1195,352 @@ status_t NuPlayer::setVideoScalingMode(int32_t mode) {
return OK;
}
+status_t NuPlayer::getTrackInfo(Parcel* reply) const {
+ sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, id());
+ msg->setPointer("reply", reply);
+
+ sp<AMessage> response;
+ status_t err = msg->postAndAwaitResponse(&response);
+ return err;
+}
+
+status_t NuPlayer::selectTrack(size_t trackIndex, bool select) {
+ sp<AMessage> msg = new AMessage(kWhatSelectTrack, id());
+ msg->setSize("trackIndex", trackIndex);
+ msg->setInt32("select", select);
+
+ sp<AMessage> response;
+ status_t err = msg->postAndAwaitResponse(&response);
+
+ return err;
+}
+
+void NuPlayer::schedulePollDuration() {
+ sp<AMessage> msg = new AMessage(kWhatPollDuration, id());
+ msg->setInt32("generation", mPollDurationGeneration);
+ msg->post();
+}
+
+void NuPlayer::cancelPollDuration() {
+ ++mPollDurationGeneration;
+}
+
+void NuPlayer::processDeferredActions() {
+ while (!mDeferredActions.empty()) {
+ // We won't execute any deferred actions until we're no longer in
+ // an intermediate state, i.e. one more more decoders are currently
+ // flushing or shutting down.
+
+ if (mRenderer != NULL) {
+ // There's an edge case where the renderer owns all output
+ // buffers and is paused, therefore the decoder will not read
+ // more input data and will never encounter the matching
+ // discontinuity. To avoid this, we resume the renderer.
+
+ if (mFlushingAudio == AWAITING_DISCONTINUITY
+ || mFlushingVideo == AWAITING_DISCONTINUITY) {
+ mRenderer->resume();
+ }
+ }
+
+ if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
+ // We're currently flushing, postpone the reset until that's
+ // completed.
+
+ ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
+ mFlushingAudio, mFlushingVideo);
+
+ break;
+ }
+
+ sp<Action> action = *mDeferredActions.begin();
+ mDeferredActions.erase(mDeferredActions.begin());
+
+ action->execute(this);
+ }
+}
+
+void NuPlayer::performSeek(int64_t seekTimeUs) {
+ ALOGV("performSeek seekTimeUs=%lld us (%.2f secs)",
+ seekTimeUs,
+ seekTimeUs / 1E6);
+
+ mSource->seekTo(seekTimeUs);
+
+ if (mDriver != NULL) {
+ sp<NuPlayerDriver> driver = mDriver.promote();
+ if (driver != NULL) {
+ driver->notifyPosition(seekTimeUs);
+ driver->notifySeekComplete();
+ }
+ }
+
+ // everything's flushed, continue playback.
+}
+
+void NuPlayer::performDecoderFlush() {
+ ALOGV("performDecoderFlush");
+
+ if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
+ return;
+ }
+
+ mTimeDiscontinuityPending = true;
+
+ if (mAudioDecoder != NULL) {
+ flushDecoder(true /* audio */, false /* needShutdown */);
+ }
+
+ if (mVideoDecoder != NULL) {
+ flushDecoder(false /* audio */, false /* needShutdown */);
+ }
+}
+
+void NuPlayer::performDecoderShutdown(bool audio, bool video) {
+ ALOGV("performDecoderShutdown audio=%d, video=%d", audio, video);
+
+ if ((!audio || mAudioDecoder == NULL)
+ && (!video || mVideoDecoder == NULL)) {
+ return;
+ }
+
+ mTimeDiscontinuityPending = true;
+
+ if (mFlushingAudio == NONE && (!audio || mAudioDecoder == NULL)) {
+ mFlushingAudio = FLUSHED;
+ }
+
+ if (mFlushingVideo == NONE && (!video || mVideoDecoder == NULL)) {
+ mFlushingVideo = FLUSHED;
+ }
+
+ if (audio && mAudioDecoder != NULL) {
+ flushDecoder(true /* audio */, true /* needShutdown */);
+ }
+
+ if (video && mVideoDecoder != NULL) {
+ flushDecoder(false /* audio */, true /* needShutdown */);
+ }
+}
+
+void NuPlayer::performReset() {
+ ALOGV("performReset");
+
+ CHECK(mAudioDecoder == NULL);
+ CHECK(mVideoDecoder == NULL);
+
+ cancelPollDuration();
+
+ ++mScanSourcesGeneration;
+ mScanSourcesPending = false;
+
+ mRenderer.clear();
+
+ if (mSource != NULL) {
+ mSource->stop();
+
+ looper()->unregisterHandler(mSource->id());
+
+ mSource.clear();
+ }
+
+ if (mDriver != NULL) {
+ sp<NuPlayerDriver> driver = mDriver.promote();
+ if (driver != NULL) {
+ driver->notifyResetComplete();
+ }
+ }
+
+ mStarted = false;
+}
+
+void NuPlayer::performScanSources() {
+ ALOGV("performScanSources");
+
+ if (!mStarted) {
+ return;
+ }
+
+ if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
+ postScanSources();
+ }
+}
+
+void NuPlayer::performSetSurface(const sp<NativeWindowWrapper> &wrapper) {
+ ALOGV("performSetSurface");
+
+ mNativeWindow = wrapper;
+
+ // XXX - ignore error from setVideoScalingMode for now
+ setVideoScalingMode(mVideoScalingMode);
+
+ if (mDriver != NULL) {
+ sp<NuPlayerDriver> driver = mDriver.promote();
+ if (driver != NULL) {
+ driver->notifySetSurfaceComplete();
+ }
+ }
+}
+
+void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
+ int32_t what;
+ CHECK(msg->findInt32("what", &what));
+
+ switch (what) {
+ case Source::kWhatPrepared:
+ {
+ if (mSource == NULL) {
+ // This is a stale notification from a source that was
+ // asynchronously preparing when the client called reset().
+ // We handled the reset, the source is gone.
+ break;
+ }
+
+ int32_t err;
+ CHECK(msg->findInt32("err", &err));
+
+ sp<NuPlayerDriver> driver = mDriver.promote();
+ if (driver != NULL) {
+ driver->notifyPrepareCompleted(err);
+ }
+
+ int64_t durationUs;
+ if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
+ sp<NuPlayerDriver> driver = mDriver.promote();
+ if (driver != NULL) {
+ driver->notifyDuration(durationUs);
+ }
+ }
+ break;
+ }
+
+ case Source::kWhatFlagsChanged:
+ {
+ uint32_t flags;
+ CHECK(msg->findInt32("flags", (int32_t *)&flags));
+
+ sp<NuPlayerDriver> driver = mDriver.promote();
+ if (driver != NULL) {
+ driver->notifyFlagsChanged(flags);
+ }
+
+ if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
+ && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
+ cancelPollDuration();
+ } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
+ && (flags & Source::FLAG_DYNAMIC_DURATION)
+ && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
+ schedulePollDuration();
+ }
+
+ mSourceFlags = flags;
+ break;
+ }
+
+ case Source::kWhatVideoSizeChanged:
+ {
+ int32_t width, height;
+ CHECK(msg->findInt32("width", &width));
+ CHECK(msg->findInt32("height", &height));
+
+ notifyListener(MEDIA_SET_VIDEO_SIZE, width, height);
+ break;
+ }
+
+ case Source::kWhatBufferingStart:
+ {
+ notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
+ break;
+ }
+
+ case Source::kWhatBufferingEnd:
+ {
+ notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
+ break;
+ }
+
+ case Source::kWhatSubtitleData:
+ {
+ sp<ABuffer> buffer;
+ CHECK(msg->findBuffer("buffer", &buffer));
+
+ int32_t trackIndex;
+ int64_t timeUs, durationUs;
+ CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
+ CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
+ CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
+
+ Parcel in;
+ in.writeInt32(trackIndex);
+ in.writeInt64(timeUs);
+ in.writeInt64(durationUs);
+ in.writeInt32(buffer->size());
+ in.writeInt32(buffer->size());
+ in.write(buffer->data(), buffer->size());
+
+ notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
+ break;
+ }
+
+ case Source::kWhatQueueDecoderShutdown:
+ {
+ int32_t audio, video;
+ CHECK(msg->findInt32("audio", &audio));
+ CHECK(msg->findInt32("video", &video));
+
+ sp<AMessage> reply;
+ CHECK(msg->findMessage("reply", &reply));
+
+ queueDecoderShutdown(audio, video, reply);
+ break;
+ }
+
+ default:
+ TRESPASS();
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
+ sp<AMessage> notify = dupNotify();
+ notify->setInt32("what", kWhatFlagsChanged);
+ notify->setInt32("flags", flags);
+ notify->post();
+}
+
+void NuPlayer::Source::notifyVideoSizeChanged(int32_t width, int32_t height) {
+ sp<AMessage> notify = dupNotify();
+ notify->setInt32("what", kWhatVideoSizeChanged);
+ notify->setInt32("width", width);
+ notify->setInt32("height", height);
+ notify->post();
+}
+
+void NuPlayer::Source::notifyPrepared(status_t err) {
+ sp<AMessage> notify = dupNotify();
+ notify->setInt32("what", kWhatPrepared);
+ notify->setInt32("err", err);
+ notify->post();
+}
+
+void NuPlayer::Source::onMessageReceived(const sp<AMessage> &msg) {
+ TRESPASS();
+}
+
+void NuPlayer::queueDecoderShutdown(
+ bool audio, bool video, const sp<AMessage> &reply) {
+ ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
+
+ mDeferredActions.push_back(
+ new ShutdownDecoderAction(audio, video));
+
+ mDeferredActions.push_back(
+ new SimpleAction(&NuPlayer::performScanSources));
+
+ mDeferredActions.push_back(new PostMessageAction(reply));
+
+ processDeferredActions();
+}
+
} // namespace android
diff --git a/media/libmediaplayerservice/nuplayer/NuPlayer.h b/media/libmediaplayerservice/nuplayer/NuPlayer.h
index 36d3a9c..590e1f2 100644
--- a/media/libmediaplayerservice/nuplayer/NuPlayer.h
+++ b/media/libmediaplayerservice/nuplayer/NuPlayer.h
@@ -35,14 +35,18 @@ struct NuPlayer : public AHandler {
void setDriver(const wp<NuPlayerDriver> &driver);
- void setDataSource(const sp<IStreamSource> &source);
+ void setDataSourceAsync(const sp<IStreamSource> &source);
- void setDataSource(
+ void setDataSourceAsync(
const char *url, const KeyedVector<String8, String8> *headers);
- void setDataSource(int fd, int64_t offset, int64_t length);
+ void setDataSourceAsync(int fd, int64_t offset, int64_t length);
+
+ void prepareAsync();
+
+ void setVideoSurfaceTextureAsync(
+ const sp<IGraphicBufferProducer> &bufferProducer);
- void setVideoSurfaceTexture(const sp<ISurfaceTexture> &surfaceTexture);
void setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink);
void start();
@@ -56,6 +60,8 @@ struct NuPlayer : public AHandler {
void seekToAsync(int64_t seekTimeUs);
status_t setVideoScalingMode(int32_t mode);
+ status_t getTrackInfo(Parcel* reply) const;
+ status_t selectTrack(size_t trackIndex, bool select);
protected:
virtual ~NuPlayer();
@@ -73,9 +79,16 @@ private:
struct Renderer;
struct RTSPSource;
struct StreamingSource;
+ struct Action;
+ struct SeekAction;
+ struct SetSurfaceAction;
+ struct ShutdownDecoderAction;
+ struct PostMessageAction;
+ struct SimpleAction;
enum {
kWhatSetDataSource = '=DaS',
+ kWhatPrepare = 'prep',
kWhatSetVideoNativeWindow = '=NaW',
kWhatSetAudioSink = '=AuS',
kWhatMoreDataQueued = 'more',
@@ -88,25 +101,35 @@ private:
kWhatSeek = 'seek',
kWhatPause = 'paus',
kWhatResume = 'rsme',
+ kWhatPollDuration = 'polD',
+ kWhatSourceNotify = 'srcN',
+ kWhatGetTrackInfo = 'gTrI',
+ kWhatSelectTrack = 'selT',
};
wp<NuPlayerDriver> mDriver;
bool mUIDValid;
uid_t mUID;
sp<Source> mSource;
+ uint32_t mSourceFlags;
sp<NativeWindowWrapper> mNativeWindow;
sp<MediaPlayerBase::AudioSink> mAudioSink;
sp<Decoder> mVideoDecoder;
bool mVideoIsAVC;
+ bool mNeedsSwRenderer;
sp<Decoder> mAudioDecoder;
sp<Renderer> mRenderer;
+ List<sp<Action> > mDeferredActions;
+
bool mAudioEOS;
bool mVideoEOS;
bool mScanSourcesPending;
int32_t mScanSourcesGeneration;
+ int32_t mPollDurationGeneration;
+
enum FlushStatus {
NONE,
AWAITING_DISCONTINUITY,
@@ -123,8 +146,6 @@ private:
FlushStatus mFlushingAudio;
FlushStatus mFlushingVideo;
- bool mResetInProgress;
- bool mResetPostponed;
int64_t mSkipRenderingAudioUntilMediaTimeUs;
int64_t mSkipRenderingVideoUntilMediaTimeUs;
@@ -134,12 +155,14 @@ private:
int32_t mVideoScalingMode;
+ bool mStarted;
+
status_t instantiateDecoder(bool audio, sp<Decoder> *decoder);
status_t feedDecoderInputData(bool audio, const sp<AMessage> &msg);
void renderBuffer(bool audio, const sp<AMessage> &msg);
- void notifyListener(int msg, int ext1, int ext2);
+ void notifyListener(int msg, int ext1, int ext2, const Parcel *in = NULL);
void finishFlushIfPossible();
@@ -147,9 +170,25 @@ private:
static bool IsFlushingState(FlushStatus state, bool *needShutdown = NULL);
- void finishReset();
void postScanSources();
+ void schedulePollDuration();
+ void cancelPollDuration();
+
+ void processDeferredActions();
+
+ void performSeek(int64_t seekTimeUs);
+ void performDecoderFlush();
+ void performDecoderShutdown(bool audio, bool video);
+ void performReset();
+ void performScanSources();
+ void performSetSurface(const sp<NativeWindowWrapper> &wrapper);
+
+ void onSourceNotify(const sp<AMessage> &msg);
+
+ void queueDecoderShutdown(
+ bool audio, bool video, const sp<AMessage> &reply);
+
DISALLOW_EVIL_CONSTRUCTORS(NuPlayer);
};
diff --git a/media/libmediaplayerservice/nuplayer/NuPlayerDriver.cpp b/media/libmediaplayerservice/nuplayer/NuPlayerDriver.cpp
index d03601f..239296e 100644
--- a/media/libmediaplayerservice/nuplayer/NuPlayerDriver.cpp
+++ b/media/libmediaplayerservice/nuplayer/NuPlayerDriver.cpp
@@ -16,25 +16,31 @@
//#define LOG_NDEBUG 0
#define LOG_TAG "NuPlayerDriver"
+#include <inttypes.h>
#include <utils/Log.h>
#include "NuPlayerDriver.h"
#include "NuPlayer.h"
+#include "NuPlayerSource.h"
#include <media/stagefright/foundation/ADebug.h>
#include <media/stagefright/foundation/ALooper.h>
+#include <media/stagefright/MetaData.h>
namespace android {
NuPlayerDriver::NuPlayerDriver()
- : mResetInProgress(false),
+ : mState(STATE_IDLE),
+ mIsAsyncPrepare(false),
+ mAsyncResult(UNKNOWN_ERROR),
+ mSetSurfaceInProgress(false),
mDurationUs(-1),
mPositionUs(-1),
mNumFramesTotal(0),
mNumFramesDropped(0),
mLooper(new ALooper),
- mState(UNINITIALIZED),
+ mPlayerFlags(0),
mAtEOS(false),
mStartupSeekTimeUs(-1) {
mLooper->setName("NuPlayerDriver Looper");
@@ -66,60 +72,143 @@ status_t NuPlayerDriver::setUID(uid_t uid) {
status_t NuPlayerDriver::setDataSource(
const char *url, const KeyedVector<String8, String8> *headers) {
- CHECK_EQ((int)mState, (int)UNINITIALIZED);
+ Mutex::Autolock autoLock(mLock);
- mPlayer->setDataSource(url, headers);
+ if (mState != STATE_IDLE) {
+ return INVALID_OPERATION;
+ }
- mState = STOPPED;
+ mState = STATE_SET_DATASOURCE_PENDING;
- return OK;
+ mPlayer->setDataSourceAsync(url, headers);
+
+ while (mState == STATE_SET_DATASOURCE_PENDING) {
+ mCondition.wait(mLock);
+ }
+
+ return mAsyncResult;
}
status_t NuPlayerDriver::setDataSource(int fd, int64_t offset, int64_t length) {
- CHECK_EQ((int)mState, (int)UNINITIALIZED);
+ Mutex::Autolock autoLock(mLock);
- mPlayer->setDataSource(fd, offset, length);
+ if (mState != STATE_IDLE) {
+ return INVALID_OPERATION;
+ }
- mState = STOPPED;
+ mState = STATE_SET_DATASOURCE_PENDING;
- return OK;
+ mPlayer->setDataSourceAsync(fd, offset, length);
+
+ while (mState == STATE_SET_DATASOURCE_PENDING) {
+ mCondition.wait(mLock);
+ }
+
+ return mAsyncResult;
}
status_t NuPlayerDriver::setDataSource(const sp<IStreamSource> &source) {
- CHECK_EQ((int)mState, (int)UNINITIALIZED);
+ Mutex::Autolock autoLock(mLock);
- mPlayer->setDataSource(source);
+ if (mState != STATE_IDLE) {
+ return INVALID_OPERATION;
+ }
- mState = STOPPED;
+ mState = STATE_SET_DATASOURCE_PENDING;
- return OK;
+ mPlayer->setDataSourceAsync(source);
+
+ while (mState == STATE_SET_DATASOURCE_PENDING) {
+ mCondition.wait(mLock);
+ }
+
+ return mAsyncResult;
}
status_t NuPlayerDriver::setVideoSurfaceTexture(
- const sp<ISurfaceTexture> &surfaceTexture) {
- mPlayer->setVideoSurfaceTexture(surfaceTexture);
+ const sp<IGraphicBufferProducer> &bufferProducer) {
+ Mutex::Autolock autoLock(mLock);
+
+ if (mSetSurfaceInProgress) {
+ return INVALID_OPERATION;
+ }
+
+ switch (mState) {
+ case STATE_SET_DATASOURCE_PENDING:
+ case STATE_RESET_IN_PROGRESS:
+ return INVALID_OPERATION;
+
+ default:
+ break;
+ }
+
+ mSetSurfaceInProgress = true;
+
+ mPlayer->setVideoSurfaceTextureAsync(bufferProducer);
+
+ while (mSetSurfaceInProgress) {
+ mCondition.wait(mLock);
+ }
return OK;
}
status_t NuPlayerDriver::prepare() {
- sendEvent(MEDIA_SET_VIDEO_SIZE, 0, 0);
- return OK;
+ Mutex::Autolock autoLock(mLock);
+ return prepare_l();
}
-status_t NuPlayerDriver::prepareAsync() {
- status_t err = prepare();
+status_t NuPlayerDriver::prepare_l() {
+ switch (mState) {
+ case STATE_UNPREPARED:
+ mState = STATE_PREPARING;
+
+ // Make sure we're not posting any notifications, success or
+ // failure information is only communicated through our result
+ // code.
+ mIsAsyncPrepare = false;
+ mPlayer->prepareAsync();
+ while (mState == STATE_PREPARING) {
+ mCondition.wait(mLock);
+ }
+ return (mState == STATE_PREPARED) ? OK : UNKNOWN_ERROR;
+ default:
+ return INVALID_OPERATION;
+ };
+}
- notifyListener(MEDIA_PREPARED);
+status_t NuPlayerDriver::prepareAsync() {
+ Mutex::Autolock autoLock(mLock);
- return err;
+ switch (mState) {
+ case STATE_UNPREPARED:
+ mState = STATE_PREPARING;
+ mIsAsyncPrepare = true;
+ mPlayer->prepareAsync();
+ return OK;
+ default:
+ return INVALID_OPERATION;
+ };
}
status_t NuPlayerDriver::start() {
+ Mutex::Autolock autoLock(mLock);
+
switch (mState) {
- case UNINITIALIZED:
- return INVALID_OPERATION;
- case STOPPED:
+ case STATE_UNPREPARED:
+ {
+ status_t err = prepare_l();
+
+ if (err != OK) {
+ return err;
+ }
+
+ CHECK_EQ(mState, STATE_PREPARED);
+
+ // fall through
+ }
+
+ case STATE_PREPARED:
{
mAtEOS = false;
mPlayer->start();
@@ -133,21 +222,23 @@ status_t NuPlayerDriver::start() {
mStartupSeekTimeUs = -1;
}
-
break;
}
- case PLAYING:
- return OK;
- default:
- {
- CHECK_EQ((int)mState, (int)PAUSED);
+ case STATE_RUNNING:
+ break;
+
+ case STATE_PAUSED:
+ {
mPlayer->resume();
break;
}
+
+ default:
+ return INVALID_OPERATION;
}
- mState = PLAYING;
+ mState = STATE_RUNNING;
return OK;
}
@@ -157,52 +248,55 @@ status_t NuPlayerDriver::stop() {
}
status_t NuPlayerDriver::pause() {
+ Mutex::Autolock autoLock(mLock);
+
switch (mState) {
- case UNINITIALIZED:
- return INVALID_OPERATION;
- case STOPPED:
+ case STATE_PAUSED:
+ case STATE_PREPARED:
return OK;
- case PLAYING:
+
+ case STATE_RUNNING:
+ notifyListener(MEDIA_PAUSED);
mPlayer->pause();
break;
+
default:
- {
- CHECK_EQ((int)mState, (int)PAUSED);
- return OK;
- }
+ return INVALID_OPERATION;
}
- mState = PAUSED;
+ mState = STATE_PAUSED;
return OK;
}
bool NuPlayerDriver::isPlaying() {
- return mState == PLAYING && !mAtEOS;
+ return mState == STATE_RUNNING && !mAtEOS;
}
status_t NuPlayerDriver::seekTo(int msec) {
+ Mutex::Autolock autoLock(mLock);
+
int64_t seekTimeUs = msec * 1000ll;
switch (mState) {
- case UNINITIALIZED:
- return INVALID_OPERATION;
- case STOPPED:
+ case STATE_PREPARED:
{
mStartupSeekTimeUs = seekTimeUs;
break;
}
- case PLAYING:
- case PAUSED:
+
+ case STATE_RUNNING:
+ case STATE_PAUSED:
{
mAtEOS = false;
+ // seeks can take a while, so we essentially paused
+ notifyListener(MEDIA_PAUSED);
mPlayer->seekToAsync(seekTimeUs);
break;
}
default:
- TRESPASS();
- break;
+ return INVALID_OPERATION;
}
return OK;
@@ -224,27 +318,48 @@ status_t NuPlayerDriver::getDuration(int *msec) {
Mutex::Autolock autoLock(mLock);
if (mDurationUs < 0) {
- *msec = 0;
- } else {
- *msec = (mDurationUs + 500ll) / 1000;
+ return UNKNOWN_ERROR;
}
+ *msec = (mDurationUs + 500ll) / 1000;
+
return OK;
}
status_t NuPlayerDriver::reset() {
Mutex::Autolock autoLock(mLock);
- mResetInProgress = true;
+ switch (mState) {
+ case STATE_IDLE:
+ return OK;
+
+ case STATE_SET_DATASOURCE_PENDING:
+ case STATE_RESET_IN_PROGRESS:
+ return INVALID_OPERATION;
+
+ case STATE_PREPARING:
+ {
+ CHECK(mIsAsyncPrepare);
+
+ notifyListener(MEDIA_PREPARED);
+ break;
+ }
+
+ default:
+ break;
+ }
+
+ notifyListener(MEDIA_STOPPED);
+
+ mState = STATE_RESET_IN_PROGRESS;
mPlayer->resetAsync();
- while (mResetInProgress) {
+ while (mState == STATE_RESET_IN_PROGRESS) {
mCondition.wait(mLock);
}
mDurationUs = -1;
mPositionUs = -1;
- mState = UNINITIALIZED;
mStartupSeekTimeUs = -1;
return OK;
@@ -277,6 +392,24 @@ status_t NuPlayerDriver::invoke(const Parcel &request, Parcel *reply) {
int mode = request.readInt32();
return mPlayer->setVideoScalingMode(mode);
}
+
+ case INVOKE_ID_GET_TRACK_INFO:
+ {
+ return mPlayer->getTrackInfo(reply);
+ }
+
+ case INVOKE_ID_SELECT_TRACK:
+ {
+ int trackIndex = request.readInt32();
+ return mPlayer->selectTrack(trackIndex, true /* select */);
+ }
+
+ case INVOKE_ID_UNSELECT_TRACK:
+ {
+ int trackIndex = request.readInt32();
+ return mPlayer->selectTrack(trackIndex, false /* select */);
+ }
+
default:
{
return INVALID_OPERATION;
@@ -298,13 +431,45 @@ status_t NuPlayerDriver::getParameter(int key, Parcel *reply) {
status_t NuPlayerDriver::getMetadata(
const media::Metadata::Filter& ids, Parcel *records) {
- return INVALID_OPERATION;
+ Mutex::Autolock autoLock(mLock);
+
+ using media::Metadata;
+
+ Metadata meta(records);
+
+ meta.appendBool(
+ Metadata::kPauseAvailable,
+ mPlayerFlags & NuPlayer::Source::FLAG_CAN_PAUSE);
+
+ meta.appendBool(
+ Metadata::kSeekBackwardAvailable,
+ mPlayerFlags & NuPlayer::Source::FLAG_CAN_SEEK_BACKWARD);
+
+ meta.appendBool(
+ Metadata::kSeekForwardAvailable,
+ mPlayerFlags & NuPlayer::Source::FLAG_CAN_SEEK_FORWARD);
+
+ meta.appendBool(
+ Metadata::kSeekAvailable,
+ mPlayerFlags & NuPlayer::Source::FLAG_CAN_SEEK);
+
+ return OK;
}
void NuPlayerDriver::notifyResetComplete() {
Mutex::Autolock autoLock(mLock);
- CHECK(mResetInProgress);
- mResetInProgress = false;
+
+ CHECK_EQ(mState, STATE_RESET_IN_PROGRESS);
+ mState = STATE_IDLE;
+ mCondition.broadcast();
+}
+
+void NuPlayerDriver::notifySetSurfaceComplete() {
+ Mutex::Autolock autoLock(mLock);
+
+ CHECK(mSetSurfaceInProgress);
+ mSetSurfaceInProgress = false;
+
mCondition.broadcast();
}
@@ -335,7 +500,7 @@ status_t NuPlayerDriver::dump(int fd, const Vector<String16> &args) const {
FILE *out = fdopen(dup(fd), "w");
fprintf(out, " NuPlayer\n");
- fprintf(out, " numFramesTotal(%lld), numFramesDropped(%lld), "
+ fprintf(out, " numFramesTotal(%" PRId64 "), numFramesDropped(%" PRId64 "), "
"percentageDropped(%.2f)\n",
mNumFramesTotal,
mNumFramesDropped,
@@ -348,12 +513,59 @@ status_t NuPlayerDriver::dump(int fd, const Vector<String16> &args) const {
return OK;
}
-void NuPlayerDriver::notifyListener(int msg, int ext1, int ext2) {
+void NuPlayerDriver::notifyListener(
+ int msg, int ext1, int ext2, const Parcel *in) {
if (msg == MEDIA_PLAYBACK_COMPLETE || msg == MEDIA_ERROR) {
mAtEOS = true;
}
- sendEvent(msg, ext1, ext2);
+ sendEvent(msg, ext1, ext2, in);
+}
+
+void NuPlayerDriver::notifySetDataSourceCompleted(status_t err) {
+ Mutex::Autolock autoLock(mLock);
+
+ CHECK_EQ(mState, STATE_SET_DATASOURCE_PENDING);
+
+ mAsyncResult = err;
+ mState = (err == OK) ? STATE_UNPREPARED : STATE_IDLE;
+ mCondition.broadcast();
+}
+
+void NuPlayerDriver::notifyPrepareCompleted(status_t err) {
+ Mutex::Autolock autoLock(mLock);
+
+ if (mState != STATE_PREPARING) {
+ // We were preparing asynchronously when the client called
+ // reset(), we sent a premature "prepared" notification and
+ // then initiated the reset. This notification is stale.
+ CHECK(mState == STATE_RESET_IN_PROGRESS || mState == STATE_IDLE);
+ return;
+ }
+
+ CHECK_EQ(mState, STATE_PREPARING);
+
+ mAsyncResult = err;
+
+ if (err == OK) {
+ if (mIsAsyncPrepare) {
+ notifyListener(MEDIA_PREPARED);
+ }
+ mState = STATE_PREPARED;
+ } else {
+ if (mIsAsyncPrepare) {
+ notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
+ }
+ mState = STATE_UNPREPARED;
+ }
+
+ mCondition.broadcast();
+}
+
+void NuPlayerDriver::notifyFlagsChanged(uint32_t flags) {
+ Mutex::Autolock autoLock(mLock);
+
+ mPlayerFlags = flags;
}
} // namespace android
diff --git a/media/libmediaplayerservice/nuplayer/NuPlayerDriver.h b/media/libmediaplayerservice/nuplayer/NuPlayerDriver.h
index 4a0026c..99f72a6 100644
--- a/media/libmediaplayerservice/nuplayer/NuPlayerDriver.h
+++ b/media/libmediaplayerservice/nuplayer/NuPlayerDriver.h
@@ -38,7 +38,7 @@ struct NuPlayerDriver : public MediaPlayerInterface {
virtual status_t setDataSource(const sp<IStreamSource> &source);
virtual status_t setVideoSurfaceTexture(
- const sp<ISurfaceTexture> &surfaceTexture);
+ const sp<IGraphicBufferProducer> &bufferProducer);
virtual status_t prepare();
virtual status_t prepareAsync();
virtual status_t start();
@@ -61,23 +61,43 @@ struct NuPlayerDriver : public MediaPlayerInterface {
virtual status_t dump(int fd, const Vector<String16> &args) const;
+ void notifySetDataSourceCompleted(status_t err);
+ void notifyPrepareCompleted(status_t err);
void notifyResetComplete();
+ void notifySetSurfaceComplete();
void notifyDuration(int64_t durationUs);
void notifyPosition(int64_t positionUs);
void notifySeekComplete();
void notifyFrameStats(int64_t numFramesTotal, int64_t numFramesDropped);
- void notifyListener(int msg, int ext1 = 0, int ext2 = 0);
+ void notifyListener(int msg, int ext1 = 0, int ext2 = 0, const Parcel *in = NULL);
+ void notifyFlagsChanged(uint32_t flags);
protected:
virtual ~NuPlayerDriver();
private:
+ enum State {
+ STATE_IDLE,
+ STATE_SET_DATASOURCE_PENDING,
+ STATE_UNPREPARED,
+ STATE_PREPARING,
+ STATE_PREPARED,
+ STATE_RUNNING,
+ STATE_PAUSED,
+ STATE_RESET_IN_PROGRESS,
+ };
+
mutable Mutex mLock;
Condition mCondition;
+ State mState;
+
+ bool mIsAsyncPrepare;
+ status_t mAsyncResult;
+
// The following are protected through "mLock"
// >>>
- bool mResetInProgress;
+ bool mSetSurfaceInProgress;
int64_t mDurationUs;
int64_t mPositionUs;
int64_t mNumFramesTotal;
@@ -86,19 +106,14 @@ private:
sp<ALooper> mLooper;
sp<NuPlayer> mPlayer;
+ uint32_t mPlayerFlags;
- enum State {
- UNINITIALIZED,
- STOPPED,
- PLAYING,
- PAUSED
- };
-
- State mState;
bool mAtEOS;
int64_t mStartupSeekTimeUs;
+ status_t prepare_l();
+
DISALLOW_EVIL_CONSTRUCTORS(NuPlayerDriver);
};
diff --git a/media/libmediaplayerservice/nuplayer/NuPlayerRenderer.cpp b/media/libmediaplayerservice/nuplayer/NuPlayerRenderer.cpp
index 8a75f83..bf5271e 100644
--- a/media/libmediaplayerservice/nuplayer/NuPlayerRenderer.cpp
+++ b/media/libmediaplayerservice/nuplayer/NuPlayerRenderer.cpp
@@ -20,6 +20,8 @@
#include "NuPlayerRenderer.h"
+#include "SoftwareRenderer.h"
+
#include <media/stagefright/foundation/ABuffer.h>
#include <media/stagefright/foundation/ADebug.h>
#include <media/stagefright/foundation/AMessage.h>
@@ -31,9 +33,12 @@ const int64_t NuPlayer::Renderer::kMinPositionUpdateDelayUs = 100000ll;
NuPlayer::Renderer::Renderer(
const sp<MediaPlayerBase::AudioSink> &sink,
- const sp<AMessage> &notify)
+ const sp<AMessage> &notify,
+ uint32_t flags)
: mAudioSink(sink),
+ mSoftRenderer(NULL),
mNotify(notify),
+ mFlags(flags),
mNumFramesWritten(0),
mDrainAudioQueuePending(false),
mDrainVideoQueuePending(false),
@@ -48,11 +53,19 @@ NuPlayer::Renderer::Renderer(
mSyncQueues(false),
mPaused(false),
mVideoRenderingStarted(false),
+ mVideoRenderingStartGeneration(0),
+ mAudioRenderingStartGeneration(0),
mLastPositionUpdateUs(-1ll),
mVideoLateByUs(0ll) {
}
NuPlayer::Renderer::~Renderer() {
+ delete mSoftRenderer;
+}
+
+void NuPlayer::Renderer::setSoftRenderer(SoftwareRenderer *softRenderer) {
+ delete mSoftRenderer;
+ mSoftRenderer = softRenderer;
}
void NuPlayer::Renderer::queueBuffer(
@@ -93,11 +106,11 @@ void NuPlayer::Renderer::flush(bool audio) {
}
void NuPlayer::Renderer::signalTimeDiscontinuity() {
- CHECK(mAudioQueue.empty());
- CHECK(mVideoQueue.empty());
+ // CHECK(mAudioQueue.empty());
+ // CHECK(mVideoQueue.empty());
mAnchorTimeMediaUs = -1;
mAnchorTimeRealUs = -1;
- mSyncQueues = mHasAudio && mHasVideo;
+ mSyncQueues = false;
}
void NuPlayer::Renderer::pause() {
@@ -218,6 +231,23 @@ void NuPlayer::Renderer::signalAudioSinkChanged() {
(new AMessage(kWhatAudioSinkChanged, id()))->post();
}
+void NuPlayer::Renderer::prepareForMediaRenderingStart() {
+ mAudioRenderingStartGeneration = mAudioQueueGeneration;
+ mVideoRenderingStartGeneration = mVideoQueueGeneration;
+}
+
+void NuPlayer::Renderer::notifyIfMediaRenderingStarted() {
+ if (mVideoRenderingStartGeneration == mVideoQueueGeneration &&
+ mAudioRenderingStartGeneration == mAudioQueueGeneration) {
+ mVideoRenderingStartGeneration = -1;
+ mAudioRenderingStartGeneration = -1;
+
+ sp<AMessage> notify = mNotify->dup();
+ notify->setInt32("what", kWhatMediaRenderingStart);
+ notify->post();
+ }
+}
+
bool NuPlayer::Renderer::onDrainAudioQueue() {
uint32_t numFramesPlayed;
if (mAudioSink->getPosition(&numFramesPlayed) != OK) {
@@ -297,6 +327,8 @@ bool NuPlayer::Renderer::onDrainAudioQueue() {
numBytesAvailableToWrite -= copy;
size_t copiedFrames = copy / mAudioSink->frameSize();
mNumFramesWritten += copiedFrames;
+
+ notifyIfMediaRenderingStarted();
}
notifyPosition();
@@ -323,6 +355,11 @@ void NuPlayer::Renderer::postDrainVideoQueue() {
if (entry.mBuffer == NULL) {
// EOS doesn't carry a timestamp.
delayUs = 0;
+ } else if (mFlags & FLAG_REAL_TIME) {
+ int64_t mediaTimeUs;
+ CHECK(entry.mBuffer->meta()->findInt64("timeUs", &mediaTimeUs));
+
+ delayUs = mediaTimeUs - ALooper::GetNowUs();
} else {
int64_t mediaTimeUs;
CHECK(entry.mBuffer->meta()->findInt64("timeUs", &mediaTimeUs));
@@ -368,19 +405,29 @@ void NuPlayer::Renderer::onDrainVideoQueue() {
return;
}
- int64_t mediaTimeUs;
- CHECK(entry->mBuffer->meta()->findInt64("timeUs", &mediaTimeUs));
+ int64_t realTimeUs;
+ if (mFlags & FLAG_REAL_TIME) {
+ CHECK(entry->mBuffer->meta()->findInt64("timeUs", &realTimeUs));
+ } else {
+ int64_t mediaTimeUs;
+ CHECK(entry->mBuffer->meta()->findInt64("timeUs", &mediaTimeUs));
- int64_t realTimeUs = mediaTimeUs - mAnchorTimeMediaUs + mAnchorTimeRealUs;
- mVideoLateByUs = ALooper::GetNowUs() - realTimeUs;
+ realTimeUs = mediaTimeUs - mAnchorTimeMediaUs + mAnchorTimeRealUs;
+ }
+ mVideoLateByUs = ALooper::GetNowUs() - realTimeUs;
bool tooLate = (mVideoLateByUs > 40000);
if (tooLate) {
ALOGV("video late by %lld us (%.2f secs)",
mVideoLateByUs, mVideoLateByUs / 1E6);
} else {
- ALOGV("rendering video at media time %.2f secs", mediaTimeUs / 1E6);
+ ALOGV("rendering video at media time %.2f secs",
+ (mFlags & FLAG_REAL_TIME ? realTimeUs :
+ (realTimeUs + mAnchorTimeMediaUs - mAnchorTimeRealUs)) / 1E6);
+ if (mSoftRenderer != NULL) {
+ mSoftRenderer->render(entry->mBuffer->data(), entry->mBuffer->size(), NULL);
+ }
}
entry->mNotifyConsumed->setInt32("render", !tooLate);
@@ -393,6 +440,8 @@ void NuPlayer::Renderer::onDrainVideoQueue() {
notifyVideoRenderingStart();
}
+ notifyIfMediaRenderingStarted();
+
notifyPosition();
}
@@ -512,9 +561,15 @@ void NuPlayer::Renderer::onQueueEOS(const sp<AMessage> &msg) {
entry.mFinalResult = finalResult;
if (audio) {
+ if (mAudioQueue.empty() && mSyncQueues) {
+ syncQueuesDone();
+ }
mAudioQueue.push_back(entry);
postDrainAudioQueue();
} else {
+ if (mVideoQueue.empty() && mSyncQueues) {
+ syncQueuesDone();
+ }
mVideoQueue.push_back(entry);
postDrainVideoQueue();
}
@@ -534,6 +589,7 @@ void NuPlayer::Renderer::onFlush(const sp<AMessage> &msg) {
// is flushed.
syncQueuesDone();
+ ALOGV("flushing %s", audio ? "audio" : "video");
if (audio) {
flushQueue(&mAudioQueue);
@@ -542,6 +598,8 @@ void NuPlayer::Renderer::onFlush(const sp<AMessage> &msg) {
mDrainAudioQueuePending = false;
++mAudioQueueGeneration;
+
+ prepareForMediaRenderingStart();
} else {
flushQueue(&mVideoQueue);
@@ -550,6 +608,8 @@ void NuPlayer::Renderer::onFlush(const sp<AMessage> &msg) {
mDrainVideoQueuePending = false;
++mVideoQueueGeneration;
+
+ prepareForMediaRenderingStart();
}
notifyFlushComplete(audio);
@@ -640,6 +700,8 @@ void NuPlayer::Renderer::onPause() {
mDrainVideoQueuePending = false;
++mVideoQueueGeneration;
+ prepareForMediaRenderingStart();
+
if (mHasAudio) {
mAudioSink->pause();
}
diff --git a/media/libmediaplayerservice/nuplayer/NuPlayerRenderer.h b/media/libmediaplayerservice/nuplayer/NuPlayerRenderer.h
index e4368c7..9124e03 100644
--- a/media/libmediaplayerservice/nuplayer/NuPlayerRenderer.h
+++ b/media/libmediaplayerservice/nuplayer/NuPlayerRenderer.h
@@ -23,10 +23,15 @@
namespace android {
struct ABuffer;
+class SoftwareRenderer;
struct NuPlayer::Renderer : public AHandler {
+ enum Flags {
+ FLAG_REAL_TIME = 1,
+ };
Renderer(const sp<MediaPlayerBase::AudioSink> &sink,
- const sp<AMessage> &notify);
+ const sp<AMessage> &notify,
+ uint32_t flags = 0);
void queueBuffer(
bool audio,
@@ -49,8 +54,11 @@ struct NuPlayer::Renderer : public AHandler {
kWhatFlushComplete = 'fluC',
kWhatPosition = 'posi',
kWhatVideoRenderingStart = 'vdrd',
+ kWhatMediaRenderingStart = 'mdrd',
};
+ void setSoftRenderer(SoftwareRenderer *softRenderer);
+
protected:
virtual ~Renderer();
@@ -78,7 +86,9 @@ private:
static const int64_t kMinPositionUpdateDelayUs;
sp<MediaPlayerBase::AudioSink> mAudioSink;
+ SoftwareRenderer *mSoftRenderer;
sp<AMessage> mNotify;
+ uint32_t mFlags;
List<QueueEntry> mAudioQueue;
List<QueueEntry> mVideoQueue;
uint32_t mNumFramesWritten;
@@ -101,6 +111,8 @@ private:
bool mPaused;
bool mVideoRenderingStarted;
+ int32_t mVideoRenderingStartGeneration;
+ int32_t mAudioRenderingStartGeneration;
int64_t mLastPositionUpdateUs;
int64_t mVideoLateByUs;
@@ -111,6 +123,9 @@ private:
void onDrainVideoQueue();
void postDrainVideoQueue();
+ void prepareForMediaRenderingStart();
+ void notifyIfMediaRenderingStarted();
+
void onQueueBuffer(const sp<AMessage> &msg);
void onQueueEOS(const sp<AMessage> &msg);
void onFlush(const sp<AMessage> &msg);
diff --git a/media/libmediaplayerservice/nuplayer/NuPlayerSource.h b/media/libmediaplayerservice/nuplayer/NuPlayerSource.h
index 66aeff3..e50533a 100644
--- a/media/libmediaplayerservice/nuplayer/NuPlayerSource.h
+++ b/media/libmediaplayerservice/nuplayer/NuPlayerSource.h
@@ -20,15 +20,44 @@
#include "NuPlayer.h"
+#include <media/stagefright/foundation/AMessage.h>
+
namespace android {
struct ABuffer;
+struct MetaData;
+
+struct NuPlayer::Source : public AHandler {
+ enum Flags {
+ FLAG_CAN_PAUSE = 1,
+ FLAG_CAN_SEEK_BACKWARD = 2, // the "10 sec back button"
+ FLAG_CAN_SEEK_FORWARD = 4, // the "10 sec forward button"
+ FLAG_CAN_SEEK = 8, // the "seek bar"
+ FLAG_DYNAMIC_DURATION = 16,
+ };
+
+ enum {
+ kWhatPrepared,
+ kWhatFlagsChanged,
+ kWhatVideoSizeChanged,
+ kWhatBufferingStart,
+ kWhatBufferingEnd,
+ kWhatSubtitleData,
+ kWhatQueueDecoderShutdown,
+ };
+
+ // The provides message is used to notify the player about various
+ // events.
+ Source(const sp<AMessage> &notify)
+ : mNotify(notify) {
+ }
-struct NuPlayer::Source : public RefBase {
- Source() {}
+ virtual void prepareAsync() = 0;
virtual void start() = 0;
virtual void stop() {}
+ virtual void pause() {}
+ virtual void resume() {}
// Returns OK iff more data was available,
// an error or ERROR_END_OF_STREAM if not.
@@ -43,20 +72,38 @@ struct NuPlayer::Source : public RefBase {
return INVALID_OPERATION;
}
+ virtual status_t getTrackInfo(Parcel* reply) const {
+ return INVALID_OPERATION;
+ }
+
+ virtual status_t selectTrack(size_t trackIndex, bool select) {
+ return INVALID_OPERATION;
+ }
+
virtual status_t seekTo(int64_t seekTimeUs) {
return INVALID_OPERATION;
}
- virtual bool isSeekable() {
+ virtual bool isRealTime() const {
return false;
}
protected:
virtual ~Source() {}
+ virtual void onMessageReceived(const sp<AMessage> &msg);
+
virtual sp<MetaData> getFormatMeta(bool audio) { return NULL; }
+ sp<AMessage> dupNotify() const { return mNotify->dup(); }
+
+ void notifyFlagsChanged(uint32_t flags);
+ void notifyVideoSizeChanged(int32_t width, int32_t height);
+ void notifyPrepared(status_t err = OK);
+
private:
+ sp<AMessage> mNotify;
+
DISALLOW_EVIL_CONSTRUCTORS(Source);
};
diff --git a/media/libmediaplayerservice/nuplayer/RTSPSource.cpp b/media/libmediaplayerservice/nuplayer/RTSPSource.cpp
index 5a7a785..18cf6d1 100644
--- a/media/libmediaplayerservice/nuplayer/RTSPSource.cpp
+++ b/media/libmediaplayerservice/nuplayer/RTSPSource.cpp
@@ -22,26 +22,35 @@
#include "AnotherPacketSource.h"
#include "MyHandler.h"
+#include "SDPLoader.h"
#include <media/stagefright/MediaDefs.h>
#include <media/stagefright/MetaData.h>
namespace android {
+const int64_t kNearEOSTimeoutUs = 2000000ll; // 2 secs
+
NuPlayer::RTSPSource::RTSPSource(
+ const sp<AMessage> &notify,
const char *url,
const KeyedVector<String8, String8> *headers,
bool uidValid,
- uid_t uid)
- : mURL(url),
+ uid_t uid,
+ bool isSDP)
+ : Source(notify),
+ mURL(url),
mUIDValid(uidValid),
mUID(uid),
mFlags(0),
+ mIsSDP(isSDP),
mState(DISCONNECTED),
mFinalResult(OK),
mDisconnectReplyID(0),
- mStartingUp(true),
- mSeekGeneration(0) {
+ mBuffering(true),
+ mSeekGeneration(0),
+ mEOSTimeoutAudio(0),
+ mEOSTimeoutVideo(0) {
if (headers) {
mExtraHeaders = *headers;
@@ -62,7 +71,7 @@ NuPlayer::RTSPSource::~RTSPSource() {
}
}
-void NuPlayer::RTSPSource::start() {
+void NuPlayer::RTSPSource::prepareAsync() {
if (mLooper == NULL) {
mLooper = new ALooper;
mLooper->setName("rtsp");
@@ -73,25 +82,64 @@ void NuPlayer::RTSPSource::start() {
}
CHECK(mHandler == NULL);
+ CHECK(mSDPLoader == NULL);
sp<AMessage> notify = new AMessage(kWhatNotify, mReflector->id());
- mHandler = new MyHandler(mURL.c_str(), notify, mUIDValid, mUID);
- mLooper->registerHandler(mHandler);
-
CHECK_EQ(mState, (int)DISCONNECTED);
mState = CONNECTING;
- mHandler->connect();
+ if (mIsSDP) {
+ mSDPLoader = new SDPLoader(notify,
+ (mFlags & kFlagIncognito) ? SDPLoader::kFlagIncognito : 0,
+ mUIDValid, mUID);
+
+ mSDPLoader->load(
+ mURL.c_str(), mExtraHeaders.isEmpty() ? NULL : &mExtraHeaders);
+ } else {
+ mHandler = new MyHandler(mURL.c_str(), notify, mUIDValid, mUID);
+ mLooper->registerHandler(mHandler);
+
+ mHandler->connect();
+ }
+
+ sp<AMessage> notifyStart = dupNotify();
+ notifyStart->setInt32("what", kWhatBufferingStart);
+ notifyStart->post();
+}
+
+void NuPlayer::RTSPSource::start() {
}
void NuPlayer::RTSPSource::stop() {
+ if (mLooper == NULL) {
+ return;
+ }
sp<AMessage> msg = new AMessage(kWhatDisconnect, mReflector->id());
sp<AMessage> dummy;
msg->postAndAwaitResponse(&dummy);
}
+void NuPlayer::RTSPSource::pause() {
+ int64_t mediaDurationUs = 0;
+ getDuration(&mediaDurationUs);
+ for (size_t index = 0; index < mTracks.size(); index++) {
+ TrackInfo *info = &mTracks.editItemAt(index);
+ sp<AnotherPacketSource> source = info->mSource;
+
+ // Check if EOS or ERROR is received
+ if (source != NULL && source->isFinished(mediaDurationUs)) {
+ return;
+ }
+ }
+ mHandler->pause();
+}
+
+void NuPlayer::RTSPSource::resume() {
+ mHandler->resume();
+}
+
status_t NuPlayer::RTSPSource::feedMoreTSData() {
return mFinalResult;
}
@@ -112,6 +160,13 @@ bool NuPlayer::RTSPSource::haveSufficientDataOnAllTracks() {
static const int64_t kMinDurationUs = 2000000ll;
+ int64_t mediaDurationUs = 0;
+ getDuration(&mediaDurationUs);
+ if ((mAudioTrack != NULL && mAudioTrack->isFinished(mediaDurationUs))
+ || (mVideoTrack != NULL && mVideoTrack->isFinished(mediaDurationUs))) {
+ return true;
+ }
+
status_t err;
int64_t durationUs;
if (mAudioTrack != NULL
@@ -137,12 +192,16 @@ bool NuPlayer::RTSPSource::haveSufficientDataOnAllTracks() {
status_t NuPlayer::RTSPSource::dequeueAccessUnit(
bool audio, sp<ABuffer> *accessUnit) {
- if (mStartingUp) {
+ if (mBuffering) {
if (!haveSufficientDataOnAllTracks()) {
return -EWOULDBLOCK;
}
- mStartingUp = false;
+ mBuffering = false;
+
+ sp<AMessage> notify = dupNotify();
+ notify->setInt32("what", kWhatBufferingEnd);
+ notify->post();
}
sp<AnotherPacketSource> source = getSource(audio);
@@ -153,9 +212,51 @@ status_t NuPlayer::RTSPSource::dequeueAccessUnit(
status_t finalResult;
if (!source->hasBufferAvailable(&finalResult)) {
- return finalResult == OK ? -EWOULDBLOCK : finalResult;
+ if (finalResult == OK) {
+ int64_t mediaDurationUs = 0;
+ getDuration(&mediaDurationUs);
+ sp<AnotherPacketSource> otherSource = getSource(!audio);
+ status_t otherFinalResult;
+
+ // If other source already signaled EOS, this source should also signal EOS
+ if (otherSource != NULL &&
+ !otherSource->hasBufferAvailable(&otherFinalResult) &&
+ otherFinalResult == ERROR_END_OF_STREAM) {
+ source->signalEOS(ERROR_END_OF_STREAM);
+ return ERROR_END_OF_STREAM;
+ }
+
+ // If this source has detected near end, give it some time to retrieve more
+ // data before signaling EOS
+ if (source->isFinished(mediaDurationUs)) {
+ int64_t eosTimeout = audio ? mEOSTimeoutAudio : mEOSTimeoutVideo;
+ if (eosTimeout == 0) {
+ setEOSTimeout(audio, ALooper::GetNowUs());
+ } else if ((ALooper::GetNowUs() - eosTimeout) > kNearEOSTimeoutUs) {
+ setEOSTimeout(audio, 0);
+ source->signalEOS(ERROR_END_OF_STREAM);
+ return ERROR_END_OF_STREAM;
+ }
+ return -EWOULDBLOCK;
+ }
+
+ if (!(otherSource != NULL && otherSource->isFinished(mediaDurationUs))) {
+ // We should not enter buffering mode
+ // if any of the sources already have detected EOS.
+ mBuffering = true;
+
+ sp<AMessage> notify = dupNotify();
+ notify->setInt32("what", kWhatBufferingStart);
+ notify->post();
+ }
+
+ return -EWOULDBLOCK;
+ }
+ return finalResult;
}
+ setEOSTimeout(audio, 0);
+
return source->dequeueAccessUnit(accessUnit);
}
@@ -170,6 +271,14 @@ sp<AnotherPacketSource> NuPlayer::RTSPSource::getSource(bool audio) {
return audio ? mAudioTrack : mVideoTrack;
}
+void NuPlayer::RTSPSource::setEOSTimeout(bool audio, int64_t timeout) {
+ if (audio) {
+ mEOSTimeoutAudio = timeout;
+ } else {
+ mEOSTimeoutVideo = timeout;
+ }
+}
+
status_t NuPlayer::RTSPSource::getDuration(int64_t *durationUs) {
*durationUs = 0ll;
@@ -210,10 +319,6 @@ void NuPlayer::RTSPSource::performSeek(int64_t seekTimeUs) {
mHandler->seek(seekTimeUs);
}
-bool NuPlayer::RTSPSource::isSeekable() {
- return true;
-}
-
void NuPlayer::RTSPSource::onMessageReceived(const sp<AMessage> &msg) {
if (msg->what() == kWhatDisconnect) {
uint32_t replyID;
@@ -245,17 +350,34 @@ void NuPlayer::RTSPSource::onMessageReceived(const sp<AMessage> &msg) {
switch (what) {
case MyHandler::kWhatConnected:
+ {
onConnected();
+
+ notifyVideoSizeChanged(0, 0);
+
+ uint32_t flags = 0;
+
+ if (mHandler->isSeekable()) {
+ flags = FLAG_CAN_PAUSE
+ | FLAG_CAN_SEEK
+ | FLAG_CAN_SEEK_BACKWARD
+ | FLAG_CAN_SEEK_FORWARD;
+ }
+
+ notifyFlagsChanged(flags);
+ notifyPrepared();
break;
+ }
case MyHandler::kWhatDisconnected:
+ {
onDisconnected(msg);
break;
+ }
case MyHandler::kWhatSeekDone:
{
mState = CONNECTED;
- mStartingUp = true;
break;
}
@@ -405,6 +527,12 @@ void NuPlayer::RTSPSource::onMessageReceived(const sp<AMessage> &msg) {
break;
}
+ case SDPLoader::kWhatSDPLoaded:
+ {
+ onSDPLoaded(msg);
+ break;
+ }
+
default:
TRESPASS();
}
@@ -458,7 +586,57 @@ void NuPlayer::RTSPSource::onConnected() {
mState = CONNECTED;
}
+void NuPlayer::RTSPSource::onSDPLoaded(const sp<AMessage> &msg) {
+ status_t err;
+ CHECK(msg->findInt32("result", &err));
+
+ mSDPLoader.clear();
+
+ if (mDisconnectReplyID != 0) {
+ err = UNKNOWN_ERROR;
+ }
+
+ if (err == OK) {
+ sp<ASessionDescription> desc;
+ sp<RefBase> obj;
+ CHECK(msg->findObject("description", &obj));
+ desc = static_cast<ASessionDescription *>(obj.get());
+
+ AString rtspUri;
+ if (!desc->findAttribute(0, "a=control", &rtspUri)) {
+ ALOGE("Unable to find url in SDP");
+ err = UNKNOWN_ERROR;
+ } else {
+ sp<AMessage> notify = new AMessage(kWhatNotify, mReflector->id());
+
+ mHandler = new MyHandler(rtspUri.c_str(), notify, mUIDValid, mUID);
+ mLooper->registerHandler(mHandler);
+
+ mHandler->loadSDP(desc);
+ }
+ }
+
+ if (err != OK) {
+ if (mState == CONNECTING) {
+ // We're still in the preparation phase, signal that it
+ // failed.
+ notifyPrepared(err);
+ }
+
+ mState = DISCONNECTED;
+ mFinalResult = err;
+
+ if (mDisconnectReplyID != 0) {
+ finishDisconnectIfPossible();
+ }
+ }
+}
+
void NuPlayer::RTSPSource::onDisconnected(const sp<AMessage> &msg) {
+ if (mState == DISCONNECTED) {
+ return;
+ }
+
status_t err;
CHECK(msg->findInt32("result", &err));
CHECK_NE(err, (status_t)OK);
@@ -466,6 +644,12 @@ void NuPlayer::RTSPSource::onDisconnected(const sp<AMessage> &msg) {
mLooper->unregisterHandler(mHandler->id());
mHandler.clear();
+ if (mState == CONNECTING) {
+ // We're still in the preparation phase, signal that it
+ // failed.
+ notifyPrepared(err);
+ }
+
mState = DISCONNECTED;
mFinalResult = err;
@@ -476,7 +660,11 @@ void NuPlayer::RTSPSource::onDisconnected(const sp<AMessage> &msg) {
void NuPlayer::RTSPSource::finishDisconnectIfPossible() {
if (mState != DISCONNECTED) {
- mHandler->disconnect();
+ if (mHandler != NULL) {
+ mHandler->disconnect();
+ } else if (mSDPLoader != NULL) {
+ mSDPLoader->cancel();
+ }
return;
}
diff --git a/media/libmediaplayerservice/nuplayer/RTSPSource.h b/media/libmediaplayerservice/nuplayer/RTSPSource.h
index f07c724..8cf34a0 100644
--- a/media/libmediaplayerservice/nuplayer/RTSPSource.h
+++ b/media/libmediaplayerservice/nuplayer/RTSPSource.h
@@ -29,16 +29,22 @@ namespace android {
struct ALooper;
struct AnotherPacketSource;
struct MyHandler;
+struct SDPLoader;
struct NuPlayer::RTSPSource : public NuPlayer::Source {
RTSPSource(
+ const sp<AMessage> &notify,
const char *url,
const KeyedVector<String8, String8> *headers,
bool uidValid = false,
- uid_t uid = 0);
+ uid_t uid = 0,
+ bool isSDP = false);
+ virtual void prepareAsync();
virtual void start();
virtual void stop();
+ virtual void pause();
+ virtual void resume();
virtual status_t feedMoreTSData();
@@ -46,7 +52,6 @@ struct NuPlayer::RTSPSource : public NuPlayer::Source {
virtual status_t getDuration(int64_t *durationUs);
virtual status_t seekTo(int64_t seekTimeUs);
- virtual bool isSeekable();
void onMessageReceived(const sp<AMessage> &msg);
@@ -88,14 +93,16 @@ private:
bool mUIDValid;
uid_t mUID;
uint32_t mFlags;
+ bool mIsSDP;
State mState;
status_t mFinalResult;
uint32_t mDisconnectReplyID;
- bool mStartingUp;
+ bool mBuffering;
sp<ALooper> mLooper;
sp<AHandlerReflector<RTSPSource> > mReflector;
sp<MyHandler> mHandler;
+ sp<SDPLoader> mSDPLoader;
Vector<TrackInfo> mTracks;
sp<AnotherPacketSource> mAudioTrack;
@@ -105,9 +112,13 @@ private:
int32_t mSeekGeneration;
+ int64_t mEOSTimeoutAudio;
+ int64_t mEOSTimeoutVideo;
+
sp<AnotherPacketSource> getSource(bool audio);
void onConnected();
+ void onSDPLoaded(const sp<AMessage> &msg);
void onDisconnected(const sp<AMessage> &msg);
void finishDisconnectIfPossible();
@@ -115,6 +126,8 @@ private:
bool haveSufficientDataOnAllTracks();
+ void setEOSTimeout(bool audio, int64_t timeout);
+
DISALLOW_EVIL_CONSTRUCTORS(RTSPSource);
};
diff --git a/media/libmediaplayerservice/nuplayer/StreamingSource.cpp b/media/libmediaplayerservice/nuplayer/StreamingSource.cpp
index a1fd2ed..28f0d50 100644
--- a/media/libmediaplayerservice/nuplayer/StreamingSource.cpp
+++ b/media/libmediaplayerservice/nuplayer/StreamingSource.cpp
@@ -32,14 +32,23 @@
namespace android {
-NuPlayer::StreamingSource::StreamingSource(const sp<IStreamSource> &source)
- : mSource(source),
+NuPlayer::StreamingSource::StreamingSource(
+ const sp<AMessage> &notify,
+ const sp<IStreamSource> &source)
+ : Source(notify),
+ mSource(source),
mFinalResult(OK) {
}
NuPlayer::StreamingSource::~StreamingSource() {
}
+void NuPlayer::StreamingSource::prepareAsync() {
+ notifyVideoSizeChanged(0, 0);
+ notifyFlagsChanged(0);
+ notifyPrepared();
+}
+
void NuPlayer::StreamingSource::start() {
mStreamListener = new NuPlayerStreamListener(mSource, 0);
@@ -93,8 +102,22 @@ status_t NuPlayer::StreamingSource::feedMoreTSData() {
} else {
if (buffer[0] == 0x00) {
// XXX legacy
+
+ if (extra == NULL) {
+ extra = new AMessage;
+ }
+
+ uint8_t type = buffer[1];
+
+ if (type & 2) {
+ int64_t mediaTimeUs;
+ memcpy(&mediaTimeUs, &buffer[2], sizeof(mediaTimeUs));
+
+ extra->setInt64(IStreamListener::kKeyMediaTimeUs, mediaTimeUs);
+ }
+
mTSParser->signalDiscontinuity(
- buffer[1] == 0x00
+ ((type & 1) == 0)
? ATSParser::DISCONTINUITY_SEEK
: ATSParser::DISCONTINUITY_FORMATCHANGE,
extra);
@@ -159,5 +182,9 @@ status_t NuPlayer::StreamingSource::dequeueAccessUnit(
return err;
}
+bool NuPlayer::StreamingSource::isRealTime() const {
+ return mSource->flags() & IStreamSource::kFlagIsRealTimeData;
+}
+
} // namespace android
diff --git a/media/libmediaplayerservice/nuplayer/StreamingSource.h b/media/libmediaplayerservice/nuplayer/StreamingSource.h
index 3971e2a..412b6c4 100644
--- a/media/libmediaplayerservice/nuplayer/StreamingSource.h
+++ b/media/libmediaplayerservice/nuplayer/StreamingSource.h
@@ -27,14 +27,19 @@ struct ABuffer;
struct ATSParser;
struct NuPlayer::StreamingSource : public NuPlayer::Source {
- StreamingSource(const sp<IStreamSource> &source);
+ StreamingSource(
+ const sp<AMessage> &notify,
+ const sp<IStreamSource> &source);
+ virtual void prepareAsync();
virtual void start();
virtual status_t feedMoreTSData();
virtual status_t dequeueAccessUnit(bool audio, sp<ABuffer> *accessUnit);
+ virtual bool isRealTime() const;
+
protected:
virtual ~StreamingSource();
diff --git a/media/libmediaplayerservice/nuplayer/mp4/MP4Source.cpp b/media/libmediaplayerservice/nuplayer/mp4/MP4Source.cpp
index ffb3a65..2aae4dd 100644
--- a/media/libmediaplayerservice/nuplayer/mp4/MP4Source.cpp
+++ b/media/libmediaplayerservice/nuplayer/mp4/MP4Source.cpp
@@ -86,7 +86,7 @@ struct StreamSource : public FragmentedMP4Parser::Source {
total += n;
}
- ALOGV("read %ld bytes at offset %lld", n, mPosition);
+ ALOGV("read %ld bytes at offset %lld", total, mPosition);
mPosition += total;
@@ -104,8 +104,10 @@ private:
DISALLOW_EVIL_CONSTRUCTORS(StreamSource);
};
-MP4Source::MP4Source(const sp<IStreamSource> &source)
- : mSource(source),
+MP4Source::MP4Source(
+ const sp<AMessage> &notify, const sp<IStreamSource> &source)
+ : Source(notify),
+ mSource(source),
mLooper(new ALooper),
mParser(new FragmentedMP4Parser),
mEOS(false) {
@@ -115,6 +117,12 @@ MP4Source::MP4Source(const sp<IStreamSource> &source)
MP4Source::~MP4Source() {
}
+void MP4Source::prepareAsync() {
+ notifyVideoSizeChanged(0, 0);
+ notifyFlagsChanged(0);
+ notifyPrepared();
+}
+
void MP4Source::start() {
mLooper->start(false /* runOnCallingThread */);
mParser->start(new StreamSource(mSource));
diff --git a/media/libmediaplayerservice/nuplayer/mp4/MP4Source.h b/media/libmediaplayerservice/nuplayer/mp4/MP4Source.h
index 4e927af..a6ef622 100644
--- a/media/libmediaplayerservice/nuplayer/mp4/MP4Source.h
+++ b/media/libmediaplayerservice/nuplayer/mp4/MP4Source.h
@@ -24,8 +24,9 @@ namespace android {
struct FragmentedMP4Parser;
struct MP4Source : public NuPlayer::Source {
- MP4Source(const sp<IStreamSource> &source);
+ MP4Source(const sp<AMessage> &notify, const sp<IStreamSource> &source);
+ virtual void prepareAsync();
virtual void start();
virtual status_t feedMoreTSData();