Merge "libmedia: add sub resource type audio-codec and video-codec."
diff --git a/include/media/IResourceManagerClient.h b/include/media/IResourceManagerClient.h
index 3587aea..aa0cd88 100644
--- a/include/media/IResourceManagerClient.h
+++ b/include/media/IResourceManagerClient.h
@@ -18,6 +18,7 @@
#define ANDROID_IRESOURCEMANAGERCLIENT_H
#include <utils/RefBase.h>
+#include <utils/String8.h>
#include <binder/IInterface.h>
#include <binder/Parcel.h>
@@ -29,6 +30,7 @@
DECLARE_META_INTERFACE(ResourceManagerClient);
virtual bool reclaimResource() = 0;
+ virtual String8 getName() = 0;
};
// ----------------------------------------------------------------------------
diff --git a/include/media/stagefright/ACodec.h b/include/media/stagefright/ACodec.h
index a8d0fcb..c14e6c0 100644
--- a/include/media/stagefright/ACodec.h
+++ b/include/media/stagefright/ACodec.h
@@ -192,6 +192,7 @@
List<sp<AMessage> > mDeferredQueue;
bool mSentFormat;
+ bool mIsVideo;
bool mIsEncoder;
bool mUseMetadataOnEncoderOutput;
bool mShutdownInProgress;
diff --git a/include/media/stagefright/MediaSync.h b/include/media/stagefright/MediaSync.h
index 8bb8c7f..8ad74a4 100644
--- a/include/media/stagefright/MediaSync.h
+++ b/include/media/stagefright/MediaSync.h
@@ -113,6 +113,9 @@
// MediaClock::getMediaTime() and MediaClock::getRealTimeFor().
sp<const MediaClock> getMediaClock();
+ // Get the play time for pending audio frames in audio sink.
+ status_t getPlayTimeForPendingAudioFrames(int64_t *outTimeUs);
+
protected:
virtual void onMessageReceived(const sp<AMessage> &msg);
diff --git a/include/media/stagefright/MetaData.h b/include/media/stagefright/MetaData.h
index 8bdebf6..ca80123 100644
--- a/include/media/stagefright/MetaData.h
+++ b/include/media/stagefright/MetaData.h
@@ -176,6 +176,9 @@
kKeyTrackIsDefault = 'dflt', // bool (int32_t)
// Similar to MediaFormat.KEY_IS_FORCED_SUBTITLE but pertains to av tracks as well.
kKeyTrackIsForced = 'frcd', // bool (int32_t)
+
+ // H264 supplemental enhancement information offsets/sizes
+ kKeySEI = 'sei ', // raw data
};
enum {
diff --git a/media/libmedia/IResourceManagerClient.cpp b/media/libmedia/IResourceManagerClient.cpp
index 6fa56fc..b3f56e8 100644
--- a/media/libmedia/IResourceManagerClient.cpp
+++ b/media/libmedia/IResourceManagerClient.cpp
@@ -25,6 +25,7 @@
enum {
RECLAIM_RESOURCE = IBinder::FIRST_CALL_TRANSACTION,
+ GET_NAME,
};
class BpResourceManagerClient: public BpInterface<IResourceManagerClient>
@@ -46,6 +47,19 @@
}
return ret;
}
+
+ virtual String8 getName() {
+ Parcel data, reply;
+ data.writeInterfaceToken(IResourceManagerClient::getInterfaceDescriptor());
+
+ String8 ret;
+ status_t status = remote()->transact(GET_NAME, data, &reply);
+ if (status == NO_ERROR) {
+ ret = reply.readString8();
+ }
+ return ret;
+ }
+
};
IMPLEMENT_META_INTERFACE(ResourceManagerClient, "android.media.IResourceManagerClient");
@@ -62,6 +76,12 @@
reply->writeInt32(ret);
return NO_ERROR;
} break;
+ case GET_NAME: {
+ CHECK_INTERFACE(IResourceManagerClient, data, reply);
+ String8 ret = getName();
+ reply->writeString8(ret);
+ return NO_ERROR;
+ } break;
default:
return BBinder::onTransact(code, data, reply, flags);
}
diff --git a/media/libmediaplayerservice/Drm.cpp b/media/libmediaplayerservice/Drm.cpp
index 8ca8769..d55482d 100644
--- a/media/libmediaplayerservice/Drm.cpp
+++ b/media/libmediaplayerservice/Drm.cpp
@@ -52,6 +52,7 @@
KeyedVector<Vector<uint8_t>, String8> Drm::mUUIDToLibraryPathMap;
KeyedVector<String8, wp<SharedLibrary> > Drm::mLibraryPathToOpenLibraryMap;
Mutex Drm::mMapLock;
+Mutex Drm::mLock;
static bool operator<(const Vector<uint8_t> &lhs, const Vector<uint8_t> &rhs) {
if (lhs.size() < rhs.size()) {
diff --git a/media/libmediaplayerservice/Drm.h b/media/libmediaplayerservice/Drm.h
index c4013b8..056723c 100644
--- a/media/libmediaplayerservice/Drm.h
+++ b/media/libmediaplayerservice/Drm.h
@@ -143,7 +143,7 @@
virtual void binderDied(const wp<IBinder> &the_late_who);
private:
- mutable Mutex mLock;
+ static Mutex mLock;
status_t mInitCheck;
diff --git a/media/libmediaplayerservice/nuplayer/GenericSource.cpp b/media/libmediaplayerservice/nuplayer/GenericSource.cpp
index b7a88e7..7eaa0e0 100644
--- a/media/libmediaplayerservice/nuplayer/GenericSource.cpp
+++ b/media/libmediaplayerservice/nuplayer/GenericSource.cpp
@@ -1400,6 +1400,14 @@
meta->setInt32("trackIndex", mSubtitleTrack.mIndex);
}
+ uint32_t dataType; // unused
+ const void *seiData;
+ size_t seiLength;
+ if (mb->meta_data()->findData(kKeySEI, &dataType, &seiData, &seiLength)) {
+ sp<ABuffer> sei = ABuffer::CreateAsCopy(seiData, seiLength);;
+ meta->setBuffer("sei", sei);
+ }
+
if (actualTimeUs) {
*actualTimeUs = timeUs;
}
diff --git a/media/libmediaplayerservice/nuplayer/NuPlayer.cpp b/media/libmediaplayerservice/nuplayer/NuPlayer.cpp
index a028b01..b670d68 100644
--- a/media/libmediaplayerservice/nuplayer/NuPlayer.cpp
+++ b/media/libmediaplayerservice/nuplayer/NuPlayer.cpp
@@ -634,6 +634,17 @@
if (mRenderer != NULL) {
mRenderer->setPlaybackRate(mPlaybackRate);
}
+
+ if (mVideoDecoder != NULL) {
+ sp<MetaData> meta = getFileMeta();
+ int32_t rate;
+ if (meta != NULL && meta->findInt32(kKeyFrameRate, &rate) && rate > 0) {
+ sp<AMessage> params = new AMessage();
+ params->setFloat("operating-rate", rate * mPlaybackRate);
+ mVideoDecoder->setParameters(params);
+ }
+ }
+
break;
}
@@ -1249,6 +1260,8 @@
return -EWOULDBLOCK;
}
+ format->setInt32("priority", 0 /* realtime */);
+
if (!audio) {
AString mime;
CHECK(format->findString("mime", &mime));
@@ -1265,6 +1278,12 @@
if (mSourceFlags & Source::FLAG_PROTECTED) {
format->setInt32("protected", true);
}
+
+ sp<MetaData> meta = getFileMeta();
+ int32_t rate;
+ if (meta != NULL && meta->findInt32(kKeyFrameRate, &rate) && rate > 0) {
+ format->setFloat("operating-rate", rate * mPlaybackRate);
+ }
}
if (audio) {
diff --git a/media/libmediaplayerservice/nuplayer/NuPlayerDecoder.cpp b/media/libmediaplayerservice/nuplayer/NuPlayerDecoder.cpp
index 65e80c3..3c4d695 100644
--- a/media/libmediaplayerservice/nuplayer/NuPlayerDecoder.cpp
+++ b/media/libmediaplayerservice/nuplayer/NuPlayerDecoder.cpp
@@ -252,6 +252,14 @@
mResumePending = false;
}
+void NuPlayer::Decoder::onSetParameters(const sp<AMessage> ¶ms) {
+ if (mCodec == NULL) {
+ ALOGW("onSetParameters called before codec is created.");
+ return;
+ }
+ mCodec->setParameters(params);
+}
+
void NuPlayer::Decoder::onSetRenderer(const sp<Renderer> &renderer) {
bool hadNoRenderer = (mRenderer == NULL);
mRenderer = renderer;
@@ -658,7 +666,7 @@
#if 0
int64_t mediaTimeUs;
CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
- ALOGV("[%s] feeding input buffer at media time %" PRId64,
+ ALOGV("[%s] feeding input buffer at media time %.3f",
mIsAudio ? "audio" : "video",
mediaTimeUs / 1E6);
#endif
diff --git a/media/libmediaplayerservice/nuplayer/NuPlayerDecoder.h b/media/libmediaplayerservice/nuplayer/NuPlayerDecoder.h
index 9f0ef1b5..dd84620 100644
--- a/media/libmediaplayerservice/nuplayer/NuPlayerDecoder.h
+++ b/media/libmediaplayerservice/nuplayer/NuPlayerDecoder.h
@@ -40,6 +40,7 @@
virtual void onMessageReceived(const sp<AMessage> &msg);
virtual void onConfigure(const sp<AMessage> &format);
+ virtual void onSetParameters(const sp<AMessage> ¶ms);
virtual void onSetRenderer(const sp<Renderer> &renderer);
virtual void onGetInputBuffers(Vector<sp<ABuffer> > *dstBuffers);
virtual void onResume(bool notifyComplete);
diff --git a/media/libmediaplayerservice/nuplayer/NuPlayerDecoderBase.cpp b/media/libmediaplayerservice/nuplayer/NuPlayerDecoderBase.cpp
index 36b41ec..9d509bf 100644
--- a/media/libmediaplayerservice/nuplayer/NuPlayerDecoderBase.cpp
+++ b/media/libmediaplayerservice/nuplayer/NuPlayerDecoderBase.cpp
@@ -70,6 +70,12 @@
mDecoderLooper->registerHandler(this);
}
+void NuPlayer::DecoderBase::setParameters(const sp<AMessage> ¶ms) {
+ sp<AMessage> msg = new AMessage(kWhatSetParameters, this);
+ msg->setMessage("params", params);
+ msg->post();
+}
+
void NuPlayer::DecoderBase::setRenderer(const sp<Renderer> &renderer) {
sp<AMessage> msg = new AMessage(kWhatSetRenderer, this);
msg->setObject("renderer", renderer);
@@ -123,6 +129,14 @@
break;
}
+ case kWhatSetParameters:
+ {
+ sp<AMessage> params;
+ CHECK(msg->findMessage("params", ¶ms));
+ onSetParameters(params);
+ break;
+ }
+
case kWhatSetRenderer:
{
sp<RefBase> obj;
diff --git a/media/libmediaplayerservice/nuplayer/NuPlayerDecoderBase.h b/media/libmediaplayerservice/nuplayer/NuPlayerDecoderBase.h
index 262f5d5..b52e7f7 100644
--- a/media/libmediaplayerservice/nuplayer/NuPlayerDecoderBase.h
+++ b/media/libmediaplayerservice/nuplayer/NuPlayerDecoderBase.h
@@ -33,6 +33,7 @@
void configure(const sp<AMessage> &format);
void init();
+ void setParameters(const sp<AMessage> ¶ms);
void setRenderer(const sp<Renderer> &renderer);
@@ -62,6 +63,7 @@
virtual void onMessageReceived(const sp<AMessage> &msg);
virtual void onConfigure(const sp<AMessage> &format) = 0;
+ virtual void onSetParameters(const sp<AMessage> ¶ms) = 0;
virtual void onSetRenderer(const sp<Renderer> &renderer) = 0;
virtual void onGetInputBuffers(Vector<sp<ABuffer> > *dstBuffers) = 0;
virtual void onResume(bool notifyComplete) = 0;
@@ -78,6 +80,7 @@
private:
enum {
kWhatConfigure = 'conf',
+ kWhatSetParameters = 'setP',
kWhatSetRenderer = 'setR',
kWhatGetInputBuffers = 'gInB',
kWhatRequestInputBuffers = 'reqB',
diff --git a/media/libmediaplayerservice/nuplayer/NuPlayerDecoderPassThrough.cpp b/media/libmediaplayerservice/nuplayer/NuPlayerDecoderPassThrough.cpp
index fdb9039..d7b070e 100644
--- a/media/libmediaplayerservice/nuplayer/NuPlayerDecoderPassThrough.cpp
+++ b/media/libmediaplayerservice/nuplayer/NuPlayerDecoderPassThrough.cpp
@@ -88,6 +88,10 @@
}
}
+void NuPlayer::DecoderPassThrough::onSetParameters(const sp<AMessage> &/*params*/) {
+ ALOGW("onSetParameters() called unexpectedly");
+}
+
void NuPlayer::DecoderPassThrough::onSetRenderer(
const sp<Renderer> &renderer) {
// renderer can't be changed during offloading
diff --git a/media/libmediaplayerservice/nuplayer/NuPlayerDecoderPassThrough.h b/media/libmediaplayerservice/nuplayer/NuPlayerDecoderPassThrough.h
index b7dcb8d..2f6df2c 100644
--- a/media/libmediaplayerservice/nuplayer/NuPlayerDecoderPassThrough.h
+++ b/media/libmediaplayerservice/nuplayer/NuPlayerDecoderPassThrough.h
@@ -40,6 +40,7 @@
virtual void onMessageReceived(const sp<AMessage> &msg);
virtual void onConfigure(const sp<AMessage> &format);
+ virtual void onSetParameters(const sp<AMessage> ¶ms);
virtual void onSetRenderer(const sp<Renderer> &renderer);
virtual void onGetInputBuffers(Vector<sp<ABuffer> > *dstBuffers);
virtual void onResume(bool notifyComplete);
diff --git a/media/libstagefright/ACodec.cpp b/media/libstagefright/ACodec.cpp
index da22f11..c7df5a0 100644
--- a/media/libstagefright/ACodec.cpp
+++ b/media/libstagefright/ACodec.cpp
@@ -405,6 +405,7 @@
: mQuirks(0),
mNode(0),
mSentFormat(false),
+ mIsVideo(false),
mIsEncoder(false),
mUseMetadataOnEncoderOutput(false),
mShutdownInProgress(false),
@@ -1186,6 +1187,7 @@
mIsEncoder = encoder;
+
status_t err = setComponentRole(encoder /* isEncoder */, mime);
if (err != OK) {
@@ -1244,6 +1246,7 @@
// sps/pps to idr frames, since in metadata mode the bitstream is in an
// opaque handle, to which we don't have access.
int32_t video = !strncasecmp(mime, "video/", 6);
+ mIsVideo = video;
if (encoder && video) {
OMX_BOOL enable = (OMX_BOOL) (prependSPSPPS
&& msg->findInt32("store-metadata-in-buffers-output", &storeMeta)
@@ -5684,6 +5687,15 @@
}
}
+ float rate;
+ if (params->findFloat("operating-rate", &rate) && rate > 0) {
+ status_t err = setOperatingRate(rate, mIsVideo);
+ if (err != OK) {
+ ALOGE("Failed to set parameter 'operating-rate' (err %d)", err);
+ return err;
+ }
+ }
+
return OK;
}
diff --git a/media/libstagefright/MediaCodec.cpp b/media/libstagefright/MediaCodec.cpp
index 5326c66..aa0d2e6 100644
--- a/media/libstagefright/MediaCodec.cpp
+++ b/media/libstagefright/MediaCodec.cpp
@@ -79,6 +79,21 @@
return (err == OK);
}
+ virtual String8 getName() {
+ String8 ret;
+ sp<MediaCodec> codec = mMediaCodec.promote();
+ if (codec == NULL) {
+ // codec is already gone.
+ return ret;
+ }
+
+ AString name;
+ if (codec->getName(&name) == OK) {
+ ret.setTo(name.c_str());
+ }
+ return ret;
+ }
+
protected:
virtual ~ResourceManagerClient() {}
@@ -139,12 +154,7 @@
return NULL;
}
mDeathNotifier = new DeathNotifier();
- if (IInterface::asBinder(mBatteryStatService)->
- linkToDeath(mDeathNotifier) != OK) {
- mBatteryStatService.clear();
- mDeathNotifier.clear();
- return NULL;
- }
+ IInterface::asBinder(mBatteryStatService)->linkToDeath(mDeathNotifier);
// notify start now if media already started
if (mVideoRefCount > 0) {
mBatteryStatService->noteStartVideo(AID_MEDIA);
@@ -179,8 +189,9 @@
return;
}
- mVideoRefCount--;
sp<IBatteryStats> batteryService = getBatteryService_l();
+
+ mVideoRefCount--;
if (mVideoRefCount == 0 && batteryService != NULL) {
batteryService->noteStopVideo(AID_MEDIA);
}
@@ -202,8 +213,9 @@
return;
}
- mAudioRefCount--;
sp<IBatteryStats> batteryService = getBatteryService_l();
+
+ mAudioRefCount--;
if (mAudioRefCount == 0 && batteryService != NULL) {
batteryService->noteStopAudio(AID_MEDIA);
}
diff --git a/media/libstagefright/MediaSync.cpp b/media/libstagefright/MediaSync.cpp
index 9c37a3d..8030a36 100644
--- a/media/libstagefright/MediaSync.cpp
+++ b/media/libstagefright/MediaSync.cpp
@@ -179,6 +179,42 @@
return mMediaClock;
}
+status_t MediaSync::getPlayTimeForPendingAudioFrames(int64_t *outTimeUs) {
+ Mutex::Autolock lock(mMutex);
+ // User should check the playback rate if it doesn't want to receive a
+ // huge number for play time.
+ if (mPlaybackRate == 0.0f) {
+ *outTimeUs = INT64_MAX;
+ return OK;
+ }
+
+ uint32_t numFramesPlayed = 0;
+ if (mAudioTrack != NULL) {
+ status_t res = mAudioTrack->getPosition(&numFramesPlayed);
+ if (res != OK) {
+ return res;
+ }
+ }
+
+ int64_t numPendingFrames = mNumFramesWritten - numFramesPlayed;
+ if (numPendingFrames < 0) {
+ numPendingFrames = 0;
+ ALOGW("getPlayTimeForPendingAudioFrames: pending frame count is negative.");
+ }
+ double timeUs = numPendingFrames * 1000000.0
+ / (mNativeSampleRateInHz * (double)mPlaybackRate);
+ if (timeUs > (double)INT64_MAX) {
+ // Overflow.
+ *outTimeUs = INT64_MAX;
+ ALOGW("getPlayTimeForPendingAudioFrames: play time for pending audio frames "
+ "is too high, possibly due to super low playback rate(%f)", mPlaybackRate);
+ } else {
+ *outTimeUs = (int64_t)timeUs;
+ }
+
+ return OK;
+}
+
status_t MediaSync::updateQueuedAudioData(
size_t sizeInBytes, int64_t presentationTimeUs) {
if (sizeInBytes == 0) {
diff --git a/media/libstagefright/httplive/Android.mk b/media/libstagefright/httplive/Android.mk
index 2639deb..fc85835 100644
--- a/media/libstagefright/httplive/Android.mk
+++ b/media/libstagefright/httplive/Android.mk
@@ -3,6 +3,7 @@
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= \
+ HTTPDownloader.cpp \
LiveDataSource.cpp \
LiveSession.cpp \
M3UParser.cpp \
diff --git a/media/libstagefright/httplive/HTTPDownloader.cpp b/media/libstagefright/httplive/HTTPDownloader.cpp
new file mode 100644
index 0000000..3b44bae
--- /dev/null
+++ b/media/libstagefright/httplive/HTTPDownloader.cpp
@@ -0,0 +1,273 @@
+/*
+ * Copyright 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+//#define LOG_NDEBUG 0
+#define LOG_TAG "HTTPDownloader"
+#include <utils/Log.h>
+
+#include "HTTPDownloader.h"
+#include "M3UParser.h"
+
+#include <media/IMediaHTTPConnection.h>
+#include <media/IMediaHTTPService.h>
+#include <media/stagefright/foundation/ABuffer.h>
+#include <media/stagefright/foundation/ADebug.h>
+#include <media/stagefright/MediaHTTP.h>
+#include <media/stagefright/DataSource.h>
+#include <media/stagefright/FileSource.h>
+#include <openssl/aes.h>
+#include <openssl/md5.h>
+#include <utils/Mutex.h>
+
+namespace android {
+
+HTTPDownloader::HTTPDownloader(
+ const sp<IMediaHTTPService> &httpService,
+ const KeyedVector<String8, String8> &headers) :
+ mHTTPDataSource(new MediaHTTP(httpService->makeHTTPConnection())),
+ mExtraHeaders(headers),
+ mDisconnecting(false) {
+}
+
+void HTTPDownloader::reconnect() {
+ AutoMutex _l(mLock);
+ mDisconnecting = false;
+}
+
+void HTTPDownloader::disconnect() {
+ {
+ AutoMutex _l(mLock);
+ mDisconnecting = true;
+ }
+ mHTTPDataSource->disconnect();
+}
+
+bool HTTPDownloader::isDisconnecting() {
+ AutoMutex _l(mLock);
+ return mDisconnecting;
+}
+
+/*
+ * Illustration of parameters:
+ *
+ * 0 `range_offset`
+ * +------------+-------------------------------------------------------+--+--+
+ * | | | next block to fetch | | |
+ * | | `source` handle => `out` buffer | | | |
+ * | `url` file |<--------- buffer size --------->|<--- `block_size` -->| | |
+ * | |<----------- `range_length` / buffer capacity ----------->| |
+ * |<------------------------------ file_size ------------------------------->|
+ *
+ * Special parameter values:
+ * - range_length == -1 means entire file
+ * - block_size == 0 means entire range
+ *
+ */
+ssize_t HTTPDownloader::fetchBlock(
+ const char *url, sp<ABuffer> *out,
+ int64_t range_offset, int64_t range_length,
+ uint32_t block_size, /* download block size */
+ String8 *actualUrl,
+ bool reconnect /* force connect HTTP when resuing source */) {
+ if (isDisconnecting()) {
+ return ERROR_NOT_CONNECTED;
+ }
+
+ off64_t size;
+
+ if (reconnect) {
+ if (!strncasecmp(url, "file://", 7)) {
+ mDataSource = new FileSource(url + 7);
+ } else if (strncasecmp(url, "http://", 7)
+ && strncasecmp(url, "https://", 8)) {
+ return ERROR_UNSUPPORTED;
+ } else {
+ KeyedVector<String8, String8> headers = mExtraHeaders;
+ if (range_offset > 0 || range_length >= 0) {
+ headers.add(
+ String8("Range"),
+ String8(
+ AStringPrintf(
+ "bytes=%lld-%s",
+ range_offset,
+ range_length < 0
+ ? "" : AStringPrintf("%lld",
+ range_offset + range_length - 1).c_str()).c_str()));
+ }
+
+ status_t err = mHTTPDataSource->connect(url, &headers);
+
+ if (isDisconnecting()) {
+ return ERROR_NOT_CONNECTED;
+ }
+
+ if (err != OK) {
+ return err;
+ }
+
+ mDataSource = mHTTPDataSource;
+ }
+ }
+
+ status_t getSizeErr = mDataSource->getSize(&size);
+
+ if (isDisconnecting()) {
+ return ERROR_NOT_CONNECTED;
+ }
+
+ if (getSizeErr != OK) {
+ size = 65536;
+ }
+
+ sp<ABuffer> buffer = *out != NULL ? *out : new ABuffer(size);
+ if (*out == NULL) {
+ buffer->setRange(0, 0);
+ }
+
+ ssize_t bytesRead = 0;
+ // adjust range_length if only reading partial block
+ if (block_size > 0 && (range_length == -1 || (int64_t)(buffer->size() + block_size) < range_length)) {
+ range_length = buffer->size() + block_size;
+ }
+ for (;;) {
+ // Only resize when we don't know the size.
+ size_t bufferRemaining = buffer->capacity() - buffer->size();
+ if (bufferRemaining == 0 && getSizeErr != OK) {
+ size_t bufferIncrement = buffer->size() / 2;
+ if (bufferIncrement < 32768) {
+ bufferIncrement = 32768;
+ }
+ bufferRemaining = bufferIncrement;
+
+ ALOGV("increasing download buffer to %zu bytes",
+ buffer->size() + bufferRemaining);
+
+ sp<ABuffer> copy = new ABuffer(buffer->size() + bufferRemaining);
+ memcpy(copy->data(), buffer->data(), buffer->size());
+ copy->setRange(0, buffer->size());
+
+ buffer = copy;
+ }
+
+ size_t maxBytesToRead = bufferRemaining;
+ if (range_length >= 0) {
+ int64_t bytesLeftInRange = range_length - buffer->size();
+ if (bytesLeftInRange < (int64_t)maxBytesToRead) {
+ maxBytesToRead = bytesLeftInRange;
+
+ if (bytesLeftInRange == 0) {
+ break;
+ }
+ }
+ }
+
+ // The DataSource is responsible for informing us of error (n < 0) or eof (n == 0)
+ // to help us break out of the loop.
+ ssize_t n = mDataSource->readAt(
+ buffer->size(), buffer->data() + buffer->size(),
+ maxBytesToRead);
+
+ if (isDisconnecting()) {
+ return ERROR_NOT_CONNECTED;
+ }
+
+ if (n < 0) {
+ return n;
+ }
+
+ if (n == 0) {
+ break;
+ }
+
+ buffer->setRange(0, buffer->size() + (size_t)n);
+ bytesRead += n;
+ }
+
+ *out = buffer;
+ if (actualUrl != NULL) {
+ *actualUrl = mDataSource->getUri();
+ if (actualUrl->isEmpty()) {
+ *actualUrl = url;
+ }
+ }
+
+ return bytesRead;
+}
+
+ssize_t HTTPDownloader::fetchFile(
+ const char *url, sp<ABuffer> *out, String8 *actualUrl) {
+ ssize_t err = fetchBlock(url, out, 0, -1, 0, actualUrl, true /* reconnect */);
+
+ // close off the connection after use
+ mHTTPDataSource->disconnect();
+
+ return err;
+}
+
+sp<M3UParser> HTTPDownloader::fetchPlaylist(
+ const char *url, uint8_t *curPlaylistHash, bool *unchanged) {
+ ALOGV("fetchPlaylist '%s'", url);
+
+ *unchanged = false;
+
+ sp<ABuffer> buffer;
+ String8 actualUrl;
+ ssize_t err = fetchFile(url, &buffer, &actualUrl);
+
+ // close off the connection after use
+ mHTTPDataSource->disconnect();
+
+ if (err <= 0) {
+ return NULL;
+ }
+
+ // MD5 functionality is not available on the simulator, treat all
+ // playlists as changed.
+
+#if defined(HAVE_ANDROID_OS)
+ uint8_t hash[16];
+
+ MD5_CTX m;
+ MD5_Init(&m);
+ MD5_Update(&m, buffer->data(), buffer->size());
+
+ MD5_Final(hash, &m);
+
+ if (curPlaylistHash != NULL && !memcmp(hash, curPlaylistHash, 16)) {
+ // playlist unchanged
+ *unchanged = true;
+
+ return NULL;
+ }
+
+ if (curPlaylistHash != NULL) {
+ memcpy(curPlaylistHash, hash, sizeof(hash));
+ }
+#endif
+
+ sp<M3UParser> playlist =
+ new M3UParser(actualUrl.string(), buffer->data(), buffer->size());
+
+ if (playlist->initCheck() != OK) {
+ ALOGE("failed to parse .m3u8 playlist");
+
+ return NULL;
+ }
+
+ return playlist;
+}
+
+} // namespace android
diff --git a/media/libstagefright/httplive/HTTPDownloader.h b/media/libstagefright/httplive/HTTPDownloader.h
new file mode 100644
index 0000000..1db4a48
--- /dev/null
+++ b/media/libstagefright/httplive/HTTPDownloader.h
@@ -0,0 +1,86 @@
+/*
+ * Copyright 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef HTTP_DOWNLOADER_H_
+
+#define HTTP_DOWNLOADER_H_
+
+#include <media/stagefright/foundation/ADebug.h>
+#include <utils/KeyedVector.h>
+#include <utils/Mutex.h>
+#include <utils/RefBase.h>
+
+namespace android {
+
+struct ABuffer;
+class DataSource;
+struct HTTPBase;
+struct IMediaHTTPService;
+struct M3UParser;
+
+struct HTTPDownloader : public RefBase {
+ HTTPDownloader(
+ const sp<IMediaHTTPService> &httpService,
+ const KeyedVector<String8, String8> &headers);
+
+ void reconnect();
+ void disconnect();
+ bool isDisconnecting();
+ // If given a non-zero block_size (default 0), it is used to cap the number of
+ // bytes read in from the DataSource. If given a non-NULL buffer, new content
+ // is read into the end.
+ //
+ // The DataSource we read from is responsible for signaling error or EOF to help us
+ // break out of the read loop. The DataSource can be returned to the caller, so
+ // that the caller can reuse it for subsequent fetches (within the initially
+ // requested range).
+ //
+ // For reused HTTP sources, the caller must download a file sequentially without
+ // any overlaps or gaps to prevent reconnection.
+ ssize_t fetchBlock(
+ const char *url,
+ sp<ABuffer> *out,
+ int64_t range_offset, /* open file at range_offset */
+ int64_t range_length, /* open file for range_length (-1: entire file) */
+ uint32_t block_size, /* download block size (0: entire range) */
+ String8 *actualUrl, /* returns actual URL */
+ bool reconnect /* force connect http */
+ );
+
+ // simplified version to fetch a single file
+ ssize_t fetchFile(
+ const char *url,
+ sp<ABuffer> *out,
+ String8 *actualUrl = NULL);
+
+ // fetch a playlist file
+ sp<M3UParser> fetchPlaylist(
+ const char *url, uint8_t *curPlaylistHash, bool *unchanged);
+
+private:
+ sp<HTTPBase> mHTTPDataSource;
+ sp<DataSource> mDataSource;
+ KeyedVector<String8, String8> mExtraHeaders;
+
+ Mutex mLock;
+ bool mDisconnecting;
+
+ DISALLOW_EVIL_CONSTRUCTORS(HTTPDownloader);
+};
+
+} // namespace android
+
+#endif // HTTP_DOWNLOADER_H_
diff --git a/media/libstagefright/httplive/LiveSession.cpp b/media/libstagefright/httplive/LiveSession.cpp
index 9ac764c..d8c38e7 100644
--- a/media/libstagefright/httplive/LiveSession.cpp
+++ b/media/libstagefright/httplive/LiveSession.cpp
@@ -19,25 +19,18 @@
#include <utils/Log.h>
#include "LiveSession.h"
-
+#include "HTTPDownloader.h"
#include "M3UParser.h"
#include "PlaylistFetcher.h"
-#include "include/HTTPBase.h"
#include "mpeg2ts/AnotherPacketSource.h"
#include <cutils/properties.h>
-#include <media/IMediaHTTPConnection.h>
#include <media/IMediaHTTPService.h>
-#include <media/stagefright/foundation/hexdump.h>
#include <media/stagefright/foundation/ABuffer.h>
#include <media/stagefright/foundation/ADebug.h>
#include <media/stagefright/foundation/AMessage.h>
#include <media/stagefright/foundation/AUtils.h>
-#include <media/stagefright/DataSource.h>
-#include <media/stagefright/FileSource.h>
-#include <media/stagefright/MediaErrors.h>
-#include <media/stagefright/MediaHTTP.h>
#include <media/stagefright/MediaDefs.h>
#include <media/stagefright/MetaData.h>
#include <media/stagefright/Utils.h>
@@ -46,8 +39,6 @@
#include <ctype.h>
#include <inttypes.h>
-#include <openssl/aes.h>
-#include <openssl/md5.h>
namespace android {
@@ -257,7 +248,6 @@
mInPreparationPhase(true),
mPollBufferingGeneration(0),
mPrevBufferPercentage(-1),
- mHTTPDataSource(new MediaHTTP(mHTTPService->makeHTTPConnection())),
mCurBandwidthIndex(-1),
mOrigBandwidthIndex(-1),
mLastBandwidthBps(-1ll),
@@ -317,7 +307,7 @@
ssize_t streamIdx = typeToIndex(stream);
if (streamIdx < 0) {
- return INVALID_VALUE;
+ return BAD_VALUE;
}
const char *streamStr = getNameForStream(stream);
// Do not let client pull data if we don't have data packets yet.
@@ -465,8 +455,8 @@
return convertMetaDataToMessage(meta, format);
}
-sp<HTTPBase> LiveSession::getHTTPDataSource() {
- return new MediaHTTP(mHTTPService->makeHTTPConnection());
+sp<HTTPDownloader> LiveSession::getHTTPDownloader() {
+ return new HTTPDownloader(mHTTPService, mExtraHeaders);
}
void LiveSession::connectAsync(
@@ -838,6 +828,12 @@
break;
}
+ case PlaylistFetcher::kWhatPlaylistFetched:
+ {
+ onMasterPlaylistFetched(msg);
+ break;
+ }
+
case PlaylistFetcher::kWhatMetadataDetected:
{
if (!mHasMetadata) {
@@ -874,12 +870,6 @@
break;
}
- case kWhatFinishDisconnect2:
- {
- onFinishDisconnect2();
- break;
- }
-
case kWhatPollBuffering:
{
int32_t generation;
@@ -931,8 +921,10 @@
}
void LiveSession::onConnect(const sp<AMessage> &msg) {
- AString url;
- CHECK(msg->findString("url", &url));
+ CHECK(msg->findString("url", &mMasterURL));
+
+ // TODO currently we don't know if we are coming here from incognito mode
+ ALOGI("onConnect %s", uriDebugString(mMasterURL).c_str());
KeyedVector<String8, String8> *headers = NULL;
if (!msg->findPointer("headers", (void **)&headers)) {
@@ -944,21 +936,6 @@
headers = NULL;
}
- // TODO currently we don't know if we are coming here from incognito mode
- ALOGI("onConnect %s", uriDebugString(url).c_str());
-
- mMasterURL = url;
-
- bool dummy;
- mPlaylist = fetchPlaylist(url.c_str(), NULL /* curPlaylistHash */, &dummy);
-
- if (mPlaylist == NULL) {
- ALOGE("unable to fetch master playlist %s.", uriDebugString(url).c_str());
-
- postPrepared(ERROR_IO);
- return;
- }
-
// create looper for fetchers
if (mFetcherLooper == NULL) {
mFetcherLooper = new ALooper();
@@ -967,6 +944,31 @@
mFetcherLooper->start(false, false);
}
+ // create fetcher to fetch the master playlist
+ addFetcher(mMasterURL.c_str())->fetchPlaylistAsync();
+}
+
+void LiveSession::onMasterPlaylistFetched(const sp<AMessage> &msg) {
+ AString uri;
+ CHECK(msg->findString("uri", &uri));
+ ssize_t index = mFetcherInfos.indexOfKey(uri);
+ if (index < 0) {
+ ALOGW("fetcher for master playlist is gone.");
+ return;
+ }
+
+ // no longer useful, remove
+ mFetcherLooper->unregisterHandler(mFetcherInfos[index].mFetcher->id());
+ mFetcherInfos.removeItemsAt(index);
+
+ CHECK(msg->findObject("playlist", (sp<RefBase> *)&mPlaylist));
+ if (mPlaylist == NULL) {
+ ALOGE("unable to fetch master playlist %s.",
+ uriDebugString(mMasterURL).c_str());
+
+ postPrepared(ERROR_IO);
+ return;
+ }
// We trust the content provider to make a reasonable choice of preferred
// initial bandwidth by listing it first in the variant playlist.
// At startup we really don't have a good estimate on the available
@@ -1050,22 +1052,26 @@
// cancel buffer polling
cancelPollBuffering();
+ // TRICKY: don't wait for all fetcher to be stopped when disconnecting
+ //
+ // Some fetchers might be stuck in connect/getSize at this point. These
+ // operations will eventually timeout (as we have a timeout set in
+ // MediaHTTPConnection), but we don't want to block the main UI thread
+ // until then. Here we just need to make sure we clear all references
+ // to the fetchers, so that when they finally exit from the blocking
+ // operation, they can be destructed.
+ //
+ // There is one very tricky point though. For this scheme to work, the
+ // fecther must hold a reference to LiveSession, so that LiveSession is
+ // destroyed after fetcher. Otherwise LiveSession would get stuck in its
+ // own destructor when it waits for mFetcherLooper to stop, which still
+ // blocks main UI thread.
for (size_t i = 0; i < mFetcherInfos.size(); ++i) {
mFetcherInfos.valueAt(i).mFetcher->stopAsync();
+ mFetcherLooper->unregisterHandler(
+ mFetcherInfos.valueAt(i).mFetcher->id());
}
-
- sp<AMessage> msg = new AMessage(kWhatFinishDisconnect2, this);
-
- mContinuationCounter = mFetcherInfos.size();
- mContinuation = msg;
-
- if (mContinuationCounter == 0) {
- msg->post();
- }
-}
-
-void LiveSession::onFinishDisconnect2() {
- mContinuation.clear();
+ mFetcherInfos.clear();
mPacketSources.valueFor(STREAMTYPE_AUDIO)->signalEOS(ERROR_END_OF_STREAM);
mPacketSources.valueFor(STREAMTYPE_VIDEO)->signalEOS(ERROR_END_OF_STREAM);
@@ -1104,198 +1110,6 @@
return info.mFetcher;
}
-/*
- * Illustration of parameters:
- *
- * 0 `range_offset`
- * +------------+-------------------------------------------------------+--+--+
- * | | | next block to fetch | | |
- * | | `source` handle => `out` buffer | | | |
- * | `url` file |<--------- buffer size --------->|<--- `block_size` -->| | |
- * | |<----------- `range_length` / buffer capacity ----------->| |
- * |<------------------------------ file_size ------------------------------->|
- *
- * Special parameter values:
- * - range_length == -1 means entire file
- * - block_size == 0 means entire range
- *
- */
-ssize_t LiveSession::fetchFile(
- const char *url, sp<ABuffer> *out,
- int64_t range_offset, int64_t range_length,
- uint32_t block_size, /* download block size */
- sp<DataSource> *source, /* to return and reuse source */
- String8 *actualUrl,
- bool forceConnectHTTP /* force connect HTTP when resuing source */) {
- off64_t size;
- sp<DataSource> temp_source;
- if (source == NULL) {
- source = &temp_source;
- }
-
- if (*source == NULL || forceConnectHTTP) {
- if (!strncasecmp(url, "file://", 7)) {
- *source = new FileSource(url + 7);
- } else if (strncasecmp(url, "http://", 7)
- && strncasecmp(url, "https://", 8)) {
- return ERROR_UNSUPPORTED;
- } else {
- KeyedVector<String8, String8> headers = mExtraHeaders;
- if (range_offset > 0 || range_length >= 0) {
- headers.add(
- String8("Range"),
- String8(
- AStringPrintf(
- "bytes=%lld-%s",
- range_offset,
- range_length < 0
- ? "" : AStringPrintf("%lld",
- range_offset + range_length - 1).c_str()).c_str()));
- }
-
- HTTPBase* httpDataSource =
- (*source == NULL) ? mHTTPDataSource.get() : (HTTPBase*)source->get();
- status_t err = httpDataSource->connect(url, &headers);
-
- if (err != OK) {
- return err;
- }
-
- if (*source == NULL) {
- *source = mHTTPDataSource;
- }
- }
- }
-
- status_t getSizeErr = (*source)->getSize(&size);
- if (getSizeErr != OK) {
- size = 65536;
- }
-
- sp<ABuffer> buffer = *out != NULL ? *out : new ABuffer(size);
- if (*out == NULL) {
- buffer->setRange(0, 0);
- }
-
- ssize_t bytesRead = 0;
- // adjust range_length if only reading partial block
- if (block_size > 0 && (range_length == -1 || (int64_t)(buffer->size() + block_size) < range_length)) {
- range_length = buffer->size() + block_size;
- }
- for (;;) {
- // Only resize when we don't know the size.
- size_t bufferRemaining = buffer->capacity() - buffer->size();
- if (bufferRemaining == 0 && getSizeErr != OK) {
- size_t bufferIncrement = buffer->size() / 2;
- if (bufferIncrement < 32768) {
- bufferIncrement = 32768;
- }
- bufferRemaining = bufferIncrement;
-
- ALOGV("increasing download buffer to %zu bytes",
- buffer->size() + bufferRemaining);
-
- sp<ABuffer> copy = new ABuffer(buffer->size() + bufferRemaining);
- memcpy(copy->data(), buffer->data(), buffer->size());
- copy->setRange(0, buffer->size());
-
- buffer = copy;
- }
-
- size_t maxBytesToRead = bufferRemaining;
- if (range_length >= 0) {
- int64_t bytesLeftInRange = range_length - buffer->size();
- if (bytesLeftInRange < (int64_t)maxBytesToRead) {
- maxBytesToRead = bytesLeftInRange;
-
- if (bytesLeftInRange == 0) {
- break;
- }
- }
- }
-
- // The DataSource is responsible for informing us of error (n < 0) or eof (n == 0)
- // to help us break out of the loop.
- ssize_t n = (*source)->readAt(
- buffer->size(), buffer->data() + buffer->size(),
- maxBytesToRead);
-
- if (n < 0) {
- return n;
- }
-
- if (n == 0) {
- break;
- }
-
- buffer->setRange(0, buffer->size() + (size_t)n);
- bytesRead += n;
- }
-
- *out = buffer;
- if (actualUrl != NULL) {
- *actualUrl = (*source)->getUri();
- if (actualUrl->isEmpty()) {
- *actualUrl = url;
- }
- }
-
- return bytesRead;
-}
-
-sp<M3UParser> LiveSession::fetchPlaylist(
- const char *url, uint8_t *curPlaylistHash, bool *unchanged) {
- ALOGV("fetchPlaylist '%s'", url);
-
- *unchanged = false;
-
- sp<ABuffer> buffer;
- String8 actualUrl;
- ssize_t err = fetchFile(url, &buffer, 0, -1, 0, NULL, &actualUrl);
-
- // close off the connection after use
- mHTTPDataSource->disconnect();
-
- if (err <= 0) {
- return NULL;
- }
-
- // MD5 functionality is not available on the simulator, treat all
- // playlists as changed.
-
-#if defined(HAVE_ANDROID_OS)
- uint8_t hash[16];
-
- MD5_CTX m;
- MD5_Init(&m);
- MD5_Update(&m, buffer->data(), buffer->size());
-
- MD5_Final(hash, &m);
-
- if (curPlaylistHash != NULL && !memcmp(hash, curPlaylistHash, 16)) {
- // playlist unchanged
- *unchanged = true;
-
- return NULL;
- }
-
- if (curPlaylistHash != NULL) {
- memcpy(curPlaylistHash, hash, sizeof(hash));
- }
-#endif
-
- sp<M3UParser> playlist =
- new M3UParser(actualUrl.string(), buffer->data(), buffer->size());
-
- if (playlist->initCheck() != OK) {
- ALOGE("failed to parse .m3u8 playlist");
-
- return NULL;
- }
-
- return playlist;
-}
-
#if 0
static double uniformRand() {
return (double)rand() / RAND_MAX;
@@ -1690,9 +1504,11 @@
fetcher->stopAsync();
} else {
float threshold = -1.0f; // always finish fetching by default
+ bool disconnect = false;
if (timeUs >= 0ll) {
// seeking, no need to finish fetching
threshold = 0.0f;
+ disconnect = true;
} else if (delayRemoval) {
// adapting, abort if remaining of current segment is over threshold
threshold = getAbortThreshold(
@@ -1701,7 +1517,7 @@
ALOGV("pausing fetcher-%d, threshold=%.2f",
fetcher->getFetcherID(), threshold);
- fetcher->pauseAsync(threshold);
+ fetcher->pauseAsync(threshold, disconnect);
}
}
diff --git a/media/libstagefright/httplive/LiveSession.h b/media/libstagefright/httplive/LiveSession.h
index 4e7ccac..21be413 100644
--- a/media/libstagefright/httplive/LiveSession.h
+++ b/media/libstagefright/httplive/LiveSession.h
@@ -37,6 +37,7 @@
struct M3UParser;
struct PlaylistFetcher;
struct HLSTime;
+struct HTTPDownloader;
struct LiveSession : public AHandler {
enum Flags {
@@ -76,7 +77,7 @@
status_t getStreamFormat(StreamType stream, sp<AMessage> *format);
- sp<HTTPBase> getHTTPDataSource();
+ sp<HTTPDownloader> getHTTPDownloader();
void connectAsync(
const char *url,
@@ -127,7 +128,6 @@
kWhatChangeConfiguration = 'chC0',
kWhatChangeConfiguration2 = 'chC2',
kWhatChangeConfiguration3 = 'chC3',
- kWhatFinishDisconnect2 = 'fin2',
kWhatPollBuffering = 'poll',
};
@@ -191,7 +191,6 @@
int32_t mPollBufferingGeneration;
int32_t mPrevBufferPercentage;
- sp<HTTPBase> mHTTPDataSource;
KeyedVector<String8, String8> mExtraHeaders;
AString mMasterURL;
@@ -253,34 +252,8 @@
sp<PlaylistFetcher> addFetcher(const char *uri);
void onConnect(const sp<AMessage> &msg);
+ void onMasterPlaylistFetched(const sp<AMessage> &msg);
void onSeek(const sp<AMessage> &msg);
- void onFinishDisconnect2();
-
- // If given a non-zero block_size (default 0), it is used to cap the number of
- // bytes read in from the DataSource. If given a non-NULL buffer, new content
- // is read into the end.
- //
- // The DataSource we read from is responsible for signaling error or EOF to help us
- // break out of the read loop. The DataSource can be returned to the caller, so
- // that the caller can reuse it for subsequent fetches (within the initially
- // requested range).
- //
- // For reused HTTP sources, the caller must download a file sequentially without
- // any overlaps or gaps to prevent reconnection.
- ssize_t fetchFile(
- const char *url, sp<ABuffer> *out,
- /* request/open a file starting at range_offset for range_length bytes */
- int64_t range_offset = 0, int64_t range_length = -1,
- /* download block size */
- uint32_t block_size = 0,
- /* reuse DataSource if doing partial fetch */
- sp<DataSource> *source = NULL,
- String8 *actualUrl = NULL,
- /* force connect http even when resuing DataSource */
- bool forceConnectHTTP = false);
-
- sp<M3UParser> fetchPlaylist(
- const char *url, uint8_t *curPlaylistHash, bool *unchanged);
bool UriIsSameAsIndex( const AString &uri, int32_t index, bool newUri);
sp<AnotherPacketSource> getPacketSourceForStreamIndex(size_t trackIndex, bool newUri);
diff --git a/media/libstagefright/httplive/PlaylistFetcher.cpp b/media/libstagefright/httplive/PlaylistFetcher.cpp
index 8350c1b..53087b6 100644
--- a/media/libstagefright/httplive/PlaylistFetcher.cpp
+++ b/media/libstagefright/httplive/PlaylistFetcher.cpp
@@ -20,23 +20,16 @@
#include <utils/misc.h>
#include "PlaylistFetcher.h"
-
-#include "LiveDataSource.h"
+#include "HTTPDownloader.h"
#include "LiveSession.h"
#include "M3UParser.h"
-
#include "include/avc_utils.h"
-#include "include/HTTPBase.h"
#include "include/ID3.h"
#include "mpeg2ts/AnotherPacketSource.h"
-#include <media/IStreamSource.h>
#include <media/stagefright/foundation/ABitReader.h>
#include <media/stagefright/foundation/ABuffer.h>
#include <media/stagefright/foundation/ADebug.h>
-#include <media/stagefright/foundation/AUtils.h>
-#include <media/stagefright/foundation/hexdump.h>
-#include <media/stagefright/FileSource.h>
#include <media/stagefright/MediaDefs.h>
#include <media/stagefright/MetaData.h>
#include <media/stagefright/Utils.h>
@@ -44,7 +37,6 @@
#include <ctype.h>
#include <inttypes.h>
#include <openssl/aes.h>
-#include <openssl/md5.h>
#define FLOGV(fmt, ...) ALOGV("[fetcher-%d] " fmt, mFetcherID, ##__VA_ARGS__)
#define FSLOGV(stream, fmt, ...) ALOGV("[fetcher-%d] [%s] " fmt, mFetcherID, \
@@ -179,7 +171,7 @@
mDownloadState(new DownloadState()),
mHasMetadata(false) {
memset(mPlaylistHash, 0, sizeof(mPlaylistHash));
- mHTTPDataSource = mSession->getHTTPDataSource();
+ mHTTPDownloader = mSession->getHTTPDownloader();
}
PlaylistFetcher::~PlaylistFetcher() {
@@ -338,9 +330,11 @@
if (index >= 0) {
key = mAESKeyForURI.valueAt(index);
} else {
- ssize_t err = mSession->fetchFile(keyURI.c_str(), &key);
+ ssize_t err = mHTTPDownloader->fetchFile(keyURI.c_str(), &key);
- if (err < 0) {
+ if (err == ERROR_NOT_CONNECTED) {
+ return ERROR_NOT_CONNECTED;
+ } else if (err < 0) {
ALOGE("failed to fetch cipher key from '%s'.", keyURI.c_str());
return ERROR_IO;
} else if (key->size() != 16) {
@@ -448,12 +442,32 @@
++mMonitorQueueGeneration;
}
-void PlaylistFetcher::setStoppingThreshold(float thresholdRatio) {
- AutoMutex _l(mThresholdLock);
- if (mStreamTypeMask == LiveSession::STREAMTYPE_SUBTITLES) {
- return;
+void PlaylistFetcher::setStoppingThreshold(float thresholdRatio, bool disconnect) {
+ {
+ AutoMutex _l(mThresholdLock);
+ mThresholdRatio = thresholdRatio;
}
- mThresholdRatio = thresholdRatio;
+ if (disconnect) {
+ mHTTPDownloader->disconnect();
+ }
+}
+
+void PlaylistFetcher::resetStoppingThreshold(bool disconnect) {
+ {
+ AutoMutex _l(mThresholdLock);
+ mThresholdRatio = -1.0f;
+ }
+ if (disconnect) {
+ mHTTPDownloader->disconnect();
+ } else {
+ // allow reconnect
+ mHTTPDownloader->reconnect();
+ }
+}
+
+float PlaylistFetcher::getStoppingThreshold() {
+ AutoMutex _l(mThresholdLock);
+ return mThresholdRatio;
}
void PlaylistFetcher::startAsync(
@@ -497,15 +511,15 @@
msg->post();
}
-void PlaylistFetcher::pauseAsync(float thresholdRatio) {
- if (thresholdRatio >= 0.0f) {
- setStoppingThreshold(thresholdRatio);
- }
+void PlaylistFetcher::pauseAsync(
+ float thresholdRatio, bool disconnect) {
+ setStoppingThreshold(thresholdRatio, disconnect);
+
(new AMessage(kWhatPause, this))->post();
}
void PlaylistFetcher::stopAsync(bool clear) {
- setStoppingThreshold(0.0f);
+ setStoppingThreshold(0.0f, true /* disconncect */);
sp<AMessage> msg = new AMessage(kWhatStop, this);
msg->setInt32("clear", clear);
@@ -520,6 +534,10 @@
msg->post();
}
+void PlaylistFetcher::fetchPlaylistAsync() {
+ (new AMessage(kWhatFetchPlaylist, this))->post();
+}
+
void PlaylistFetcher::onMessageReceived(const sp<AMessage> &msg) {
switch (msg->what()) {
case kWhatStart:
@@ -557,6 +575,19 @@
break;
}
+ case kWhatFetchPlaylist:
+ {
+ bool unchanged;
+ sp<M3UParser> playlist = mHTTPDownloader->fetchPlaylist(
+ mURI.c_str(), NULL /* curPlaylistHash */, &unchanged);
+
+ sp<AMessage> notify = mNotify->dup();
+ notify->setInt32("what", kWhatPlaylistFetched);
+ notify->setObject("playlist", playlist);
+ notify->post();
+ break;
+ }
+
case kWhatMonitorQueue:
case kWhatDownloadNext:
{
@@ -676,7 +707,7 @@
cancelMonitorQueue();
mLastDiscontinuitySeq = mDiscontinuitySeq;
- setStoppingThreshold(-1.0f);
+ resetStoppingThreshold(false /* disconnect */);
}
void PlaylistFetcher::onStop(const sp<AMessage> &msg) {
@@ -691,14 +722,11 @@
}
}
- // close off the connection after use
- mHTTPDataSource->disconnect();
-
mDownloadState->resetState();
mPacketSources.clear();
mStreamTypeMask = 0;
- setStoppingThreshold(-1.0f);
+ resetStoppingThreshold(true /* disconnect */);
}
// Resume until we have reached the boundary timestamps listed in `msg`; when
@@ -815,7 +843,7 @@
status_t PlaylistFetcher::refreshPlaylist() {
if (delayUsToRefreshPlaylist() <= 0) {
bool unchanged;
- sp<M3UParser> playlist = mSession->fetchPlaylist(
+ sp<M3UParser> playlist = mHTTPDownloader->fetchPlaylist(
mURI.c_str(), mPlaylistHash, &unchanged);
if (playlist == NULL) {
@@ -864,18 +892,12 @@
}
// Calculate threshold to abort current download
- int64_t targetDurationUs = mPlaylist->getTargetDuration();
- int64_t thresholdUs = -1;
- {
- AutoMutex _l(mThresholdLock);
- thresholdUs = (mThresholdRatio < 0.0f) ?
- -1ll : mThresholdRatio * targetDurationUs;
- }
+ float thresholdRatio = getStoppingThreshold();
- if (thresholdUs < 0) {
+ if (thresholdRatio < 0.0f) {
// never abort
return false;
- } else if (thresholdUs == 0) {
+ } else if (thresholdRatio == 0.0f) {
// immediately abort
return true;
}
@@ -905,6 +927,9 @@
}
lastEnqueueUs -= mSegmentFirstPTS;
+ int64_t targetDurationUs = mPlaylist->getTargetDuration();
+ int64_t thresholdUs = thresholdRatio * targetDurationUs;
+
FLOGV("%spausing now, thresholdUs %lld, remaining %lld",
targetDurationUs - lastEnqueueUs > thresholdUs ? "" : "not ",
(long long)thresholdUs,
@@ -1101,7 +1126,9 @@
junk->setRange(0, 16);
status_t err = decryptBuffer(mSeqNumber - firstSeqNumberInPlaylist, junk,
true /* first */);
- if (err != OK) {
+ if (err == ERROR_NOT_CONNECTED) {
+ return false;
+ } else if (err != OK) {
notifyError(err);
return false;
}
@@ -1202,12 +1229,21 @@
bool shouldPause = false;
ssize_t bytesRead;
do {
- sp<DataSource> source = mHTTPDataSource;
-
int64_t startUs = ALooper::GetNowUs();
- bytesRead = mSession->fetchFile(
+ bytesRead = mHTTPDownloader->fetchBlock(
uri.c_str(), &buffer, range_offset, range_length, kDownloadBlockSize,
- &source, NULL, connectHTTP);
+ NULL /* actualURL */, connectHTTP);
+ int64_t delayUs = ALooper::GetNowUs() - startUs;
+
+ if (bytesRead == ERROR_NOT_CONNECTED) {
+ return;
+ }
+ if (bytesRead < 0) {
+ status_t err = bytesRead;
+ ALOGE("failed to fetch .ts segment at url '%s'", uri.c_str());
+ notifyError(err);
+ return;
+ }
// add sample for bandwidth estimation, excluding samples from subtitles (as
// its too small), or during startup/resumeUntil (when we could have more than
@@ -1216,9 +1252,7 @@
&& (mStreamTypeMask
& (LiveSession::STREAMTYPE_AUDIO
| LiveSession::STREAMTYPE_VIDEO))) {
- int64_t delayUs = ALooper::GetNowUs() - startUs;
mSession->addBandwidthMeasurement(bytesRead, delayUs);
-
if (delayUs > 2000000ll) {
FLOGV("bytesRead %zd took %.2f seconds - abnormal bandwidth dip",
bytesRead, (double)delayUs / 1.0e6);
@@ -1227,13 +1261,6 @@
connectHTTP = false;
- if (bytesRead < 0) {
- status_t err = bytesRead;
- ALOGE("failed to fetch .ts segment at url '%s'", uri.c_str());
- notifyError(err);
- return;
- }
-
CHECK(buffer != NULL);
size_t size = buffer->size();
diff --git a/media/libstagefright/httplive/PlaylistFetcher.h b/media/libstagefright/httplive/PlaylistFetcher.h
index 1f5e9b0..c8ca457 100644
--- a/media/libstagefright/httplive/PlaylistFetcher.h
+++ b/media/libstagefright/httplive/PlaylistFetcher.h
@@ -49,6 +49,7 @@
kWhatPreparationFailed,
kWhatStartedAt,
kWhatStopReached,
+ kWhatPlaylistFetched,
kWhatMetadataDetected,
};
@@ -61,8 +62,6 @@
int32_t getFetcherID() const;
- sp<DataSource> getDataSource();
-
void startAsync(
const sp<AnotherPacketSource> &audioSource,
const sp<AnotherPacketSource> &videoSource,
@@ -74,12 +73,14 @@
int32_t startDiscontinuitySeq = -1,
LiveSession::SeekMode seekMode = LiveSession::kSeekModeExactPosition);
- void pauseAsync(float thresholdRatio);
+ void pauseAsync(float thresholdRatio, bool disconnect);
void stopAsync(bool clear = true);
void resumeUntilAsync(const sp<AMessage> ¶ms);
+ void fetchPlaylistAsync();
+
uint32_t getStreamTypeMask() const {
return mStreamTypeMask;
}
@@ -100,6 +101,7 @@
kWhatMonitorQueue = 'moni',
kWhatResumeUntil = 'rsme',
kWhatDownloadNext = 'dlnx',
+ kWhatFetchPlaylist = 'flst'
};
struct DownloadState;
@@ -114,7 +116,7 @@
sp<AMessage> mNotify;
sp<AMessage> mStartTimeUsNotify;
- sp<HTTPBase> mHTTPDataSource;
+ sp<HTTPDownloader> mHTTPDownloader;
sp<LiveSession> mSession;
AString mURI;
@@ -197,7 +199,9 @@
void postMonitorQueue(int64_t delayUs = 0, int64_t minDelayUs = 0);
void cancelMonitorQueue();
- void setStoppingThreshold(float thresholdRatio);
+ void setStoppingThreshold(float thresholdRatio, bool disconnect);
+ void resetStoppingThreshold(bool disconnect);
+ float getStoppingThreshold();
bool shouldPauseDownload();
int64_t delayUsToRefreshPlaylist() const;
diff --git a/media/libstagefright/matroska/MatroskaExtractor.cpp b/media/libstagefright/matroska/MatroskaExtractor.cpp
index 0712bf0..ddca437 100644
--- a/media/libstagefright/matroska/MatroskaExtractor.cpp
+++ b/media/libstagefright/matroska/MatroskaExtractor.cpp
@@ -941,7 +941,10 @@
const mkvparser::VideoTrack *vtrack =
static_cast<const mkvparser::VideoTrack *>(track);
- if (!strcmp("V_MPEG4/ISO/AVC", codecID)) {
+ if (codecID == NULL) {
+ ALOGW("unknown codecID is not supported.");
+ continue;
+ } else if (!strcmp("V_MPEG4/ISO/AVC", codecID)) {
meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC);
meta->setData(kKeyAVCC, 0, codecPrivate, codecPrivateSize);
} else if (!strcmp("V_MPEG4/ISO/ASP", codecID)) {
diff --git a/media/libstagefright/mpeg2ts/AnotherPacketSource.cpp b/media/libstagefright/mpeg2ts/AnotherPacketSource.cpp
index f74b859..0878a1b 100644
--- a/media/libstagefright/mpeg2ts/AnotherPacketSource.cpp
+++ b/media/libstagefright/mpeg2ts/AnotherPacketSource.cpp
@@ -211,6 +211,11 @@
mediaBuffer->meta_data()->setInt32(kKeyIsSyncFrame, isSync);
}
+ sp<ABuffer> sei;
+ if (buffer->meta()->findBuffer("sei", &sei) && sei != NULL) {
+ mediaBuffer->meta_data()->setData(kKeySEI, 0, sei->data(), sei->size());
+ }
+
*out = mediaBuffer;
return OK;
}
diff --git a/services/audiopolicy/common/managerdefinitions/include/AudioPort.h b/services/audiopolicy/common/managerdefinitions/include/AudioPort.h
index 1c2c27e..82e2c43 100644
--- a/services/audiopolicy/common/managerdefinitions/include/AudioPort.h
+++ b/services/audiopolicy/common/managerdefinitions/include/AudioPort.h
@@ -75,10 +75,8 @@
audio_format_t pickFormat() const;
static const audio_format_t sPcmFormatCompareTable[];
- static int compareFormatsGoodToBad(
- const audio_format_t *format1, const audio_format_t *format2) {
- // compareFormats sorts from bad to good, we reverse it here
- return compareFormats(*format2, *format1);
+ static int compareFormats(const audio_format_t *format1, const audio_format_t *format2) {
+ return compareFormats(*format1, *format2);
}
static int compareFormats(audio_format_t format1, audio_format_t format2);
diff --git a/services/audiopolicy/common/managerdefinitions/src/AudioPort.cpp b/services/audiopolicy/common/managerdefinitions/src/AudioPort.cpp
index 647550c..64f883a 100644
--- a/services/audiopolicy/common/managerdefinitions/src/AudioPort.cpp
+++ b/services/audiopolicy/common/managerdefinitions/src/AudioPort.cpp
@@ -146,7 +146,7 @@
break;
}
}
- if (!hasFormat) { // never import a channel mask twice
+ if (!hasFormat) { // never import a format twice
mFormats.add(format);
}
}
@@ -216,7 +216,12 @@
}
str = strtok(NULL, "|");
}
- mFormats.sort(compareFormatsGoodToBad);
+ // we sort from worst to best, so that AUDIO_FORMAT_DEFAULT is always the first entry.
+ // TODO: compareFormats could be a lambda to convert between pointer-to-format to format:
+ // [](const audio_format_t *format1, const audio_format_t *format2) {
+ // return compareFormats(*format1, *format2);
+ // }
+ mFormats.sort(compareFormats);
}
void AudioPort::loadInChannels(char *name)
@@ -558,11 +563,13 @@
mType == AUDIO_PORT_TYPE_MIX && mRole == AUDIO_PORT_ROLE_SINK
&& audio_is_linear_pcm(format);
- for (size_t i = 0; i < mFormats.size(); ++i) {
+ // iterate from best format to worst format (reverse order)
+ for (ssize_t i = mFormats.size() - 1; i >= 0 ; --i) {
if (mFormats[i] == format ||
- (checkInexact && audio_is_linear_pcm(mFormats[i]))) {
- // for inexact checks we take the first linear pcm format since
- // mFormats is sorted from best PCM format to worst PCM format.
+ (checkInexact
+ && mFormats[i] != AUDIO_FORMAT_DEFAULT
+ && audio_is_linear_pcm(mFormats[i]))) {
+ // for inexact checks we take the first linear pcm format due to sorting.
if (updatedFormat != NULL) {
*updatedFormat = mFormats[i];
}
@@ -789,10 +796,15 @@
const char *formatStr = ConfigParsingUtils::enumToString(sFormatNameToEnumTable,
ARRAY_SIZE(sFormatNameToEnumTable),
mFormats[i]);
- if (i == 0 && strcmp(formatStr, "") == 0) {
+ const bool isEmptyStr = formatStr[0] == 0;
+ if (i == 0 && isEmptyStr) {
snprintf(buffer, SIZE, "Dynamic");
} else {
- snprintf(buffer, SIZE, "%s", formatStr);
+ if (isEmptyStr) {
+ snprintf(buffer, SIZE, "%#x", mFormats[i]);
+ } else {
+ snprintf(buffer, SIZE, "%s", formatStr);
+ }
}
result.append(buffer);
result.append(i == (mFormats.size() - 1) ? "" : ", ");
diff --git a/services/audiopolicy/managerdefault/AudioPolicyManager.cpp b/services/audiopolicy/managerdefault/AudioPolicyManager.cpp
index 01f2b61..ba3fcaf 100644
--- a/services/audiopolicy/managerdefault/AudioPolicyManager.cpp
+++ b/services/audiopolicy/managerdefault/AudioPolicyManager.cpp
@@ -1420,6 +1420,13 @@
}
if (inputDesc->mRefCount == 0) {
+ // if input maps to a dynamic policy with an activity listener, notify of state change
+ if ((inputDesc->mPolicyMix != NULL)
+ && ((inputDesc->mPolicyMix->mFlags & MIX_FLAG_NOTIFY_ACTIVITY) != 0)) {
+ mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mRegistrationId,
+ MIX_STATE_MIXING);
+ }
+
if (mInputs.activeInputsCount() == 0) {
SoundTrigger::setCaptureState(true);
}
@@ -1473,6 +1480,12 @@
inputDesc->mRefCount--;
if (inputDesc->mRefCount == 0) {
+ // if input maps to a dynamic policy with an activity listener, notify of state change
+ if ((inputDesc->mPolicyMix != NULL)
+ && ((inputDesc->mPolicyMix->mFlags & MIX_FLAG_NOTIFY_ACTIVITY) != 0)) {
+ mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mRegistrationId,
+ MIX_STATE_IDLE);
+ }
// automatically disable the remote submix output when input is stopped if not
// used by a policy mix of type MIX_TYPE_RECORDERS
diff --git a/services/mediaresourcemanager/ResourceManagerService.cpp b/services/mediaresourcemanager/ResourceManagerService.cpp
index 75a69ed..17aac4e 100644
--- a/services/mediaresourcemanager/ResourceManagerService.cpp
+++ b/services/mediaresourcemanager/ResourceManagerService.cpp
@@ -88,6 +88,51 @@
return infos.editItemAt(infos.size() - 1);
}
+status_t ResourceManagerService::dump(int fd, const Vector<String16>& args) {
+ Mutex::Autolock lock(mLock);
+
+ String8 result;
+ const size_t SIZE = 256;
+ char buffer[SIZE];
+
+ snprintf(buffer, SIZE, "ResourceManagerService: %p\n", this);
+ result.append(buffer);
+ result.append(" Policies:\n");
+ snprintf(buffer, SIZE, " SupportsMultipleSecureCodecs: %d\n", mSupportsMultipleSecureCodecs);
+ result.append(buffer);
+ snprintf(buffer, SIZE, " SupportsSecureWithNonSecureCodec: %d\n", mSupportsSecureWithNonSecureCodec);
+ result.append(buffer);
+
+ snprintf(buffer, SIZE, " Processes:\n");
+ result.append(buffer);
+ for (size_t i = 0; i < mMap.size(); ++i) {
+ snprintf(buffer, SIZE, " Pid: %d\n", mMap.keyAt(i));
+ result.append(buffer);
+
+ const ResourceInfos &infos = mMap.valueAt(i);
+ for (size_t j = 0; j < infos.size(); ++j) {
+ snprintf(buffer, SIZE, " Client:\n");
+ result.append(buffer);
+ snprintf(buffer, SIZE, " Id: %lld\n", (long long)infos[j].clientId);
+ result.append(buffer);
+
+ snprintf(buffer, SIZE, " Name: %s\n", infos[j].client->getName().string());
+ result.append(buffer);
+
+ Vector<MediaResource> resources = infos[j].resources;
+ snprintf(buffer, SIZE, " Resources:\n");
+ result.append(buffer);
+ for (size_t k = 0; k < resources.size(); ++k) {
+ snprintf(buffer, SIZE, " %s\n", resources[k].toString().string());
+ result.append(buffer);
+ }
+ }
+ }
+
+ write(fd, result.string(), result.size());
+ return OK;
+}
+
ResourceManagerService::ResourceManagerService()
: mProcessInfo(new ProcessInfo()),
mSupportsMultipleSecureCodecs(true),
diff --git a/services/mediaresourcemanager/ResourceManagerService.h b/services/mediaresourcemanager/ResourceManagerService.h
index 2ed9bf8..0c3d694 100644
--- a/services/mediaresourcemanager/ResourceManagerService.h
+++ b/services/mediaresourcemanager/ResourceManagerService.h
@@ -48,6 +48,8 @@
public:
static char const *getServiceName() { return "media.resource_manager"; }
+ virtual status_t dump(int fd, const Vector<String16>& args);
+
ResourceManagerService();
ResourceManagerService(sp<ProcessInfoInterface> processInfo);
diff --git a/services/mediaresourcemanager/test/ResourceManagerService_test.cpp b/services/mediaresourcemanager/test/ResourceManagerService_test.cpp
index 48d1395..bccc7fa 100644
--- a/services/mediaresourcemanager/test/ResourceManagerService_test.cpp
+++ b/services/mediaresourcemanager/test/ResourceManagerService_test.cpp
@@ -55,6 +55,10 @@
return true;
}
+ virtual String8 getName() {
+ return String8("test_client");
+ }
+
bool reclaimed() const {
return mReclaimed;
}
diff --git a/services/radio/RadioService.cpp b/services/radio/RadioService.cpp
index a6c2bdf..cd0f5f3 100644
--- a/services/radio/RadioService.cpp
+++ b/services/radio/RadioService.cpp
@@ -146,7 +146,7 @@
radio = module->addClient(client, config, withAudio);
if (radio == 0) {
- NO_INIT;
+ return NO_INIT;
}
return NO_ERROR;
}
@@ -500,13 +500,12 @@
if (audio) {
notifyDeviceConnection(true, "");
}
+ ALOGV("addClient() DONE moduleClient %p", moduleClient.get());
} else {
+ ALOGW("%s open_tuner failed with error %d", __FUNCTION__, ret);
moduleClient.clear();
}
-
- ALOGV("addClient() DONE moduleClient %p", moduleClient.get());
-
return moduleClient;
}