Merge "Add support for HLG HDR video thumbnails" into rvc-dev
diff --git a/apex/Android.bp b/apex/Android.bp
index 140a2e8..3098001 100644
--- a/apex/Android.bp
+++ b/apex/Android.bp
@@ -14,6 +14,7 @@
apex_defaults {
name: "com.android.media-defaults",
+ updatable: true,
java_libs: ["updatable-media"],
multilib: {
first: {
@@ -65,6 +66,7 @@
apex_defaults {
name: "com.android.media.swcodec-defaults",
+ updatable: true,
binaries: [
"mediaswcodec",
],
diff --git a/media/codec2/OWNERS b/media/codec2/OWNERS
new file mode 100644
index 0000000..46a9fca
--- /dev/null
+++ b/media/codec2/OWNERS
@@ -0,0 +1,5 @@
+set noparent
+wonsik@google.com
+lajos@google.com
+pawin@google.com
+taklee@google.com
diff --git a/media/codec2/components/vorbis/Android.bp b/media/codec2/components/vorbis/Android.bp
index a5f485d..bc1c380 100644
--- a/media/codec2/components/vorbis/Android.bp
+++ b/media/codec2/components/vorbis/Android.bp
@@ -7,5 +7,5 @@
srcs: ["C2SoftVorbisDec.cpp"],
- shared_libs: ["libvorbisidec"],
+ static_libs: ["libvorbisidec"],
}
diff --git a/media/codec2/core/OWNERS b/media/codec2/core/OWNERS
new file mode 100644
index 0000000..31ecca5
--- /dev/null
+++ b/media/codec2/core/OWNERS
@@ -0,0 +1,2 @@
+set noparent
+lajos@google.com
diff --git a/media/codec2/sfplugin/C2OMXNode.cpp b/media/codec2/sfplugin/C2OMXNode.cpp
index 78d221e..c7588e9 100644
--- a/media/codec2/sfplugin/C2OMXNode.cpp
+++ b/media/codec2/sfplugin/C2OMXNode.cpp
@@ -62,7 +62,7 @@
android::base::unique_fd &&fd0,
android::base::unique_fd &&fd1) {
Mutexed<Jobs>::Locked jobs(mJobs);
- auto it = jobs->queues.try_emplace(comp, comp, systemTime()).first;
+ auto it = jobs->queues.try_emplace(comp, comp).first;
it->second.workList.emplace_back(
std::move(work), fenceFd, std::move(fd0), std::move(fd1));
jobs->cond.broadcast();
@@ -79,7 +79,8 @@
for (auto it = jobs->queues.begin(); it != jobs->queues.end(); ) {
Queue &queue = it->second;
if (queue.workList.empty()
- || nowNs - queue.lastQueuedTimestampNs < kIntervalNs) {
+ || (queue.lastQueuedTimestampNs != 0 &&
+ nowNs - queue.lastQueuedTimestampNs < kIntervalNs)) {
++it;
continue;
}
@@ -104,6 +105,7 @@
sp<Fence> fence(new Fence(fenceFd));
fence->waitForever(LOG_TAG);
}
+ queue.lastQueuedTimestampNs = nowNs;
comp->queue(&items);
for (android::base::unique_fd &ufd : uniqueFds) {
(void)ufd.release();
@@ -143,8 +145,8 @@
android::base::unique_fd fd1;
};
struct Queue {
- Queue(const std::shared_ptr<Codec2Client::Component> &comp, nsecs_t timestamp)
- : component(comp), lastQueuedTimestampNs(timestamp) {}
+ Queue(const std::shared_ptr<Codec2Client::Component> &comp)
+ : component(comp), lastQueuedTimestampNs(0) {}
Queue(const Queue &) = delete;
Queue &operator =(const Queue &) = delete;
diff --git a/media/codec2/sfplugin/CCodec.cpp b/media/codec2/sfplugin/CCodec.cpp
index 0d093da..cc132de 100644
--- a/media/codec2/sfplugin/CCodec.cpp
+++ b/media/codec2/sfplugin/CCodec.cpp
@@ -1320,7 +1320,8 @@
Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
const std::unique_ptr<Config> &config = *configLocked;
inputFormat = config->mInputFormat;
- outputFormat = config->mOutputFormat;
+ // start triggers format dup
+ outputFormat = config->mOutputFormat = config->mOutputFormat->dup();
if (config->mInputSurface) {
err2 = config->mInputSurface->start();
}
diff --git a/media/codec2/sfplugin/CCodecBufferChannel.cpp b/media/codec2/sfplugin/CCodecBufferChannel.cpp
index b869bd9..e85e73a 100644
--- a/media/codec2/sfplugin/CCodecBufferChannel.cpp
+++ b/media/codec2/sfplugin/CCodecBufferChannel.cpp
@@ -1251,7 +1251,7 @@
} else {
output->buffers.reset(new LinearOutputBuffers(mName));
}
- output->buffers->setFormat(outputFormat->dup());
+ output->buffers->setFormat(outputFormat);
// Try to set output surface to created block pool if given.
diff --git a/media/extractors/aac/AACExtractor.cpp b/media/extractors/aac/AACExtractor.cpp
index 5d00d94..8f60f6b 100644
--- a/media/extractors/aac/AACExtractor.cpp
+++ b/media/extractors/aac/AACExtractor.cpp
@@ -181,6 +181,8 @@
}
AACExtractor::~AACExtractor() {
+ mOffsetVector.clear();
+ delete mDataSource;
if (mMeta != nullptr) {
AMediaFormat_delete(mMeta);
}
diff --git a/media/extractors/mp4/AC4Parser.cpp b/media/extractors/mp4/AC4Parser.cpp
index 13d60c8..25fc4cd 100644
--- a/media/extractors/mp4/AC4Parser.cpp
+++ b/media/extractors/mp4/AC4Parser.cpp
@@ -600,6 +600,10 @@
if (ac4_dsi_version == 1) {
uint64_t end = (mDSISize - mBitReader.numBitsLeft()) / 8;
uint64_t presentation_bytes = end - start;
+ if (pres_bytes < presentation_bytes) {
+ ALOGE("pres_bytes is smaller than presentation_bytes.");
+ return false;
+ }
uint64_t skip_bytes = pres_bytes - presentation_bytes;
ALOGV("skipping = %" PRIu64 " bytes", skip_bytes);
CHECK_BITS_LEFT(skip_bytes * 8);
diff --git a/media/libaudioclient/Android.bp b/media/libaudioclient/Android.bp
index 3638d2c..65afc8d 100644
--- a/media/libaudioclient/Android.bp
+++ b/media/libaudioclient/Android.bp
@@ -51,6 +51,7 @@
// AIDL files for audioclient interfaces
// The headers for these interfaces will be available to any modules that
// include libaudioclient, at the path "aidl/package/path/BnFoo.h"
+ ":libaudioclient_aidl_callback",
":libaudioclient_aidl_private",
":libaudioclient_aidl",
@@ -138,3 +139,12 @@
],
path: "aidl",
}
+
+// AIDL interface for audio track callback
+filegroup {
+ name: "libaudioclient_aidl_callback",
+ srcs: [
+ "aidl/android/media/IAudioTrackCallback.aidl",
+ ],
+ path: "aidl",
+}
diff --git a/media/libaudioclient/AudioTrack.cpp b/media/libaudioclient/AudioTrack.cpp
index d5690fb..2f95886 100644
--- a/media/libaudioclient/AudioTrack.cpp
+++ b/media/libaudioclient/AudioTrack.cpp
@@ -217,7 +217,8 @@
mPreviousSchedulingGroup(SP_DEFAULT),
mPausedPosition(0),
mSelectedDeviceId(AUDIO_PORT_HANDLE_NONE),
- mRoutedDeviceId(AUDIO_PORT_HANDLE_NONE)
+ mRoutedDeviceId(AUDIO_PORT_HANDLE_NONE),
+ mAudioTrackCallback(new AudioTrackCallback())
{
mAttributes.content_type = AUDIO_CONTENT_TYPE_UNKNOWN;
mAttributes.usage = AUDIO_USAGE_UNKNOWN;
@@ -248,7 +249,8 @@
mState(STATE_STOPPED),
mPreviousPriority(ANDROID_PRIORITY_NORMAL),
mPreviousSchedulingGroup(SP_DEFAULT),
- mPausedPosition(0)
+ mPausedPosition(0),
+ mAudioTrackCallback(new AudioTrackCallback())
{
mAttributes = AUDIO_ATTRIBUTES_INITIALIZER;
@@ -281,7 +283,8 @@
mPreviousPriority(ANDROID_PRIORITY_NORMAL),
mPreviousSchedulingGroup(SP_DEFAULT),
mPausedPosition(0),
- mSelectedDeviceId(AUDIO_PORT_HANDLE_NONE)
+ mSelectedDeviceId(AUDIO_PORT_HANDLE_NONE),
+ mAudioTrackCallback(new AudioTrackCallback())
{
mAttributes = AUDIO_ATTRIBUTES_INITIALIZER;
@@ -1527,6 +1530,7 @@
input.notificationFrameCount = mNotificationFramesReq;
input.selectedDeviceId = mSelectedDeviceId;
input.sessionId = mSessionId;
+ input.audioTrackCallback = mAudioTrackCallback;
IAudioFlinger::CreateTrackOutput output;
@@ -3376,4 +3380,23 @@
mPausedNs = ns;
}
+binder::Status AudioTrack::AudioTrackCallback::onCodecFormatChanged(
+ const std::vector<uint8_t>& audioMetadata)
+{
+ AutoMutex _l(mAudioTrackCbLock);
+ sp<media::IAudioTrackCallback> callback = mCallback.promote();
+ if (callback.get() != nullptr) {
+ callback->onCodecFormatChanged(audioMetadata);
+ } else {
+ mCallback.clear();
+ }
+ return binder::Status::ok();
+}
+
+void AudioTrack::AudioTrackCallback::setAudioTrackCallback(
+ const sp<media::IAudioTrackCallback> &callback) {
+ AutoMutex lock(mAudioTrackCbLock);
+ mCallback = callback;
+}
+
} // namespace android
diff --git a/media/libaudioclient/aidl/android/media/IAudioTrackCallback.aidl b/media/libaudioclient/aidl/android/media/IAudioTrackCallback.aidl
new file mode 100644
index 0000000..21553b5
--- /dev/null
+++ b/media/libaudioclient/aidl/android/media/IAudioTrackCallback.aidl
@@ -0,0 +1,24 @@
+/*
+ * Copyright (C) 2020 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.
+ */
+
+package android.media;
+
+/**
+ * @hide
+ */
+interface IAudioTrackCallback {
+ oneway void onCodecFormatChanged(in byte[] audioMetadata);
+}
diff --git a/media/libaudioclient/include/media/AudioTrack.h b/media/libaudioclient/include/media/AudioTrack.h
index 95cad0a..4adaaea 100644
--- a/media/libaudioclient/include/media/AudioTrack.h
+++ b/media/libaudioclient/include/media/AudioTrack.h
@@ -26,6 +26,9 @@
#include <media/Modulo.h>
#include <utils/threads.h>
+#include "android/media/BnAudioTrackCallback.h"
+#include "android/media/IAudioTrackCallback.h"
+
namespace android {
// ----------------------------------------------------------------------------
@@ -885,8 +888,6 @@
virtual void onAudioDeviceUpdate(audio_io_handle_t audioIo,
audio_port_handle_t deviceId);
-
-
/* Obtain the pending duration in milliseconds for playback of pure PCM
* (mixable without embedded timing) data remaining in AudioTrack.
*
@@ -933,6 +934,10 @@
*/
audio_port_handle_t getPortId() const { return mPortId; };
+ void setAudioTrackCallback(const sp<media::IAudioTrackCallback>& callback) {
+ mAudioTrackCallback->setAudioTrackCallback(callback);
+ }
+
protected:
/* copying audio tracks is not allowed */
AudioTrack(const AudioTrack& other);
@@ -1254,6 +1259,18 @@
};
MediaMetrics mMediaMetrics;
std::string mMetricsId; // GUARDED_BY(mLock), could change in createTrack_l().
+
+private:
+ class AudioTrackCallback : public media::BnAudioTrackCallback {
+ public:
+ binder::Status onCodecFormatChanged(const std::vector<uint8_t>& audioMetadata) override;
+
+ void setAudioTrackCallback(const sp<media::IAudioTrackCallback>& callback);
+ private:
+ Mutex mAudioTrackCbLock;
+ wp<media::IAudioTrackCallback> mCallback;
+ };
+ sp<AudioTrackCallback> mAudioTrackCallback;
};
}; // namespace android
diff --git a/media/libaudioclient/include/media/IAudioFlinger.h b/media/libaudioclient/include/media/IAudioFlinger.h
index 8658cfa..f4ce5d7 100644
--- a/media/libaudioclient/include/media/IAudioFlinger.h
+++ b/media/libaudioclient/include/media/IAudioFlinger.h
@@ -40,6 +40,7 @@
#include <vector>
#include "android/media/IAudioRecord.h"
+#include "android/media/IAudioTrackCallback.h"
namespace android {
@@ -83,6 +84,8 @@
}
notificationsPerBuffer = parcel->readInt32();
speed = parcel->readFloat();
+ audioTrackCallback = interface_cast<media::IAudioTrackCallback>(
+ parcel->readStrongBinder());
/* input/output arguments*/
(void)parcel->read(&flags, sizeof(audio_output_flags_t));
@@ -107,6 +110,7 @@
}
(void)parcel->writeInt32(notificationsPerBuffer);
(void)parcel->writeFloat(speed);
+ (void)parcel->writeStrongBinder(IInterface::asBinder(audioTrackCallback));
/* input/output arguments*/
(void)parcel->write(&flags, sizeof(audio_output_flags_t));
@@ -125,6 +129,7 @@
sp<IMemory> sharedBuffer;
uint32_t notificationsPerBuffer;
float speed;
+ sp<media::IAudioTrackCallback> audioTrackCallback;
/* input/output */
audio_output_flags_t flags;
diff --git a/media/libaudiohal/impl/StreamHalHidl.cpp b/media/libaudiohal/impl/StreamHalHidl.cpp
index c08dddb..2726e36 100644
--- a/media/libaudiohal/impl/StreamHalHidl.cpp
+++ b/media/libaudiohal/impl/StreamHalHidl.cpp
@@ -299,10 +299,17 @@
if (mCallback.unsafe_get()) {
processReturn("clearCallback", mStream->clearCallback());
}
+#if MAJOR_VERSION >= 6
+ if (mEventCallback.unsafe_get() != nullptr) {
+ processReturn("setEventCallback",
+ mStream->setEventCallback(nullptr));
+ }
+#endif
processReturn("close", mStream->close());
mStream.clear();
}
mCallback.clear();
+ mEventCallback.clear();
hardware::IPCThreadState::self()->flushCommands();
if (mEfGroup) {
EventFlag::deleteEventFlag(&mEfGroup);
@@ -614,6 +621,50 @@
}
#endif
+#if MAJOR_VERSION < 6
+status_t StreamOutHalHidl::setEventCallback(
+ const sp<StreamOutHalInterfaceEventCallback>& callback __unused) {
+ // Codec format callback is supported starting from audio HAL V6.0
+ return INVALID_OPERATION;
+}
+#else
+
+#include PATH(android/hardware/audio/FILE_VERSION/IStreamOutEventCallback.h)
+
+namespace {
+
+struct StreamOutEventCallback : public IStreamOutEventCallback {
+ StreamOutEventCallback(const wp<StreamOutHalHidl>& stream) : mStream(stream) {}
+
+ // IStreamOutEventCallback implementation
+ Return<void> onCodecFormatChanged(
+ const android::hardware::hidl_vec<uint8_t>& audioMetadata) override {
+ sp<StreamOutHalHidl> stream = mStream.promote();
+ if (stream != nullptr) {
+ std::basic_string<uint8_t> metadataBs(audioMetadata.begin(), audioMetadata.end());
+ stream->onCodecFormatChanged(metadataBs);
+ }
+ return Void();
+ }
+
+ private:
+ wp<StreamOutHalHidl> mStream;
+};
+
+} // namespace
+
+status_t StreamOutHalHidl::setEventCallback(
+ const sp<StreamOutHalInterfaceEventCallback>& callback) {
+ if (mStream == nullptr) return NO_INIT;
+ mEventCallback = callback;
+ status_t status = processReturn(
+ "setEventCallback",
+ mStream->setEventCallback(
+ callback.get() == nullptr ? nullptr : new StreamOutEventCallback(this)));
+ return status;
+}
+#endif
+
void StreamOutHalHidl::onWriteReady() {
sp<StreamOutHalInterfaceCallback> callback = mCallback.promote();
if (callback == 0) return;
@@ -635,6 +686,13 @@
callback->onError();
}
+void StreamOutHalHidl::onCodecFormatChanged(const std::basic_string<uint8_t>& metadataBs) {
+ sp<StreamOutHalInterfaceEventCallback> callback = mEventCallback.promote();
+ if (callback == nullptr) return;
+ ALOGV("asyncCodecFormatCallback %s", __func__);
+ callback->onCodecFormatChanged(metadataBs);
+}
+
StreamInHalHidl::StreamInHalHidl(const sp<IStreamIn>& stream)
: StreamHalHidl(stream.get()), mStream(stream), mReaderClient(0), mEfGroup(nullptr) {
diff --git a/media/libaudiohal/impl/StreamHalHidl.h b/media/libaudiohal/impl/StreamHalHidl.h
index f587889..88f8587 100644
--- a/media/libaudiohal/impl/StreamHalHidl.h
+++ b/media/libaudiohal/impl/StreamHalHidl.h
@@ -173,6 +173,11 @@
void onDrainReady();
void onError();
+ status_t setEventCallback(const sp<StreamOutHalInterfaceEventCallback>& callback) override;
+
+ // Methods used by StreamCodecFormatCallback (HIDL).
+ void onCodecFormatChanged(const std::basic_string<uint8_t>& metadataBs);
+
private:
friend class DeviceHalHidl;
typedef MessageQueue<WriteCommand, hardware::kSynchronizedReadWrite> CommandMQ;
@@ -180,6 +185,7 @@
typedef MessageQueue<WriteStatus, hardware::kSynchronizedReadWrite> StatusMQ;
wp<StreamOutHalInterfaceCallback> mCallback;
+ wp<StreamOutHalInterfaceEventCallback> mEventCallback;
sp<IStreamOut> mStream;
std::unique_ptr<CommandMQ> mCommandMQ;
std::unique_ptr<DataMQ> mDataMQ;
diff --git a/media/libaudiohal/impl/StreamHalLocal.cpp b/media/libaudiohal/impl/StreamHalLocal.cpp
index 4818fd8..69be303 100644
--- a/media/libaudiohal/impl/StreamHalLocal.cpp
+++ b/media/libaudiohal/impl/StreamHalLocal.cpp
@@ -275,6 +275,43 @@
return mStream->get_mmap_position(mStream, position);
}
+status_t StreamOutHalLocal::setEventCallback(
+ const sp<StreamOutHalInterfaceEventCallback>& callback) {
+ if (mStream->set_event_callback == nullptr) {
+ return INVALID_OPERATION;
+ }
+ stream_event_callback_t asyncCallback =
+ callback == nullptr ? nullptr : StreamOutHalLocal::asyncEventCallback;
+ status_t result = mStream->set_event_callback(mStream, asyncCallback, this);
+ if (result == OK) {
+ mEventCallback = callback;
+ }
+ return result;
+}
+
+// static
+int StreamOutHalLocal::asyncEventCallback(
+ stream_event_callback_type_t event, void *param, void *cookie) {
+ // We act as if we gave a wp<StreamOutHalLocal> to HAL. This way we should handle
+ // correctly the case when the callback is invoked while StreamOutHalLocal's destructor is
+ // already running, because the destructor is invoked after the refcount has been atomically
+ // decremented.
+ wp<StreamOutHalLocal> weakSelf(static_cast<StreamOutHalLocal*>(cookie));
+ sp<StreamOutHalLocal> self = weakSelf.promote();
+ if (self == nullptr) return 0;
+ sp<StreamOutHalInterfaceEventCallback> callback = self->mEventCallback.promote();
+ if (callback.get() == nullptr) return 0;
+ switch (event) {
+ case STREAM_EVENT_CBK_TYPE_CODEC_FORMAT_CHANGED:
+ callback->onCodecFormatChanged(std::basic_string<uint8_t>((uint8_t*)param));
+ break;
+ default:
+ ALOGW("%s unknown event %d", __func__, event);
+ break;
+ }
+ return 0;
+}
+
StreamInHalLocal::StreamInHalLocal(audio_stream_in_t *stream, sp<DeviceHalLocal> device)
: StreamHalLocal(&stream->common, device), mStream(stream) {
}
diff --git a/media/libaudiohal/impl/StreamHalLocal.h b/media/libaudiohal/impl/StreamHalLocal.h
index 34f2bd8..d17f9f3 100644
--- a/media/libaudiohal/impl/StreamHalLocal.h
+++ b/media/libaudiohal/impl/StreamHalLocal.h
@@ -156,9 +156,12 @@
// Called when the metadata of the stream's source has been changed.
status_t updateSourceMetadata(const SourceMetadata& sourceMetadata) override;
+ status_t setEventCallback(const sp<StreamOutHalInterfaceEventCallback>& callback) override;
+
private:
audio_stream_out_t *mStream;
wp<StreamOutHalInterfaceCallback> mCallback;
+ wp<StreamOutHalInterfaceEventCallback> mEventCallback;
friend class DeviceHalLocal;
@@ -168,6 +171,8 @@
virtual ~StreamOutHalLocal();
static int asyncCallback(stream_callback_event_t event, void *param, void *cookie);
+
+ static int asyncEventCallback(stream_event_callback_type_t event, void *param, void *cookie);
};
class StreamInHalLocal : public StreamInHalInterface, public StreamHalLocal {
diff --git a/media/libaudiohal/include/media/audiohal/StreamHalInterface.h b/media/libaudiohal/include/media/audiohal/StreamHalInterface.h
index 6c3b21c..e30cb72 100644
--- a/media/libaudiohal/include/media/audiohal/StreamHalInterface.h
+++ b/media/libaudiohal/include/media/audiohal/StreamHalInterface.h
@@ -101,6 +101,15 @@
virtual ~StreamOutHalInterfaceCallback() {}
};
+class StreamOutHalInterfaceEventCallback : public virtual RefBase {
+public:
+ virtual void onCodecFormatChanged(const std::basic_string<uint8_t>& metadataBs) = 0;
+
+protected:
+ StreamOutHalInterfaceEventCallback() {}
+ virtual ~StreamOutHalInterfaceEventCallback() {}
+};
+
class StreamOutHalInterface : public virtual StreamHalInterface {
public:
// Return the audio hardware driver estimated latency in milliseconds.
@@ -157,6 +166,8 @@
*/
virtual status_t updateSourceMetadata(const SourceMetadata& sourceMetadata) = 0;
+ virtual status_t setEventCallback(const sp<StreamOutHalInterfaceEventCallback>& callback) = 0;
+
protected:
virtual ~StreamOutHalInterface() {}
};
diff --git a/media/libmedia/aidl/android/media/IResourceManagerService.aidl b/media/libmedia/aidl/android/media/IResourceManagerService.aidl
index 3dd0859..1b2d522 100644
--- a/media/libmedia/aidl/android/media/IResourceManagerService.aidl
+++ b/media/libmedia/aidl/android/media/IResourceManagerService.aidl
@@ -94,4 +94,12 @@
* remove existing override on originalPid if newPid is -1.
*/
void overridePid(int originalPid, int newPid);
+
+ /**
+ * Mark a client for pending removal
+ *
+ * @param pid pid from which the client's resources will be removed.
+ * @param clientId clientId within the pid that will be removed.
+ */
+ void markClientForPendingRemoval(int pid, long clientId);
}
diff --git a/media/libmediaplayerservice/StagefrightRecorder.cpp b/media/libmediaplayerservice/StagefrightRecorder.cpp
index c10e6e0..cf7f423 100644
--- a/media/libmediaplayerservice/StagefrightRecorder.cpp
+++ b/media/libmediaplayerservice/StagefrightRecorder.cpp
@@ -2009,6 +2009,9 @@
(*meta)->setInt32(kKeyRotation, mRotationDegrees);
}
}
+ if (mOutputFormat == OUTPUT_FORMAT_MPEG_4 || mOutputFormat == OUTPUT_FORMAT_THREE_GPP) {
+ (*meta)->setInt32(kKeyEmptyTrackMalFormed, true);
+ }
}
status_t StagefrightRecorder::pause() {
diff --git a/media/libstagefright/MPEG4Writer.cpp b/media/libstagefright/MPEG4Writer.cpp
index dbbdd1b..cb6798e 100644
--- a/media/libstagefright/MPEG4Writer.cpp
+++ b/media/libstagefright/MPEG4Writer.cpp
@@ -1131,8 +1131,8 @@
err = writerErr;
}
- // Do not write out movie header on error.
- if (err != OK) {
+ // Do not write out movie header on error except malformed track.
+ if (err != OK && err != ERROR_MALFORMED) {
release();
return err;
}
@@ -2687,7 +2687,7 @@
status_t MPEG4Writer::Track::stop(bool stopSource) {
ALOGD("%s track stopping. %s source", getTrackType(), stopSource ? "Stop" : "Not Stop");
if (!mStarted) {
- ALOGE("Stop() called but track is not started");
+ ALOGE("Stop() called but track is not started or stopped");
return ERROR_END_OF_STREAM;
}
@@ -2711,6 +2711,7 @@
err = static_cast<status_t>(reinterpret_cast<uintptr_t>(dummy));
WARN_UNLESS(err == 0, "%s track stopped. Status :%d. %s source", getTrackType(), err,
stopSource ? "Stop" : "Not Stop");
+ mStarted = false;
return err;
}
@@ -3108,6 +3109,7 @@
int32_t nActualFrames = 0; // frames containing non-CSD data (non-0 length)
int32_t nZeroLengthFrames = 0;
int64_t lastTimestampUs = 0; // Previous sample time stamp
+ int64_t previousSampleTimestampWithoutFudgeUs = 0; // Timestamp received/without fudge for STTS
int64_t lastDurationUs = 0; // Between the previous two samples
int64_t currDurationTicks = 0; // Timescale based ticks
int64_t lastDurationTicks = 0; // Timescale based ticks
@@ -3120,6 +3122,8 @@
int64_t lastCttsOffsetTimeTicks = -1; // Timescale based ticks
int32_t cttsSampleCount = 0; // Sample count in the current ctts table entry
uint32_t lastSamplesPerChunk = 0;
+ int64_t lastSampleDurationUs = -1; // Duration calculated from EOS buffer and its timestamp
+ int64_t lastSampleDurationTicks = -1; // Timescale based ticks
if (mIsAudio) {
prctl(PR_SET_NAME, (unsigned long)"AudioTrackWriterThread", 0, 0, 0);
@@ -3139,14 +3143,28 @@
MediaBufferBase *buffer;
const char *trackName = getTrackType();
while (!mDone && (err = mSource->read(&buffer)) == OK) {
+ int32_t isEOS = false;
if (buffer->range_length() == 0) {
- buffer->release();
- buffer = NULL;
- ++nZeroLengthFrames;
- continue;
+ if (buffer->meta_data().findInt32(kKeyIsEndOfStream, &isEOS) && isEOS) {
+ int64_t eosSampleTimestampUs = -1;
+ CHECK(buffer->meta_data().findInt64(kKeyTime, &eosSampleTimestampUs));
+ ALOGV("eosSampleTimestampUs:%" PRId64, eosSampleTimestampUs);
+ lastSampleDurationUs = eosSampleTimestampUs - previousSampleTimestampWithoutFudgeUs
+ - previousPausedDurationUs;
+ CHECK(lastSampleDurationUs >= 0);
+ lastSampleDurationTicks = (lastSampleDurationUs * mTimeScale + 500000LL)/1000000LL;
+ buffer->release();
+ buffer = nullptr;
+ mSource->stop();
+ break;
+ } else {
+ buffer->release();
+ buffer = nullptr;
+ ++nZeroLengthFrames;
+ continue;
+ }
}
-
// If the codec specific data has not been received yet, delay pause.
// After the codec specific data is received, discard what we received
// when the track is to be paused.
@@ -3470,6 +3488,8 @@
break;
}
+ previousSampleTimestampWithoutFudgeUs = timestampUs;
+
// if the duration is different for this sample, see if it is close enough to the previous
// duration that we can fudge it and use the same value, to avoid filling the stts table
// with lots of near-identical entries.
@@ -3567,47 +3587,61 @@
}
}
if (isTrackMalFormed()) {
+ mIsMalformed = true;
dumpTimeStamps();
err = ERROR_MALFORMED;
}
mOwner->trackProgressStatus(mTrackId, -1, err);
- if (mIsHeic) {
- if (!mChunkSamples.empty()) {
- bufferChunk(0);
- ++nChunks;
- }
- } else {
- // Last chunk
- if (!hasMultipleTracks) {
- addOneStscTableEntry(1, mStszTableEntries->count());
- } else if (!mChunkSamples.empty()) {
- addOneStscTableEntry(++nChunks, mChunkSamples.size());
- bufferChunk(timestampUs);
- }
-
- // We don't really know how long the last frame lasts, since
- // there is no frame time after it, just repeat the previous
- // frame's duration.
- if (mStszTableEntries->count() == 1) {
- lastDurationUs = 0; // A single sample's duration
- lastDurationTicks = 0;
+ // Add final entries only for non-empty tracks.
+ if (mStszTableEntries->count() > 0) {
+ if (mIsHeic) {
+ if (!mChunkSamples.empty()) {
+ bufferChunk(0);
+ ++nChunks;
+ }
} else {
- ++sampleCount; // Count for the last sample
- }
+ // Last chunk
+ if (!hasMultipleTracks) {
+ addOneStscTableEntry(1, mStszTableEntries->count());
+ } else if (!mChunkSamples.empty()) {
+ addOneStscTableEntry(++nChunks, mChunkSamples.size());
+ bufferChunk(timestampUs);
+ }
- addOneSttsTableEntry(sampleCount, lastDurationTicks);
+ // We don't really know how long the last frame lasts, since
+ // there is no frame time after it, just repeat the previous
+ // frame's duration.
+ if (mStszTableEntries->count() == 1) {
+ if (lastSampleDurationUs >= 0) {
+ addOneSttsTableEntry(sampleCount, lastSampleDurationTicks);
+ } else {
+ lastDurationUs = 0; // A single sample's duration
+ lastDurationTicks = 0;
+ addOneSttsTableEntry(sampleCount, lastDurationTicks);
+ }
+ } else if (lastSampleDurationUs >= 0) {
+ addOneSttsTableEntry(sampleCount, lastDurationTicks);
+ addOneSttsTableEntry(1, lastSampleDurationTicks);
+ } else {
+ ++sampleCount; // Count for the last sample
+ addOneSttsTableEntry(sampleCount, lastDurationTicks);
+ }
- // The last ctts box entry may not have been written yet, and this
- // is to make sure that we write out the last ctts box entry.
- if (currCttsOffsetTimeTicks == lastCttsOffsetTimeTicks) {
- if (cttsSampleCount > 0) {
- addOneCttsTableEntry(cttsSampleCount, lastCttsOffsetTimeTicks);
+ // The last ctts box entry may not have been written yet, and this
+ // is to make sure that we write out the last ctts box entry.
+ if (currCttsOffsetTimeTicks == lastCttsOffsetTimeTicks) {
+ if (cttsSampleCount > 0) {
+ addOneCttsTableEntry(cttsSampleCount, lastCttsOffsetTimeTicks);
+ }
+ }
+ if (lastSampleDurationUs >= 0) {
+ mTrackDurationUs += lastSampleDurationUs;
+ } else {
+ mTrackDurationUs += lastDurationUs;
}
}
-
- mTrackDurationUs += lastDurationUs;
}
mReachedEOS = true;
@@ -3630,14 +3664,24 @@
return true;
}
- if (!mIsHeic && mStszTableEntries->count() == 0) { // no samples written
- ALOGE("The number of recorded samples is 0");
- return true;
- }
-
- if (mIsVideo && mStssTableEntries->count() == 0) { // no sync frames for video
- ALOGE("There are no sync frames for video track");
- return true;
+ int32_t emptyTrackMalformed = false;
+ if (mOwner->mStartMeta &&
+ mOwner->mStartMeta->findInt32(kKeyEmptyTrackMalFormed, &emptyTrackMalformed) &&
+ emptyTrackMalformed) {
+ if (!mIsHeic && mStszTableEntries->count() == 0) { // no samples written
+ ALOGE("The number of recorded samples is 0");
+ return true;
+ }
+ if (mIsVideo && mStssTableEntries->count() == 0) { // no sync frames for video
+ ALOGE("There are no sync frames for video track");
+ return true;
+ }
+ } else {
+ // No sync frames for video.
+ if (mIsVideo && mStszTableEntries->count() > 0 && mStssTableEntries->count() == 0) {
+ ALOGE("There are no sync frames for video track");
+ return true;
+ }
}
if (OK != checkCodecSpecificData()) { // no codec specific data
@@ -3886,25 +3930,28 @@
void MPEG4Writer::Track::writeStblBox() {
mOwner->beginBox("stbl");
- mOwner->beginBox("stsd");
- mOwner->writeInt32(0); // version=0, flags=0
- mOwner->writeInt32(1); // entry count
- if (mIsAudio) {
- writeAudioFourCCBox();
- } else if (mIsVideo) {
- writeVideoFourCCBox();
- } else {
- writeMetadataFourCCBox();
+ // Add subboxes only for non-empty tracks.
+ if (mStszTableEntries->count() > 0) {
+ mOwner->beginBox("stsd");
+ mOwner->writeInt32(0); // version=0, flags=0
+ mOwner->writeInt32(1); // entry count
+ if (mIsAudio) {
+ writeAudioFourCCBox();
+ } else if (mIsVideo) {
+ writeVideoFourCCBox();
+ } else {
+ writeMetadataFourCCBox();
+ }
+ mOwner->endBox(); // stsd
+ writeSttsBox();
+ if (mIsVideo) {
+ writeCttsBox();
+ writeStssBox();
+ }
+ writeStszBox();
+ writeStscBox();
+ writeCo64Box();
}
- mOwner->endBox(); // stsd
- writeSttsBox();
- if (mIsVideo) {
- writeCttsBox();
- writeStssBox();
- }
- writeStszBox();
- writeStscBox();
- writeCo64Box();
mOwner->endBox(); // stbl
}
diff --git a/media/libstagefright/MediaCodec.cpp b/media/libstagefright/MediaCodec.cpp
index b597583..3e39c6d 100644
--- a/media/libstagefright/MediaCodec.cpp
+++ b/media/libstagefright/MediaCodec.cpp
@@ -199,6 +199,7 @@
void addResource(const MediaResourceParcel &resource);
void removeResource(const MediaResourceParcel &resource);
void removeClient();
+ void markClientForPendingRemoval();
bool reclaimResource(const std::vector<MediaResourceParcel> &resources);
private:
@@ -280,6 +281,14 @@
mService->removeClient(mPid, getId(mClient));
}
+void MediaCodec::ResourceManagerServiceProxy::markClientForPendingRemoval() {
+ Mutex::Autolock _l(mLock);
+ if (mService == nullptr) {
+ return;
+ }
+ mService->markClientForPendingRemoval(mPid, getId(mClient));
+}
+
bool MediaCodec::ResourceManagerServiceProxy::reclaimResource(
const std::vector<MediaResourceParcel> &resources) {
Mutex::Autolock _l(mLock);
@@ -1432,7 +1441,13 @@
status_t MediaCodec::release() {
sp<AMessage> msg = new AMessage(kWhatRelease, this);
+ sp<AMessage> response;
+ return PostAndAwaitResponse(msg, &response);
+}
+status_t MediaCodec::releaseAsync() {
+ sp<AMessage> msg = new AMessage(kWhatRelease, this);
+ msg->setInt32("async", 1);
sp<AMessage> response;
return PostAndAwaitResponse(msg, &response);
}
@@ -2600,7 +2615,9 @@
mResourceManagerProxy->removeClient();
- (new AMessage)->postReply(mReplyID);
+ if (mReplyID != nullptr) {
+ (new AMessage)->postReply(mReplyID);
+ }
break;
}
@@ -2999,6 +3016,14 @@
pushBlankBuffersToNativeWindow(mSurface.get());
}
+ int32_t async = 0;
+ if (msg->findInt32("async", &async) && async) {
+ mResourceManagerProxy->markClientForPendingRemoval();
+ handleSetSurface(NULL);
+ (new AMessage)->postReply(mReplyID);
+ mReplyID = 0;
+ }
+
break;
}
diff --git a/media/libstagefright/MediaMuxer.cpp b/media/libstagefright/MediaMuxer.cpp
index 3808d7e..1cb45ac 100644
--- a/media/libstagefright/MediaMuxer.cpp
+++ b/media/libstagefright/MediaMuxer.cpp
@@ -157,7 +157,6 @@
status_t MediaMuxer::stop() {
Mutex::Autolock autoLock(mMuxerLock);
-
if (mState == STARTED || mState == ERROR) {
mState = STOPPED;
for (size_t i = 0; i < mTrackList.size(); i++) {
@@ -165,7 +164,10 @@
return INVALID_OPERATION;
}
}
+ // Unlock this mutex to allow notify to be called during stop process.
+ mMuxerLock.unlock();
status_t err = mWriter->stop();
+ mMuxerLock.lock();
if (err != OK || mError != OK) {
ALOGE("stop err: %d, mError:%d", err, mError);
}
@@ -217,6 +219,11 @@
sampleMetaData.setInt32(kKeyIsMuxerData, 1);
}
+ if (flags & MediaCodec::BUFFER_FLAG_EOS) {
+ sampleMetaData.setInt32(kKeyIsEndOfStream, 1);
+ ALOGV("BUFFER_FLAG_EOS");
+ }
+
sp<MediaAdapter> currentTrack = mTrackList[trackIndex];
// This pushBuffer will wait until the mediaBuffer is consumed.
return currentTrack->pushBuffer(mediaBuffer);
diff --git a/media/libstagefright/NuMediaExtractor.cpp b/media/libstagefright/NuMediaExtractor.cpp
index 9fe09fa..050d7c2 100644
--- a/media/libstagefright/NuMediaExtractor.cpp
+++ b/media/libstagefright/NuMediaExtractor.cpp
@@ -94,11 +94,16 @@
return ERROR_UNSUPPORTED;
}
+ status_t err = OK;
if (!mCasToken.empty()) {
- mImpl->setMediaCas(mCasToken);
+ err = mImpl->setMediaCas(mCasToken);
+ if (err != OK) {
+ ALOGE("%s: failed to setMediaCas (%d)", __FUNCTION__, err);
+ return err;
+ }
}
- status_t err = updateDurationAndBitrate();
+ err = updateDurationAndBitrate();
if (err == OK) {
mDataSource = dataSource;
}
@@ -131,7 +136,11 @@
}
if (!mCasToken.empty()) {
- mImpl->setMediaCas(mCasToken);
+ err = mImpl->setMediaCas(mCasToken);
+ if (err != OK) {
+ ALOGE("%s: failed to setMediaCas (%d)", __FUNCTION__, err);
+ return err;
+ }
}
err = updateDurationAndBitrate();
@@ -161,7 +170,11 @@
}
if (!mCasToken.empty()) {
- mImpl->setMediaCas(mCasToken);
+ err = mImpl->setMediaCas(mCasToken);
+ if (err != OK) {
+ ALOGE("%s: failed to setMediaCas (%d)", __FUNCTION__, err);
+ return err;
+ }
}
err = updateDurationAndBitrate();
@@ -195,8 +208,12 @@
mCasToken = casToken;
if (mImpl != NULL) {
- mImpl->setMediaCas(casToken);
- status_t err = updateDurationAndBitrate();
+ status_t err = mImpl->setMediaCas(casToken);
+ if (err != OK) {
+ ALOGE("%s: failed to setMediaCas (%d)", __FUNCTION__, err);
+ return err;
+ }
+ err = updateDurationAndBitrate();
if (err != OK) {
return err;
}
diff --git a/media/libstagefright/bqhelper/Android.bp b/media/libstagefright/bqhelper/Android.bp
index 731713f..37e842a 100644
--- a/media/libstagefright/bqhelper/Android.bp
+++ b/media/libstagefright/bqhelper/Android.bp
@@ -96,6 +96,7 @@
apex_available: [
"com.android.media.swcodec",
"test_com.android.media.swcodec",
+ "//apex_available:platform",
],
vendor_available: false,
static_libs: [
diff --git a/media/libstagefright/data/media_codecs_sw.xml b/media/libstagefright/data/media_codecs_sw.xml
index 67d3f1a..dd2eed3 100644
--- a/media/libstagefright/data/media_codecs_sw.xml
+++ b/media/libstagefright/data/media_codecs_sw.xml
@@ -48,13 +48,13 @@
</MediaCodec>
<MediaCodec name="c2.android.g711.alaw.decoder" type="audio/g711-alaw">
<Alias name="OMX.google.g711.alaw.decoder" />
- <Limit name="channel-count" max="1" />
+ <Limit name="channel-count" max="6" />
<Limit name="sample-rate" ranges="8000-48000" />
<Limit name="bitrate" range="64000" />
</MediaCodec>
<MediaCodec name="c2.android.g711.mlaw.decoder" type="audio/g711-mlaw">
<Alias name="OMX.google.g711.mlaw.decoder" />
- <Limit name="channel-count" max="1" />
+ <Limit name="channel-count" max="6" />
<Limit name="sample-rate" ranges="8000-48000" />
<Limit name="bitrate" range="64000" />
</MediaCodec>
@@ -67,7 +67,7 @@
<MediaCodec name="c2.android.opus.decoder" type="audio/opus">
<Alias name="OMX.google.opus.decoder" />
<Limit name="channel-count" max="8" />
- <Limit name="sample-rate" ranges="48000" />
+ <Limit name="sample-rate" ranges="8000,12000,16000,24000,48000" />
<Limit name="bitrate" range="6000-510000" />
</MediaCodec>
<MediaCodec name="c2.android.raw.decoder" type="audio/raw">
diff --git a/media/libstagefright/include/media/stagefright/MediaCodec.h b/media/libstagefright/include/media/stagefright/MediaCodec.h
index 022c48e..0e983b2 100644
--- a/media/libstagefright/include/media/stagefright/MediaCodec.h
+++ b/media/libstagefright/include/media/stagefright/MediaCodec.h
@@ -139,6 +139,8 @@
// object.
status_t release();
+ status_t releaseAsync();
+
status_t flush();
status_t queueInputBuffer(
diff --git a/media/libstagefright/include/media/stagefright/MetaDataBase.h b/media/libstagefright/include/media/stagefright/MetaDataBase.h
index b9cd18a..3b701f6 100644
--- a/media/libstagefright/include/media/stagefright/MetaDataBase.h
+++ b/media/libstagefright/include/media/stagefright/MetaDataBase.h
@@ -70,6 +70,7 @@
kKeyIsSyncFrame = 'sync', // int32_t (bool)
kKeyIsCodecConfig = 'conf', // int32_t (bool)
kKeyIsMuxerData = 'muxd', // int32_t (bool)
+ kKeyIsEndOfStream = 'feos', // int32_t (bool)
kKeyTime = 'time', // int64_t (usecs)
kKeyDecodingTime = 'decT', // int64_t (decoding timestamp in usecs)
kKeyNTPTime = 'ntpT', // uint64_t (ntp-timestamp)
@@ -237,6 +238,9 @@
kKeyOpaqueCSD2 = 'csd2',
kKeyHapticChannelCount = 'hapC',
+
+ // Treat empty track as malformed for MediaRecorder.
+ kKeyEmptyTrackMalFormed = 'nemt', // bool (int32_t)
};
enum {
diff --git a/media/libstagefright/rtsp/APacketSource.cpp b/media/libstagefright/rtsp/APacketSource.cpp
index 1aa8a20..574bd7a 100644
--- a/media/libstagefright/rtsp/APacketSource.cpp
+++ b/media/libstagefright/rtsp/APacketSource.cpp
@@ -112,7 +112,9 @@
sp<ABuffer> profileLevelID = NULL;
if (GetAttribute(params, "profile-level-id", &val)) {
profileLevelID = decodeHex(val);
- CHECK_EQ(profileLevelID->size(), 3u);
+ if (profileLevelID != NULL && profileLevelID->size() != 3u) {
+ profileLevelID = NULL;
+ }
}
Vector<sp<ABuffer> > paramSets;
diff --git a/media/libstagefright/tests/writer/WriterTest.cpp b/media/libstagefright/tests/writer/WriterTest.cpp
index 409f141..ff063e3 100644
--- a/media/libstagefright/tests/writer/WriterTest.cpp
+++ b/media/libstagefright/tests/writer/WriterTest.cpp
@@ -243,6 +243,10 @@
sp<MetaData> trackMeta = new MetaData;
convertMessageToMetaData(format, trackMeta);
mCurrentTrack = new MediaAdapter(trackMeta);
+ if (mCurrentTrack == nullptr) {
+ ALOGE("MediaAdapter returned nullptr");
+ return -1;
+ }
status_t result = mWriter->addSource(mCurrentTrack);
return result;
}
@@ -301,7 +305,7 @@
getFileDetails(inputFile, inputInfo, param, isAudio, inputFileIdx);
ASSERT_NE(inputFile.compare(gEnv->getRes()), 0) << "No input file specified";
- getInputBufferInfo(inputFile, inputInfo);
+ ASSERT_NO_FATAL_FAILURE(getInputBufferInfo(inputFile, inputInfo));
status = addWriterSource(isAudio, param);
ASSERT_EQ((status_t)OK, status) << "Failed to add source for " << writerFormat << "Writer";
@@ -338,7 +342,7 @@
getFileDetails(inputFile, inputInfo, param, isAudio, inputFileIdx);
ASSERT_NE(inputFile.compare(gEnv->getRes()), 0) << "No input file specified";
- getInputBufferInfo(inputFile, inputInfo);
+ ASSERT_NO_FATAL_FAILURE(getInputBufferInfo(inputFile, inputInfo));
status = addWriterSource(isAudio, param);
ASSERT_EQ((status_t)OK, status) << "Failed to add source for " << writerFormat << "Writer";
@@ -397,7 +401,7 @@
getFileDetails(inputFile, inputInfo, param, isAudio, inputFileIdx);
ASSERT_NE(inputFile.compare(gEnv->getRes()), 0) << "No input file specified";
- getInputBufferInfo(inputFile, inputInfo);
+ ASSERT_NO_FATAL_FAILURE(getInputBufferInfo(inputFile, inputInfo));
status = addWriterSource(isAudio, param);
ASSERT_EQ((status_t)OK, status) << "Failed to add source for " << writerFormat << "Writer";
diff --git a/media/libstagefright/webm/WebmFrameThread.cpp b/media/libstagefright/webm/WebmFrameThread.cpp
index 631a2ab..59ce8db 100644
--- a/media/libstagefright/webm/WebmFrameThread.cpp
+++ b/media/libstagefright/webm/WebmFrameThread.cpp
@@ -321,6 +321,7 @@
status_t WebmFrameMediaSourceThread::pause() {
if (mStarted) {
mPaused = true;
+ mResumed = false;
}
return OK;
}
diff --git a/services/audioflinger/AudioFlinger.cpp b/services/audioflinger/AudioFlinger.cpp
index e5a6e27..4adbad2 100644
--- a/services/audioflinger/AudioFlinger.cpp
+++ b/services/audioflinger/AudioFlinger.cpp
@@ -833,7 +833,7 @@
input.notificationsPerBuffer, input.speed,
input.sharedBuffer, sessionId, &output.flags,
callingPid, input.clientInfo.clientTid, clientUid,
- &lStatus, portId);
+ &lStatus, portId, input.audioTrackCallback);
LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (track == 0));
// we don't abort yet if lStatus != NO_ERROR; there is still work to be done regardless
diff --git a/services/audioflinger/AudioFlinger.h b/services/audioflinger/AudioFlinger.h
index a16fa94..d69b49d 100644
--- a/services/audioflinger/AudioFlinger.h
+++ b/services/audioflinger/AudioFlinger.h
@@ -33,6 +33,7 @@
#include <sys/types.h>
#include <limits.h>
+#include <android/media/IAudioTrackCallback.h>
#include <android/os/BnExternalVibrationController.h>
#include <android-base/macros.h>
diff --git a/services/audioflinger/Effects.cpp b/services/audioflinger/Effects.cpp
index 2f3724f..82b9c96 100644
--- a/services/audioflinger/Effects.cpp
+++ b/services/audioflinger/Effects.cpp
@@ -1395,7 +1395,11 @@
void AudioFlinger::EffectChain::setVolumeForOutput_l(uint32_t left, uint32_t right)
{
- if (mEffectCallback->isOffloadOrDirect() && !isNonOffloadableEnabled_l()) {
+ // for offload or direct thread, if the effect chain has non-offloadable
+ // effect and any effect module within the chain has volume control, then
+ // volume control is delegated to effect, otherwise, set volume to hal.
+ if (mEffectCallback->isOffloadOrDirect() &&
+ !(isNonOffloadableEnabled_l() && hasVolumeControlEnabled_l())) {
float vol_l = (float)left / (1 << 24);
float vol_r = (float)right / (1 << 24);
mEffectCallback->setVolumeForOutput(vol_l, vol_r);
@@ -2296,6 +2300,13 @@
}
}
+bool AudioFlinger::EffectChain::hasVolumeControlEnabled_l() const {
+ for (const auto &effect : mEffects) {
+ if (effect->isVolumeControlEnabled()) return true;
+ }
+ return false;
+}
+
// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
{
diff --git a/services/audioflinger/Effects.h b/services/audioflinger/Effects.h
index 4901451..2826297 100644
--- a/services/audioflinger/Effects.h
+++ b/services/audioflinger/Effects.h
@@ -596,6 +596,9 @@
void setThread(const sp<ThreadBase>& thread);
+ // true if any effect module within the chain has volume control
+ bool hasVolumeControlEnabled_l() const;
+
void setVolumeForOutput_l(uint32_t left, uint32_t right);
mutable Mutex mLock; // mutex protecting effect list
diff --git a/services/audioflinger/Threads.cpp b/services/audioflinger/Threads.cpp
index 2833525..f24dcd7 100644
--- a/services/audioflinger/Threads.cpp
+++ b/services/audioflinger/Threads.cpp
@@ -42,6 +42,7 @@
#include <private/media/AudioTrackShared.h>
#include <private/android_filesystem_config.h>
#include <audio_utils/Balance.h>
+#include <audio_utils/Metadata.h>
#include <audio_utils/channels.h>
#include <audio_utils/mono_blend.h>
#include <audio_utils/primitives.h>
@@ -1921,6 +1922,16 @@
void AudioFlinger::PlaybackThread::onFirstRef()
{
+ if (mOutput == nullptr || mOutput->stream == nullptr) {
+ ALOGE("The stream is not open yet"); // This should not happen.
+ } else {
+ // setEventCallback will need a strong pointer as a parameter. Calling it
+ // here instead of constructor of PlaybackThread so that the onFirstRef
+ // callback would not be made on an incompletely constructed object.
+ if (mOutput->stream->setEventCallback(this) != OK) {
+ ALOGE("Failed to add event callback");
+ }
+ }
run(mThreadName, ANDROID_PRIORITY_URGENT_AUDIO);
}
@@ -2050,7 +2061,8 @@
pid_t tid,
uid_t uid,
status_t *status,
- audio_port_handle_t portId)
+ audio_port_handle_t portId,
+ const sp<media::IAudioTrackCallback>& callback)
{
size_t frameCount = *pFrameCount;
size_t notificationFrameCount = *pNotificationFrameCount;
@@ -2340,6 +2352,12 @@
goto Exit;
}
mTracks.add(track);
+ {
+ Mutex::Autolock _atCbL(mAudioTrackCbLock);
+ if (callback.get() != nullptr) {
+ mAudioTrackCallbacks.emplace(callback);
+ }
+ }
sp<EffectChain> chain = getEffectChain_l(sessionId);
if (chain != 0) {
@@ -2642,6 +2660,29 @@
mCallbackThread->setAsyncError();
}
+void AudioFlinger::PlaybackThread::onCodecFormatChanged(
+ const std::basic_string<uint8_t>& metadataBs)
+{
+ std::thread([this, metadataBs]() {
+ audio_utils::metadata::Data metadata =
+ audio_utils::metadata::dataFromByteString(metadataBs);
+ if (metadata.empty()) {
+ ALOGW("Can not transform the buffer to audio metadata, %s, %d",
+ reinterpret_cast<char*>(const_cast<uint8_t*>(metadataBs.data())),
+ (int)metadataBs.size());
+ return;
+ }
+
+ audio_utils::metadata::ByteString metaDataStr =
+ audio_utils::metadata::byteStringFromData(metadata);
+ std::vector metadataVec(metaDataStr.begin(), metaDataStr.end());
+ Mutex::Autolock _l(mAudioTrackCbLock);
+ for (const auto& callback : mAudioTrackCallbacks) {
+ callback->onCodecFormatChanged(metadataVec);
+ }
+ }).detach();
+}
+
void AudioFlinger::PlaybackThread::resetWriteBlocked(uint32_t sequence)
{
Mutex::Autolock _l(mLock);
diff --git a/services/audioflinger/Threads.h b/services/audioflinger/Threads.h
index b0eb75d..153cf7c 100644
--- a/services/audioflinger/Threads.h
+++ b/services/audioflinger/Threads.h
@@ -720,7 +720,7 @@
// --- PlaybackThread ---
class PlaybackThread : public ThreadBase, public StreamOutHalInterfaceCallback,
- public VolumeInterface {
+ public VolumeInterface, public StreamOutHalInterfaceEventCallback {
public:
#include "PlaybackTracks.h"
@@ -796,6 +796,10 @@
virtual void onAddNewTrack_l();
void onAsyncError(); // error reported by AsyncCallbackThread
+ // StreamHalInterfaceCodecFormatCallback implementation
+ void onCodecFormatChanged(
+ const std::basic_string<uint8_t>& metadataBs) override;
+
// ThreadBase virtuals
virtual void preExit();
@@ -845,7 +849,8 @@
pid_t tid,
uid_t uid,
status_t *status /*non-NULL*/,
- audio_port_handle_t portId);
+ audio_port_handle_t portId,
+ const sp<media::IAudioTrackCallback>& callback);
AudioStreamOut* getOutput() const;
AudioStreamOut* clearOutput();
@@ -1168,6 +1173,10 @@
uint32_t mDrainSequence;
sp<AsyncCallbackThread> mCallbackThread;
+ Mutex mAudioTrackCbLock;
+ // Record of IAudioTrackCallback
+ std::set<sp<media::IAudioTrackCallback>> mAudioTrackCallbacks;
+
private:
// The HAL output sink is treated as non-blocking, but current implementation is blocking
sp<NBAIO_Sink> mOutputSink;
diff --git a/services/audiopolicy/service/AudioPolicyService.cpp b/services/audiopolicy/service/AudioPolicyService.cpp
index 99cec5a..1b8c202 100644
--- a/services/audiopolicy/service/AudioPolicyService.cpp
+++ b/services/audiopolicy/service/AudioPolicyService.cpp
@@ -446,17 +446,19 @@
sp<AudioRecordClient> topActive;
sp<AudioRecordClient> latestActive;
+ sp<AudioRecordClient> topSensitiveActive;
sp<AudioRecordClient> latestSensitiveActive;
nsecs_t topStartNs = 0;
nsecs_t latestStartNs = 0;
+ nsecs_t topSensitiveStartNs = 0;
nsecs_t latestSensitiveStartNs = 0;
bool isA11yOnTop = mUidPolicy->isA11yOnTop();
bool isAssistantOnTop = false;
bool isSensitiveActive = false;
bool isInCall = mPhoneState == AUDIO_MODE_IN_CALL;
- bool rttCallActive =
- (mPhoneState == AUDIO_MODE_IN_CALL || mPhoneState == AUDIO_MODE_IN_COMMUNICATION)
+ bool isInCommunication = mPhoneState == AUDIO_MODE_IN_COMMUNICATION;
+ bool rttCallActive = (isInCall || isInCommunication)
&& mUidPolicy->isRttEnabled();
bool onlyHotwordActive = true;
@@ -479,33 +481,47 @@
continue;
}
- bool isAssistant = mUidPolicy->isAssistantUid(current->uid);
bool isAccessibility = mUidPolicy->isA11yUid(current->uid);
- if (appState == APP_STATE_TOP && !isAccessibility) {
- if (current->startTimeNs > topStartNs) {
- topActive = current;
- topStartNs = current->startTimeNs;
+ // Clients capturing for Accessibility services are not considered
+ // for top or latest active to avoid masking regular clients started before
+ if (!isAccessibility) {
+ bool isAssistant = mUidPolicy->isAssistantUid(current->uid);
+ bool isPrivacySensitive =
+ (current->attributes.flags & AUDIO_FLAG_CAPTURE_PRIVATE) != 0;
+ if (appState == APP_STATE_TOP) {
+ if (isPrivacySensitive) {
+ if (current->startTimeNs > topSensitiveStartNs) {
+ topSensitiveActive = current;
+ topSensitiveStartNs = current->startTimeNs;
+ }
+ } else {
+ if (current->startTimeNs > topStartNs) {
+ topActive = current;
+ topStartNs = current->startTimeNs;
+ }
+ }
+ if (isAssistant) {
+ isAssistantOnTop = true;
+ }
}
- if (isAssistant) {
- isAssistantOnTop = true;
+ // Clients capturing for HOTWORD are not considered
+ // for latest active to avoid masking regular clients started before
+ if (!(current->attributes.source == AUDIO_SOURCE_HOTWORD
+ || ((isA11yOnTop || rttCallActive) && isAssistant))) {
+ if (isPrivacySensitive) {
+ if (current->startTimeNs > latestSensitiveStartNs) {
+ latestSensitiveActive = current;
+ latestSensitiveStartNs = current->startTimeNs;
+ }
+ isSensitiveActive = true;
+ } else {
+ if (current->startTimeNs > latestStartNs) {
+ latestActive = current;
+ latestStartNs = current->startTimeNs;
+ }
+ }
}
}
- // Client capturing for HOTWORD or Accessibility services not considered
- // for latest active to avoid masking regular clients started before
- if (current->startTimeNs > latestStartNs
- && !(current->attributes.source == AUDIO_SOURCE_HOTWORD
- || ((isA11yOnTop || rttCallActive) && isAssistant))
- && !isAccessibility) {
- latestActive = current;
- latestStartNs = current->startTimeNs;
- }
- if ((current->attributes.flags & AUDIO_FLAG_CAPTURE_PRIVATE) != 0) {
- if (current->startTimeNs > latestSensitiveStartNs) {
- latestSensitiveActive = current;
- latestSensitiveStartNs = current->startTimeNs;
- }
- isSensitiveActive = true;
- }
if (current->attributes.source != AUDIO_SOURCE_HOTWORD) {
onlyHotwordActive = false;
}
@@ -514,6 +530,21 @@
// if no active client with UI on Top, consider latest active as top
if (topActive == nullptr) {
topActive = latestActive;
+ topStartNs = latestStartNs;
+ }
+ if (topSensitiveActive == nullptr) {
+ topSensitiveActive = latestSensitiveActive;
+ topSensitiveStartNs = latestSensitiveStartNs;
+ }
+
+ // If both privacy sensitive and regular capture are active:
+ // if the regular capture is privileged
+ // allow concurrency
+ // else
+ // favor the privacy sensitive case
+ if (topActive != nullptr && topSensitiveActive != nullptr
+ && !topActive->canCaptureCallOrOutput) {
+ topActive = nullptr;
}
for (size_t i =0; i < mAudioRecordClients.size(); i++) {
@@ -524,8 +555,17 @@
audio_source_t source = current->attributes.source;
bool isTopOrLatestActive = topActive == nullptr ? false : current->uid == topActive->uid;
- bool isLatestSensitive = latestSensitiveActive == nullptr ?
- false : current->uid == latestSensitiveActive->uid;
+ bool isTopOrLatestSensitive = topSensitiveActive == nullptr ?
+ false : current->uid == topSensitiveActive->uid;
+
+ auto canCaptureIfInCallOrCommunication = [&](const auto &recordClient) {
+ bool canCaptureCall = recordClient->canCaptureCallOrOutput;
+ bool canCaptureCommunication = recordClient->canCaptureCallOrOutput
+ || recordClient->uid == mPhoneStateOwnerUid
+ || isServiceUid(mPhoneStateOwnerUid);
+ return !(isInCall && !canCaptureCall)
+ && !(isInCommunication && !canCaptureCommunication);
+ };
// By default allow capture if:
// The assistant is not on TOP
@@ -533,9 +573,10 @@
// AND there is no active privacy sensitive capture or call
// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
bool allowCapture = !isAssistantOnTop
- && ((isTopOrLatestActive && !isLatestSensitive) || isLatestSensitive)
- && !(isSensitiveActive && !(isLatestSensitive || current->canCaptureCallOrOutput))
- && !(isInCall && !current->canCaptureCallOrOutput);
+ && (isTopOrLatestActive || isTopOrLatestSensitive)
+ && !(isSensitiveActive
+ && !(isTopOrLatestSensitive || current->canCaptureCallOrOutput))
+ && canCaptureIfInCallOrCommunication(current);
if (isVirtualSource(source)) {
// Allow capture for virtual (remote submix, call audio TX or RX...) sources
@@ -554,8 +595,9 @@
}
} else {
if (((isAssistantOnTop && source == AUDIO_SOURCE_VOICE_RECOGNITION) ||
- source == AUDIO_SOURCE_HOTWORD) &&
- (!(isSensitiveActive || isInCall) || current->canCaptureCallOrOutput)) {
+ source == AUDIO_SOURCE_HOTWORD)
+ && !(isSensitiveActive && !current->canCaptureCallOrOutput)
+ && canCaptureIfInCallOrCommunication(current)) {
allowCapture = true;
}
}
@@ -567,7 +609,8 @@
// OR
// Is on TOP AND the source is VOICE_RECOGNITION or HOTWORD
if (!isAssistantOnTop
- && (!(isSensitiveActive || isInCall) || current->canCaptureCallOrOutput)) {
+ && !(isSensitiveActive && !current->canCaptureCallOrOutput)
+ && canCaptureIfInCallOrCommunication(current)) {
allowCapture = true;
}
if (isA11yOnTop) {
@@ -580,7 +623,8 @@
// All active clients are using HOTWORD source
// AND no call is active
// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
- if (onlyHotwordActive && !(isInCall && !current->canCaptureCallOrOutput)) {
+ if (onlyHotwordActive
+ && canCaptureIfInCallOrCommunication(current)) {
allowCapture = true;
}
}
diff --git a/services/camera/libcameraservice/api2/HeicEncoderInfoManager.cpp b/services/camera/libcameraservice/api2/HeicEncoderInfoManager.cpp
index d7cc2bf..d36ca3b 100644
--- a/services/camera/libcameraservice/api2/HeicEncoderInfoManager.cpp
+++ b/services/camera/libcameraservice/api2/HeicEncoderInfoManager.cpp
@@ -91,6 +91,8 @@
// The "measured-frame-rate-WIDTHxHEIGHT-range" key is optional.
// Hardcode to some default value (3.33ms * tile count) based on resolution.
*stall = 3333333LL * width * height / (kGridWidth * kGridHeight);
+ *useHeic = chooseHeic;
+ *useGrid = enableGrid;
return true;
}
@@ -275,9 +277,13 @@
ALOGE("%s: Failed to get codec info for %s", __FUNCTION__, mime);
break;
}
+ ALOGV("%s: [%s] codec found", __FUNCTION__,
+ info->getCodecName());
// Filter out software ones as they may be too slow
if (!(info->getAttributes() & MediaCodecInfo::kFlagIsHardwareAccelerated)) {
+ ALOGV("%s: [%s] Filter out software ones as they may be too slow", __FUNCTION__,
+ info->getCodecName());
continue;
}
diff --git a/services/camera/libcameraservice/common/CameraProviderManager.cpp b/services/camera/libcameraservice/common/CameraProviderManager.cpp
index 2c97a38..1d794a1 100644
--- a/services/camera/libcameraservice/common/CameraProviderManager.cpp
+++ b/services/camera/libcameraservice/common/CameraProviderManager.cpp
@@ -326,8 +326,11 @@
// Pass the camera ID to start interface so that it will save it to the map of ICameraProviders
// that are currently in use.
- const sp<provider::V2_4::ICameraProvider> interface =
- deviceInfo->mParentProvider->startProviderInterface();
+ sp<ProviderInfo> parentProvider = deviceInfo->mParentProvider.promote();
+ if (parentProvider == nullptr) {
+ return DEAD_OBJECT;
+ }
+ const sp<provider::V2_4::ICameraProvider> interface = parentProvider->startProviderInterface();
if (interface == nullptr) {
return DEAD_OBJECT;
}
@@ -380,8 +383,11 @@
if (deviceInfo == nullptr) return NAME_NOT_FOUND;
auto *deviceInfo3 = static_cast<ProviderInfo::DeviceInfo3*>(deviceInfo);
- const sp<provider::V2_4::ICameraProvider> provider =
- deviceInfo->mParentProvider->startProviderInterface();
+ sp<ProviderInfo> parentProvider = deviceInfo->mParentProvider.promote();
+ if (parentProvider == nullptr) {
+ return DEAD_OBJECT;
+ }
+ const sp<provider::V2_4::ICameraProvider> provider = parentProvider->startProviderInterface();
if (provider == nullptr) {
return DEAD_OBJECT;
}
@@ -423,8 +429,11 @@
if (deviceInfo == nullptr) return NAME_NOT_FOUND;
auto *deviceInfo1 = static_cast<ProviderInfo::DeviceInfo1*>(deviceInfo);
- const sp<provider::V2_4::ICameraProvider> provider =
- deviceInfo->mParentProvider->startProviderInterface();
+ sp<ProviderInfo> parentProvider = deviceInfo->mParentProvider.promote();
+ if (parentProvider == nullptr) {
+ return DEAD_OBJECT;
+ }
+ const sp<provider::V2_4::ICameraProvider> provider = parentProvider->startProviderInterface();
if (provider == nullptr) {
return DEAD_OBJECT;
}
@@ -999,7 +1008,8 @@
if (sizeAvail) continue;
int64_t stall = 0;
- bool useHeic, useGrid;
+ bool useHeic = false;
+ bool useGrid = false;
if (camera3::HeicCompositeStream::isSizeSupportedByHeifEncoder(
halStreamConfigs.data.i32[i+1], halStreamConfigs.data.i32[i+2],
&useHeic, &useGrid, &stall)) {
@@ -2044,7 +2054,10 @@
sp<InterfaceT> device;
ATRACE_CALL();
if (mSavedInterface == nullptr) {
- device = mParentProvider->startDeviceInterface<InterfaceT>(mName);
+ sp<ProviderInfo> parentProvider = mParentProvider.promote();
+ if (parentProvider != nullptr) {
+ device = parentProvider->startDeviceInterface<InterfaceT>(mName);
+ }
} else {
device = (InterfaceT *) mSavedInterface.get();
}
diff --git a/services/camera/libcameraservice/common/CameraProviderManager.h b/services/camera/libcameraservice/common/CameraProviderManager.h
index 1a02980..5359c39 100644
--- a/services/camera/libcameraservice/common/CameraProviderManager.h
+++ b/services/camera/libcameraservice/common/CameraProviderManager.h
@@ -447,7 +447,7 @@
std::map<std::string, hardware::camera::common::V1_0::CameraDeviceStatus>
mPhysicalStatus;
- sp<ProviderInfo> mParentProvider;
+ wp<ProviderInfo> mParentProvider;
bool hasFlashUnit() const { return mHasFlashUnit; }
bool supportNativeZoomRatio() const { return mSupportNativeZoomRatio; }
diff --git a/services/camera/libcameraservice/utils/ExifUtils.cpp b/services/camera/libcameraservice/utils/ExifUtils.cpp
index c0afdc1..8a0303a 100644
--- a/services/camera/libcameraservice/utils/ExifUtils.cpp
+++ b/services/camera/libcameraservice/utils/ExifUtils.cpp
@@ -603,13 +603,13 @@
}
bool ExifUtilsImpl::setImageHeight(uint32_t length) {
- SET_LONG(EXIF_IFD_0, EXIF_TAG_IMAGE_LENGTH, length);
+ SET_SHORT(EXIF_IFD_0, EXIF_TAG_IMAGE_LENGTH, length);
SET_LONG(EXIF_IFD_EXIF, EXIF_TAG_PIXEL_Y_DIMENSION, length);
return true;
}
bool ExifUtilsImpl::setImageWidth(uint32_t width) {
- SET_LONG(EXIF_IFD_0, EXIF_TAG_IMAGE_WIDTH, width);
+ SET_SHORT(EXIF_IFD_0, EXIF_TAG_IMAGE_WIDTH, width);
SET_LONG(EXIF_IFD_EXIF, EXIF_TAG_PIXEL_X_DIMENSION, width);
return true;
}
diff --git a/services/mediacodec/Android.bp b/services/mediacodec/Android.bp
index e2e1429..4bf103c 100644
--- a/services/mediacodec/Android.bp
+++ b/services/mediacodec/Android.bp
@@ -89,6 +89,31 @@
"android.hidl.memory@1.0",
],
+ runtime_libs: [
+ "libstagefright_soft_aacdec",
+ "libstagefright_soft_aacenc",
+ "libstagefright_soft_amrdec",
+ "libstagefright_soft_amrnbenc",
+ "libstagefright_soft_amrwbenc",
+ "libstagefright_soft_avcdec",
+ "libstagefright_soft_avcenc",
+ "libstagefright_soft_flacdec",
+ "libstagefright_soft_flacenc",
+ "libstagefright_soft_g711dec",
+ "libstagefright_soft_gsmdec",
+ "libstagefright_soft_hevcdec",
+ "libstagefright_soft_mp3dec",
+ "libstagefright_soft_mpeg2dec",
+ "libstagefright_soft_mpeg4dec",
+ "libstagefright_soft_mpeg4enc",
+ "libstagefright_soft_opusdec",
+ "libstagefright_soft_rawdec",
+ "libstagefright_soft_vorbisdec",
+ "libstagefright_soft_vpxdec",
+ "libstagefright_soft_vpxenc",
+ "libstagefright_softomx_plugin",
+ ],
+
// OMX interfaces force this to stay in 32-bit mode;
compile_multilib: "32",
diff --git a/services/mediametrics/MediaMetricsService.cpp b/services/mediametrics/MediaMetricsService.cpp
index 811e135..a6fefd2 100644
--- a/services/mediametrics/MediaMetricsService.cpp
+++ b/services/mediametrics/MediaMetricsService.cpp
@@ -113,6 +113,7 @@
case AID_MEDIA_CODEC:
case AID_MEDIA_EX:
case AID_MEDIA_DRM:
+ // case AID_SHELL: // DEBUG ONLY - used for mediametrics_tests to add new keys
case AID_SYSTEM:
// trusted source, only override default values
isTrusted = true;
@@ -145,9 +146,10 @@
}
}
- ALOGV("%s: given uid %d; sanitized uid: %d sanitized pkg: %s "
+ ALOGV("%s: isTrusted:%d given uid %d; sanitized uid: %d sanitized pkg: %s "
"sanitized pkg version: %lld",
__func__,
+ (int)isTrusted,
uid_given, item->getUid(),
item->getPkgName().c_str(),
(long long)item->getPkgVersionCode());
@@ -384,7 +386,7 @@
break;
}
if (now > when && (now - when) <= mMaxRecordAgeNs) {
- break; // TODO: if we use BOOTTIME, should be monotonic.
+ break; // Note SYSTEM_TIME_REALTIME may not be monotonic.
}
if (i >= mMaxRecordsExpiredAtOnce) {
// this represents "one too many"; tell caller there are
diff --git a/services/mediametrics/TimeMachine.h b/services/mediametrics/TimeMachine.h
index e048d0e..a4c3693 100644
--- a/services/mediametrics/TimeMachine.h
+++ b/services/mediametrics/TimeMachine.h
@@ -93,7 +93,7 @@
template <typename T>
status_t getValue(const std::string &property, T* value, int64_t time = 0) const {
- if (time == 0) time = systemTime(SYSTEM_TIME_BOOTTIME);
+ if (time == 0) time = systemTime(SYSTEM_TIME_REALTIME);
const auto tsptr = mPropertyMap.find(property);
if (tsptr == mPropertyMap.end()) return BAD_VALUE;
const auto& timeSequence = tsptr->second;
@@ -122,14 +122,25 @@
template <typename T>
void putValue(const std::string &property,
T&& e, int64_t time = 0) {
- if (time == 0) time = systemTime(SYSTEM_TIME_BOOTTIME);
+ if (time == 0) time = systemTime(SYSTEM_TIME_REALTIME);
mLastModificationTime = time;
+ if (mPropertyMap.size() >= kKeyMaxProperties &&
+ !mPropertyMap.count(property)) {
+ ALOGV("%s: too many properties, rejecting %s", __func__, property.c_str());
+ return;
+ }
auto& timeSequence = mPropertyMap[property];
Elem el{std::forward<T>(e)};
if (timeSequence.empty() // no elements
|| property.back() == AMEDIAMETRICS_PROP_SUFFIX_CHAR_DUPLICATES_ALLOWED
|| timeSequence.rbegin()->second != el) { // value changed
timeSequence.emplace(time, std::move(el));
+
+ if (timeSequence.size() > kTimeSequenceMaxElements) {
+ ALOGV("%s: restricting maximum elements (discarding oldest) for %s",
+ __func__, property.c_str());
+ timeSequence.erase(timeSequence.begin());
+ }
}
}
@@ -188,6 +199,8 @@
using History = std::map<std::string /* key */, std::shared_ptr<KeyHistory>>;
+ static inline constexpr size_t kTimeSequenceMaxElements = 100;
+ static inline constexpr size_t kKeyMaxProperties = 100;
static inline constexpr size_t kKeyLowWaterMark = 500;
static inline constexpr size_t kKeyHighWaterMark = 1000;
@@ -239,6 +252,9 @@
const int64_t time = item->getTimestamp();
const std::string &key = item->getKey();
+ ALOGV("%s(%zu, %zu): key: %s isTrusted:%d size:%zu",
+ __func__, mKeyLowWaterMark, mKeyHighWaterMark,
+ key.c_str(), (int)isTrusted, item->count());
std::shared_ptr<KeyHistory> keyHistory;
{
std::vector<std::any> garbage;
@@ -324,7 +340,7 @@
/**
* Individual property put.
*
- * Put takes in a time (if none is provided then BOOTTIME is used).
+ * Put takes in a time (if none is provided then SYSTEM_TIME_REALTIME is used).
*/
template <typename T>
status_t put(const std::string &url, T &&e, int64_t time = 0) {
@@ -333,7 +349,7 @@
std::shared_ptr<KeyHistory> keyHistory =
getKeyHistoryFromUrl(url, &key, &prop);
if (keyHistory == nullptr) return BAD_VALUE;
- if (time == 0) time = systemTime(SYSTEM_TIME_BOOTTIME);
+ if (time == 0) time = systemTime(SYSTEM_TIME_REALTIME);
std::lock_guard lock(getLockForKey(key));
keyHistory->putValue(prop, std::forward<T>(e), time);
return NO_ERROR;
@@ -446,6 +462,9 @@
/**
* Garbage collects if the TimeMachine size exceeds the high water mark.
*
+ * This GC operation limits the number of keys stored (not the size of properties
+ * stored in each key).
+ *
* \param garbage a type-erased vector of elements to be destroyed
* outside of lock. Move large items to be destroyed here.
*
diff --git a/services/mediametrics/TransactionLog.h b/services/mediametrics/TransactionLog.h
index 27e2c14..190a99e 100644
--- a/services/mediametrics/TransactionLog.h
+++ b/services/mediametrics/TransactionLog.h
@@ -264,7 +264,7 @@
return ret;
}
- const size_t mLowWaterMark = kLogItemsHighWater;
+ const size_t mLowWaterMark = kLogItemsLowWater;
const size_t mHighWaterMark = kLogItemsHighWater;
mutable std::mutex mLock;
diff --git a/services/mediametrics/tests/mediametrics_tests.cpp b/services/mediametrics/tests/mediametrics_tests.cpp
index 27b72eb..b8e566e 100644
--- a/services/mediametrics/tests/mediametrics_tests.cpp
+++ b/services/mediametrics/tests/mediametrics_tests.cpp
@@ -846,3 +846,21 @@
ASSERT_EQ(ll, (int32_t) countNewlines(s.c_str()));
}
}
+
+#if 0
+// Stress test code for garbage collection, you need to enable AID_SHELL as trusted to run
+// in MediaMetricsService.cpp.
+//
+// TODO: Make a dedicated stress test.
+//
+TEST(mediametrics_tests, gc_same_key) {
+ // random keys ignored when empty
+ for (int i = 0; i < 10000000; ++i) {
+ std::unique_ptr<mediametrics::Item> test_key(mediametrics::Item::create("audio.zzz.123"));
+ test_key->set("event#", "hello");
+ test_key->set("value", (int)10);
+ test_key->selfrecord();
+ }
+ //mediaMetrics->dump(fileno(stdout), {} /* args */);
+}
+#endif
diff --git a/services/mediaresourcemanager/ResourceManagerService.cpp b/services/mediaresourcemanager/ResourceManagerService.cpp
index be5af00..ff45c87 100644
--- a/services/mediaresourcemanager/ResourceManagerService.cpp
+++ b/services/mediaresourcemanager/ResourceManagerService.cpp
@@ -114,6 +114,7 @@
info.uid = uid;
info.clientId = clientId;
info.client = client;
+ info.pendingRemoval = false;
index = infos.add(clientId, info);
}
@@ -648,6 +649,36 @@
return Status::ok();
}
+Status ResourceManagerService::markClientForPendingRemoval(int32_t pid, int64_t clientId) {
+ String8 log = String8::format(
+ "markClientForPendingRemoval(pid %d, clientId %lld)",
+ pid, (long long) clientId);
+ mServiceLog->add(log);
+
+ Mutex::Autolock lock(mLock);
+ if (!mProcessInfo->isValidPid(pid)) {
+ ALOGE("Rejected markClientForPendingRemoval call with invalid pid.");
+ return Status::fromServiceSpecificError(BAD_VALUE);
+ }
+ ssize_t index = mMap.indexOfKey(pid);
+ if (index < 0) {
+ ALOGV("markClientForPendingRemoval: didn't find pid %d for clientId %lld",
+ pid, (long long)clientId);
+ return Status::ok();
+ }
+ ResourceInfos &infos = mMap.editValueAt(index);
+
+ index = infos.indexOfKey(clientId);
+ if (index < 0) {
+ ALOGV("markClientForPendingRemoval: didn't find clientId %lld", (long long) clientId);
+ return Status::ok();
+ }
+
+ ResourceInfo &info = infos.editValueAt(index);
+ info.pendingRemoval = true;
+ return Status::ok();
+}
+
bool ResourceManagerService::getPriority_l(int pid, int* priority) {
int newPid = pid;
@@ -693,6 +724,12 @@
int lowestPriorityPid;
int lowestPriority;
int callingPriority;
+
+ // Before looking into other processes, check if we have clients marked for
+ // pending removal in the same process.
+ if (getBiggestClient_l(callingPid, type, client, true /* pendingRemovalOnly */)) {
+ return true;
+ }
if (!getPriority_l(callingPid, &callingPriority)) {
ALOGE("getLowestPriorityBiggestClient_l: can't get process priority for pid %d",
callingPid);
@@ -761,7 +798,8 @@
}
bool ResourceManagerService::getBiggestClient_l(
- int pid, MediaResource::Type type, std::shared_ptr<IResourceManagerClient> *client) {
+ int pid, MediaResource::Type type, std::shared_ptr<IResourceManagerClient> *client,
+ bool pendingRemovalOnly) {
ssize_t index = mMap.indexOfKey(pid);
if (index < 0) {
ALOGE("getBiggestClient_l: can't find resource info for pid %d", pid);
@@ -773,6 +811,9 @@
const ResourceInfos &infos = mMap.valueAt(index);
for (size_t i = 0; i < infos.size(); ++i) {
const ResourceList &resources = infos[i].resources;
+ if (pendingRemovalOnly && !infos[i].pendingRemoval) {
+ continue;
+ }
for (auto it = resources.begin(); it != resources.end(); it++) {
const MediaResourceParcel &resource = it->second;
if (resource.type == type) {
diff --git a/services/mediaresourcemanager/ResourceManagerService.h b/services/mediaresourcemanager/ResourceManagerService.h
index f500c62..ee982b7 100644
--- a/services/mediaresourcemanager/ResourceManagerService.h
+++ b/services/mediaresourcemanager/ResourceManagerService.h
@@ -18,6 +18,8 @@
#ifndef ANDROID_MEDIA_RESOURCEMANAGERSERVICE_H
#define ANDROID_MEDIA_RESOURCEMANAGERSERVICE_H
+#include <map>
+
#include <aidl/android/media/BnResourceManagerService.h>
#include <arpa/inet.h>
#include <media/MediaResource.h>
@@ -50,6 +52,7 @@
std::shared_ptr<IResourceManagerClient> client;
sp<DeathNotifier> deathNotifier;
ResourceList resources;
+ bool pendingRemoval{false};
};
// TODO: convert these to std::map
@@ -122,6 +125,8 @@
int originalPid,
int newPid) override;
+ Status markClientForPendingRemoval(int32_t pid, int64_t clientId) override;
+
Status removeResource(int pid, int64_t clientId, bool checkValid);
private:
@@ -146,7 +151,8 @@
// Gets the client who owns biggest piece of specified resource type from pid.
// Returns false if failed. The client will remain unchanged if failed.
bool getBiggestClient_l(int pid, MediaResource::Type type,
- std::shared_ptr<IResourceManagerClient> *client);
+ std::shared_ptr<IResourceManagerClient> *client,
+ bool pendingRemovalOnly = false);
bool isCallingPriorityHigher_l(int callingPid, int pid);
diff --git a/services/mediaresourcemanager/test/ResourceManagerService_test.cpp b/services/mediaresourcemanager/test/ResourceManagerService_test.cpp
index 5d839fa..702935d 100644
--- a/services/mediaresourcemanager/test/ResourceManagerService_test.cpp
+++ b/services/mediaresourcemanager/test/ResourceManagerService_test.cpp
@@ -472,6 +472,56 @@
}
}
+ void testMarkClientForPendingRemoval() {
+ bool result;
+
+ {
+ addResource();
+ mService->mSupportsSecureWithNonSecureCodec = true;
+
+ std::vector<MediaResourceParcel> resources;
+ resources.push_back(MediaResource(MediaResource::Type::kNonSecureCodec, 1));
+
+ // Remove low priority clients
+ mService->removeClient(kTestPid1, getId(mTestClient1));
+
+ // no lower priority client
+ CHECK_STATUS_FALSE(mService->reclaimResource(kTestPid2, resources, &result));
+ verifyClients(false /* c1 */, false /* c2 */, false /* c3 */);
+
+ mService->markClientForPendingRemoval(kTestPid2, getId(mTestClient2));
+
+ // client marked for pending removal from the same process got reclaimed
+ CHECK_STATUS_TRUE(mService->reclaimResource(kTestPid2, resources, &result));
+ verifyClients(false /* c1 */, true /* c2 */, false /* c3 */);
+
+ // clean up client 3 which still left
+ mService->removeClient(kTestPid2, getId(mTestClient3));
+ }
+
+ {
+ addResource();
+ mService->mSupportsSecureWithNonSecureCodec = true;
+
+ std::vector<MediaResourceParcel> resources;
+ resources.push_back(MediaResource(MediaResource::Type::kNonSecureCodec, 1));
+
+ mService->markClientForPendingRemoval(kTestPid2, getId(mTestClient2));
+
+ // client marked for pending removal from the same process got reclaimed
+ // first, even though there are lower priority process
+ CHECK_STATUS_TRUE(mService->reclaimResource(kTestPid2, resources, &result));
+ verifyClients(false /* c1 */, true /* c2 */, false /* c3 */);
+
+ // lower priority client got reclaimed
+ CHECK_STATUS_TRUE(mService->reclaimResource(kTestPid2, resources, &result));
+ verifyClients(true /* c1 */, false /* c2 */, false /* c3 */);
+
+ // clean up client 3 which still left
+ mService->removeClient(kTestPid2, getId(mTestClient3));
+ }
+ }
+
void testRemoveClient() {
addResource();
@@ -900,4 +950,8 @@
testOverridePid();
}
+TEST_F(ResourceManagerServiceTest, markClientForPendingRemoval) {
+ testMarkClientForPendingRemoval();
+}
+
} // namespace android