#include <utils/Log.h>
#include <utils/Errors.h>
-#include <camera/CameraMetadata.h>
#include <binder/Parcel.h>
+#include <camera/CameraMetadata.h>
+#include <camera/VendorTagDescriptor.h>
namespace android {
return updateImpl(tag, (const void*)string.string(), string.size() + 1);
}
+status_t CameraMetadata::update(const camera_metadata_ro_entry &entry) {
+ status_t res;
+ if (mLocked) {
+ ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
+ return INVALID_OPERATION;
+ }
+ if ( (res = checkType(entry.tag, entry.type)) != OK) {
+ return res;
+ }
+ return updateImpl(entry.tag, (const void*)entry.data.u8, entry.count);
+}
+
status_t CameraMetadata::updateImpl(uint32_t tag, const void *data,
size_t data_count) {
status_t res;
mBuffer = otherBuf;
}
+status_t CameraMetadata::getTagFromName(const char *name,
+ const VendorTagDescriptor* vTags, uint32_t *tag) {
+
+ if (name == nullptr || tag == nullptr) return BAD_VALUE;
+
+ size_t nameLength = strlen(name);
+
+ const SortedVector<String8> *vendorSections;
+ size_t vendorSectionCount = 0;
+
+ if (vTags != NULL) {
+ vendorSections = vTags->getAllSectionNames();
+ vendorSectionCount = vendorSections->size();
+ }
+
+ // First, find the section by the longest string match
+ const char *section = NULL;
+ size_t sectionIndex = 0;
+ size_t sectionLength = 0;
+ size_t totalSectionCount = ANDROID_SECTION_COUNT + vendorSectionCount;
+ for (size_t i = 0; i < totalSectionCount; ++i) {
+
+ const char *str = (i < ANDROID_SECTION_COUNT) ? camera_metadata_section_names[i] :
+ (*vendorSections)[i - ANDROID_SECTION_COUNT].string();
+
+ ALOGV("%s: Trying to match against section '%s'", __FUNCTION__, str);
+
+ if (strstr(name, str) == name) { // name begins with the section name
+ size_t strLength = strlen(str);
+
+ ALOGV("%s: Name begins with section name", __FUNCTION__);
+
+ // section name is the longest we've found so far
+ if (section == NULL || sectionLength < strLength) {
+ section = str;
+ sectionIndex = i;
+ sectionLength = strLength;
+
+ ALOGV("%s: Found new best section (%s)", __FUNCTION__, section);
+ }
+ }
+ }
+
+ // TODO: Make above get_camera_metadata_section_from_name ?
+
+ if (section == NULL) {
+ return NAME_NOT_FOUND;
+ } else {
+ ALOGV("%s: Found matched section '%s' (%zu)",
+ __FUNCTION__, section, sectionIndex);
+ }
+
+ // Get the tag name component of the name
+ const char *nameTagName = name + sectionLength + 1; // x.y.z -> z
+ if (sectionLength + 1 >= nameLength) {
+ return BAD_VALUE;
+ }
+
+ // Match rest of name against the tag names in that section only
+ uint32_t candidateTag = 0;
+ if (sectionIndex < ANDROID_SECTION_COUNT) {
+ // Match built-in tags (typically android.*)
+ uint32_t tagBegin, tagEnd; // [tagBegin, tagEnd)
+ tagBegin = camera_metadata_section_bounds[sectionIndex][0];
+ tagEnd = camera_metadata_section_bounds[sectionIndex][1];
+
+ for (candidateTag = tagBegin; candidateTag < tagEnd; ++candidateTag) {
+ const char *tagName = get_camera_metadata_tag_name(candidateTag);
+
+ if (strcmp(nameTagName, tagName) == 0) {
+ ALOGV("%s: Found matched tag '%s' (%d)",
+ __FUNCTION__, tagName, candidateTag);
+ break;
+ }
+ }
+
+ if (candidateTag == tagEnd) {
+ return NAME_NOT_FOUND;
+ }
+ } else if (vTags != NULL) {
+ // Match vendor tags (typically com.*)
+ const String8 sectionName(section);
+ const String8 tagName(nameTagName);
+
+ status_t res = OK;
+ if ((res = vTags->lookupTag(tagName, sectionName, &candidateTag)) != OK) {
+ return NAME_NOT_FOUND;
+ }
+ }
+
+ *tag = candidateTag;
+ return OK;
+}
+
+
}; // namespace android
return res;
}
-SortedVector<String8> VendorTagDescriptor::getAllSectionNames() const {
- return mSections;
+const SortedVector<String8>* VendorTagDescriptor::getAllSectionNames() const {
+ return &mSections;
}
status_t VendorTagDescriptor::lookupTag(String8 name, String8 section, /*out*/uint32_t* tag) const {
void tearDown(int streamId);
void prepare2(int maxCount, int streamId);
+
+ void setDeferredConfiguration(int streamId, in OutputConfiguration outputConfiguration);
}
return mSurfaceSetID;
}
+int OutputConfiguration::getSurfaceType() const {
+ return mSurfaceType;
+}
+
+int OutputConfiguration::getWidth() const {
+ return mWidth;
+}
+
+int OutputConfiguration::getHeight() const {
+ return mHeight;
+}
+
OutputConfiguration::OutputConfiguration() :
mRotation(INVALID_ROTATION),
- mSurfaceSetID(INVALID_SET_ID) {
+ mSurfaceSetID(INVALID_SET_ID),
+ mSurfaceType(SURFACE_TYPE_UNKNOWN),
+ mWidth(0),
+ mHeight(0) {
}
OutputConfiguration::OutputConfiguration(const Parcel& parcel) :
return err;
}
+ int surfaceType = SURFACE_TYPE_UNKNOWN;
+ if ((err = parcel->readInt32(&surfaceType)) != OK) {
+ ALOGE("%s: Failed to read surface type from parcel", __FUNCTION__);
+ return err;
+ }
+
+ int width = 0;
+ if ((err = parcel->readInt32(&width)) != OK) {
+ ALOGE("%s: Failed to read surface width from parcel", __FUNCTION__);
+ return err;
+ }
+
+ int height = 0;
+ if ((err = parcel->readInt32(&height)) != OK) {
+ ALOGE("%s: Failed to read surface height from parcel", __FUNCTION__);
+ return err;
+ }
+
view::Surface surfaceShim;
if ((err = surfaceShim.readFromParcel(parcel)) != OK) {
- ALOGE("%s: Failed to read surface from parcel", __FUNCTION__);
- return err;
+ // Read surface failure for deferred surface configuration is expected.
+ if (surfaceType == SURFACE_TYPE_SURFACE_VIEW ||
+ surfaceType == SURFACE_TYPE_SURFACE_TEXTURE) {
+ ALOGV("%s: Get null surface from a deferred surface configuration (%dx%d)",
+ __FUNCTION__, width, height);
+ err = OK;
+ } else {
+ ALOGE("%s: Failed to read surface from parcel", __FUNCTION__);
+ return err;
+ }
}
mGbp = surfaceShim.graphicBufferProducer;
mRotation = rotation;
mSurfaceSetID = setID;
+ mSurfaceType = surfaceType;
+ mWidth = width;
+ mHeight = height;
- ALOGV("%s: OutputConfiguration: bp = %p, name = %s, rotation = %d, setId = %d", __FUNCTION__,
- mGbp.get(), String8(surfaceShim.name).string(), mRotation, mSurfaceSetID);
+ ALOGV("%s: OutputConfiguration: bp = %p, name = %s, rotation = %d, setId = %d,"
+ "surfaceType = %d", __FUNCTION__, mGbp.get(), String8(surfaceShim.name).string(),
+ mRotation, mSurfaceSetID, mSurfaceType);
return err;
}
err = parcel->writeInt32(mSurfaceSetID);
if (err != OK) return err;
+ err = parcel->writeInt32(mSurfaceType);
+ if (err != OK) return err;
+
+ err = parcel->writeInt32(mWidth);
+ if (err != OK) return err;
+
+ err = parcel->writeInt32(mHeight);
+ if (err != OK) return err;
+
view::Surface surfaceShim;
surfaceShim.name = String16("unknown_name"); // name of surface
surfaceShim.graphicBufferProducer = mGbp;
service cameraserver /system/bin/cameraserver
class main
user cameraserver
- group audio camera drmrpc inet media mediadrm net_bt net_bt_admin net_bw_acct
+ group audio camera input drmrpc
ioprio rt 4
- writepid /dev/cpuset/foreground/tasks
+ writepid /dev/cpuset/camera-daemon/tasks /dev/stune/top-app/tasks
static uint32_t gTimeLimitSec = kMaxTimeLimitSec;
// Set by signal handler to stop recording.
-static volatile bool gStopRequested;
+static volatile bool gStopRequested = false;
// Previous signal handler state, restored after first hit.
static struct sigaction gOrigSigactionINT;
return err;
}
- // This is set by the signal handler.
- gStopRequested = false;
-
// Run until we're signaled.
while (!gStopRequested) {
size_t bufIndex, offset, size;
case FORMAT_MP4: {
// Configure muxer. We have to wait for the CSD blob from the encoder
// before we can start it.
+ err = unlink(fileName);
+ if (err != 0 && errno != ENOENT) {
+ fprintf(stderr, "ERROR: couldn't remove existing file\n");
+ abort();
+ }
int fd = open(fileName, O_CREAT | O_LARGEFILE | O_TRUNC | O_RDWR, S_IRUSR | S_IWUSR);
if (fd < 0) {
fprintf(stderr, "ERROR: couldn't open file\n");
if (muxer != NULL) {
// If we don't stop muxer explicitly, i.e. let the destructor run,
// it may hang (b/11050628).
- muxer->stop();
+ err = muxer->stop();
} else if (rawFp != stdout) {
fclose(rawFp);
}
#define ANDROID_CLIENT_CAMERA2_CAMERAMETADATA_CPP
#include "system/camera_metadata.h"
+
#include <utils/String8.h>
#include <utils/Vector.h>
#include <binder/Parcelable.h>
namespace android {
+class VendorTagDescriptor;
+
/**
* A convenience wrapper around the C-based camera_metadata_t library.
*/
const camera_metadata_rational_t *data, size_t data_count);
status_t update(uint32_t tag,
const String8 &string);
+ status_t update(const camera_metadata_ro_entry &entry);
+
template<typename T>
status_t update(uint32_t tag, Vector<T> data) {
static status_t writeToParcel(Parcel &parcel,
const camera_metadata_t* metadata);
+ /**
+ * Find tag id for a given tag name, also checking vendor tags if available.
+ * On success, returns OK and writes the tag id into tag.
+ *
+ * This is a slow method.
+ */
+ static status_t getTagFromName(const char *name,
+ const VendorTagDescriptor* vTags, uint32_t *tag);
+
private:
camera_metadata_t *mBuffer;
mutable bool mLocked;
/**
* Convenience method to get a vector containing all vendor tag
* sections, or an empty vector if none are defined.
+ * The pointer is valid for the lifetime of the VendorTagDescriptor,
+ * or until readParcel or copyFrom is invoked.
*/
- SortedVector<String8> getAllSectionNames() const;
+ const SortedVector<String8>* getAllSectionNames() const;
/**
* Lookup the tag id for a given tag name and section.
static const int INVALID_ROTATION;
static const int INVALID_SET_ID;
+ enum SurfaceType{
+ SURFACE_TYPE_UNKNOWN = -1,
+ SURFACE_TYPE_SURFACE_VIEW = 0,
+ SURFACE_TYPE_SURFACE_TEXTURE = 1
+ };
sp<IGraphicBufferProducer> getGraphicBufferProducer() const;
int getRotation() const;
int getSurfaceSetID() const;
-
+ int getSurfaceType() const;
+ int getWidth() const;
+ int getHeight() const;
/**
* Keep impl up-to-date with OutputConfiguration.java in frameworks/base
*/
bool operator == (const OutputConfiguration& other) const {
return (mGbp == other.mGbp &&
mRotation == other.mRotation &&
- mSurfaceSetID == other.mSurfaceSetID);
+ mSurfaceSetID == other.mSurfaceSetID &&
+ mSurfaceType == other.mSurfaceType &&
+ mWidth == other.mWidth &&
+ mHeight == other.mHeight);
}
bool operator != (const OutputConfiguration& other) const {
return !(*this == other);
if (mSurfaceSetID != other.mSurfaceSetID) {
return mSurfaceSetID < other.mSurfaceSetID;
}
+ if (mSurfaceType != other.mSurfaceType) {
+ return mSurfaceType < other.mSurfaceType;
+ }
+ if (mWidth != other.mWidth) {
+ return mWidth < other.mWidth;
+ }
+ if (mHeight != other.mHeight) {
+ return mHeight < other.mHeight;
+ }
+
return mRotation < other.mRotation;
}
bool operator > (const OutputConfiguration& other) const {
sp<IGraphicBufferProducer> mGbp;
int mRotation;
int mSurfaceSetID;
+ int mSurfaceType;
+ int mWidth;
+ int mHeight;
// helper function
static String16 readMaybeEmptyString16(const Parcel* parcel);
};
*/
uint32_t getInputFramesLost() const;
+ /* Get the flags */
+ audio_input_flags_t getFlags() const { AutoMutex _l(mLock); return mFlags; }
+
private:
/* copying audio record objects is not allowed */
AudioRecord(const AudioRecord& other);
public:
DECLARE_META_INTERFACE(AudioFlinger);
- // or-able bits shared by createTrack and openRecord, but not all combinations make sense
- enum {
- TRACK_DEFAULT = 0, // client requests a default AudioTrack
- // FIXME: obsolete
- // TRACK_TIMED= 1, // client requests a TimedAudioTrack
- TRACK_FAST = 2, // client requests a fast AudioTrack or AudioRecord
- TRACK_OFFLOAD = 4, // client requests offload to hw codec
- TRACK_DIRECT = 8, // client requests a direct output
- };
- typedef uint32_t track_flags_t;
// invariant on exit for all APIs that return an sp<>:
// (return value != 0) == (*status == NO_ERROR)
audio_format_t format,
audio_channel_mask_t channelMask,
size_t *pFrameCount,
- track_flags_t *flags,
+ audio_output_flags_t *flags,
const sp<IMemory>& sharedBuffer,
// On successful return, AudioFlinger takes over the handle
// reference and will release it when the track is destroyed.
audio_channel_mask_t channelMask,
const String16& callingPackage,
size_t *pFrameCount,
- track_flags_t *flags,
+ audio_input_flags_t *flags,
pid_t pid,
pid_t tid, // -1 means unused, otherwise must be valid non-0
int clientUid,
#define IMEDIA_SOURCE_BASE_H_
+#include <map>
+
#include <binder/IInterface.h>
+#include <binder/IMemory.h>
+#include <media/stagefright/MediaBuffer.h>
#include <media/stagefright/MediaErrors.h>
namespace android {
struct MediaSource;
class MetaData;
-class MediaBuffer;
class MediaBufferGroup;
class IMediaSource : public IInterface {
// a) not request a seek
// b) not be late, i.e. lateness_us = 0
struct ReadOptions {
- enum SeekMode {
+ enum SeekMode : int32_t {
SEEK_PREVIOUS_SYNC,
SEEK_NEXT_SYNC,
SEEK_CLOSEST_SYNC,
void clearSeekTo();
bool getSeekTo(int64_t *time_us, SeekMode *mode) const;
+ // TODO: remove this if unused.
void setLateBy(int64_t lateness_us);
int64_t getLateBy() const;
void clearNonBlocking();
bool getNonBlocking() const;
+ // Used to clear all non-persistent options for multiple buffer reads.
+ void clearNonPersistent() {
+ clearSeekTo();
+ }
+
private:
enum Options {
kSeekTo_Option = 1,
// A result of INFO_FORMAT_CHANGED indicates that the format of this
// MediaSource has changed mid-stream, the client can continue reading
// but should be prepared for buffers of the new configuration.
+ //
+ // TODO: consider removing read() in favor of readMultiple().
virtual status_t read(
MediaBuffer **buffer, const ReadOptions *options = NULL) = 0;
- // Returns a vector of new buffers of data. The vector size could be
- // <= |maxNumBuffers|. Used for buffers with small size
- // since all buffer data are passed back by binder, not shared memory.
+ // Returns a vector of new buffers of data, where the new buffers are added
+ // to the end of the vector.
// Call blocks until an error is encountered, or the end of the stream is
// reached, or format change is hit, or |kMaxNumReadMultiple| buffers have
// been read.
- // End of stream is signalled by a result of ERROR_END_OF_STREAM.
+ // End of stream is signaled by a result of ERROR_END_OF_STREAM.
// A result of INFO_FORMAT_CHANGED indicates that the format of this
// MediaSource has changed mid-stream, the client can continue reading
// but should be prepared for buffers of the new configuration.
+ //
+ // ReadOptions may be specified. Persistent options apply to all reads;
+ // non-persistent options (e.g. seek) apply only to the first read.
virtual status_t readMultiple(
- Vector<MediaBuffer *> *buffers, uint32_t maxNumBuffers = 1) = 0;
+ Vector<MediaBuffer *> *buffers, uint32_t maxNumBuffers = 1,
+ const ReadOptions *options = nullptr) = 0;
+
+ // Returns true if |readMultiple| is supported, otherwise false.
+ virtual bool supportReadMultiple() = 0;
+
+ // Returns true if |read| supports nonblocking option, otherwise false.
+ // |readMultiple| if supported, always allows the nonblocking option.
+ virtual bool supportNonblockingRead() = 0;
// Causes this source to suspend pulling data from its upstream source
// until a subsequent read-with-seek. Currently only supported by
return ERROR_UNSUPPORTED;
}
+ // TODO: Implement this for local media sources.
virtual status_t readMultiple(
- Vector<MediaBuffer *> * /* buffers */, uint32_t /* maxNumBuffers = 1 */) {
+ Vector<MediaBuffer *> * /* buffers */, uint32_t /* maxNumBuffers = 1 */,
+ const ReadOptions * /* options = nullptr */) {
return ERROR_UNSUPPORTED;
}
+
+ virtual bool supportReadMultiple() {
+ return false;
+ }
+
+ // Override in source if nonblocking reads are supported.
+ virtual bool supportNonblockingRead() {
+ return false;
+ }
+
+ static const size_t kBinderMediaBuffers = 4; // buffers managed by BnMediaSource
+ static const size_t kTransferSharedAsSharedThreshold = 4 * 1024; // if >= shared, else inline
+ static const size_t kTransferInlineAsSharedThreshold = 64 * 1024; // if >= shared, else inline
+ static const size_t kInlineMaxTransfer = 256 * 1024; // Binder size limited to BINDER_VM_SIZE.
+
protected:
virtual ~BnMediaSource();
private:
- MediaBufferGroup *mGroup;
-};
+ uint32_t mBuffersSinceStop; // Buffer tracking variable
+
+ std::unique_ptr<MediaBufferGroup> mGroup;
+
+ // To prevent marshalling IMemory with each read transaction, we cache the IMemory pointer
+ // into a map.
+ //
+ // This is converted into an index, which is used to identify the associated memory
+ // on the receiving side. We hold a reference to the IMemory here to ensure it doesn't
+ // change underneath us.
+
+ struct IndexCache {
+ IndexCache() : mIndex(0) { }
+
+ // Returns the index of the IMemory stored in cache or 0 if not found.
+ uint64_t lookup(const sp<IMemory> &mem) {
+ auto p = mMemoryToIndex.find(mem.get());
+ if (p == mMemoryToIndex.end()) {
+ return 0;
+ }
+ if (MediaBuffer::isDeadObject(p->second.first)) {
+ // this object's dead
+ ALOGW("Attempting to lookup a dead IMemory");
+ (void)mMemoryToIndex.erase(p);
+ return 0;
+ }
+ ALOGW_IF(p->second.first.get() != mem.get(), "Mismatched buffers without reset");
+ return p->second.second;
+ }
+
+ // Returns the index of the IMemory stored in the index cache.
+ uint64_t insert(const sp<IMemory> &mem) {
+ auto p = mMemoryToIndex.find(mem.get());
+ if (p == mMemoryToIndex.end()) {
+ if (mIndex == UINT64_MAX) {
+ ALOGE("Index overflow");
+ mIndex = 1; // skip overflow condition and hope for the best
+ } else {
+ ++mIndex;
+ }
+ (void)mMemoryToIndex.emplace(// C++11 mem.get(), std::make_pair(mem, mIndex))
+ std::piecewise_construct,
+ std::forward_as_tuple(mem.get()), std::forward_as_tuple(mem, mIndex));
+ return mIndex;
+ }
+ ALOGW("IMemory already inserted into cache");
+ return p->second.second;
+ }
+
+ void reset() {
+ mMemoryToIndex.clear();
+ mIndex = 0;
+ }
+
+ void gc() {
+ for (auto it = mMemoryToIndex.begin(); it != mMemoryToIndex.end(); ) {
+ if (MediaBuffer::isDeadObject(it->second.first)) {
+ it = mMemoryToIndex.erase(it);
+ } else {
+ ++it;
+ }
+ }
+ }
+ private:
+ uint64_t mIndex;
+ // C++14 unordered_map erase on iterator is stable; C++11 has no guarantee.
+ // Could key on uintptr_t instead of IMemory *
+ std::map<IMemory *, std::pair<sp<IMemory>, uint64_t>> mMemoryToIndex;
+ } mIndexCache;
+};
} // namespace android
INTERNAL_OPTION_START_TIME, // data is an int64_t
INTERNAL_OPTION_TIME_LAPSE, // data is an int64_t[2]
INTERNAL_OPTION_COLOR_ASPECTS, // data is ColorAspects
+ INTERNAL_OPTION_TIME_OFFSET, // data is an int64_t
};
virtual status_t setInternalOption(
node_id node,
TONE_JAPAN_RADIO_ACK, // Radio path acknowlegment: 400Hz, 1s ON, 2s OFF...
// UK Supervisory tones
TONE_UK_RINGTONE, // Ring Tone: A 400Hz + 450Hz tone repeated in a 0.4s on, 0.2s off, 0.4s on, 2.0s off pattern.
+ // AUSTRALIA Supervisory tones
+ TONE_AUSTRALIA_RINGTONE, // Ring tone: A 400Hz + 450Hz tone repeated in a 0.4s on, 0.2s off, 0.4s on, 2.0s off pattern.
+ TONE_AUSTRALIA_BUSY, // Busy tone: 425 Hz repeated in a 0.375s on, 0.375s off pattern.
+ TONE_AUSTRALIA_CALL_WAITING,// Call waiting tone: 425Hz tone repeated in a 0.2s on, 0.2s off, 0.2s on, 4.4s off pattern.
+ TONE_AUSTRALIA_CONGESTION, // Congestion tone: 425Hz tone repeated in a 0.375s on, 0.375s off pattern
NUM_ALTERNATE_TONES
};
ANSI,
JAPAN,
UK,
+ AUSTRALIA,
CEPT,
NUM_REGIONS
};
int32_t width, int32_t height,
OMX_VIDEO_CODINGTYPE compressionFormat, float frameRate = -1.0);
+ // sets |portIndex| port buffer numbers to be |bufferNum|. NOTE: Component could reject
+ // this setting if the |bufferNum| is less than the minimum buffer num of the port.
+ status_t setPortBufferNum(OMX_U32 portIndex, int bufferNum);
+
// gets index or sets it to 0 on error. Returns error from codec.
status_t initDescribeColorAspectsIndex();
status_t getIntraRefreshPeriod(uint32_t *intraRefreshPeriod);
status_t setIntraRefreshPeriod(uint32_t intraRefreshPeriod, bool inConfigure);
+ // Configures temporal layering based on |msg|. |inConfigure| shall be true iff this is called
+ // during configure() call. on success the configured layering is set in |outputFormat|. If
+ // |outputFormat| is mOutputFormat, it is copied to trigger an output format changed event.
+ status_t configureTemporalLayers(
+ const sp<AMessage> &msg, bool inConfigure, sp<AMessage> &outputFormat);
+
status_t setMinBufferSize(OMX_U32 portIndex, size_t size);
status_t setupMPEG4EncoderParameters(const sp<AMessage> &msg);
status_t setupH263EncoderParameters(const sp<AMessage> &msg);
status_t setupAVCEncoderParameters(const sp<AMessage> &msg);
status_t setupHEVCEncoderParameters(const sp<AMessage> &msg);
- status_t setupVPXEncoderParameters(const sp<AMessage> &msg);
+ status_t setupVPXEncoderParameters(const sp<AMessage> &msg, sp<AMessage> &outputFormat);
status_t verifySupportForProfileAndLevel(int32_t profile, int32_t level);
status_t convertYUV420Planar(
const BitmapParams &src, const BitmapParams &dst);
+ status_t convertYUV420PlanarUseLibYUV(
+ const BitmapParams &src, const BitmapParams &dst);
+
status_t convertQCOMYUV420SemiPlanar(
const BitmapParams &src, const BitmapParams &dst);
status_t setGeoData(int latitudex10000, int longitudex10000);
status_t setCaptureRate(float captureFps);
+ status_t setTemporalLayerCount(uint32_t layerCount);
virtual void setStartTimeOffsetMs(int ms) { mStartTimeOffsetMs = ms; }
virtual int32_t getStartTimeOffsetMs() const { return mStartTimeOffsetMs; }
// Acquire lock before calling these methods
off64_t addSample_l(MediaBuffer *buffer);
off64_t addLengthPrefixedSample_l(MediaBuffer *buffer);
+ off64_t addMultipleLengthPrefixedSamples_l(MediaBuffer *buffer);
bool exceedsFileSizeLimit();
bool use32BitFileOffset() const;
#define MEDIA_BUFFER_H_
+#include <atomic>
+#include <list>
#include <media/stagefright/foundation/MediaBufferBase.h>
#include <pthread.h>
MediaBuffer(const sp<ABuffer> &buffer);
+ MediaBuffer(const sp<IMemory> &mem) :
+ MediaBuffer((uint8_t *)mem->pointer() + sizeof(SharedControl), mem->size()) {
+ // delegate and override mMemory
+ mMemory = mem;
+ }
+
// Decrements the reference count and returns the buffer to its
// associated MediaBufferGroup if the reference count drops to 0.
virtual void release();
int refcount() const;
+ bool isDeadObject() const {
+ return isDeadObject(mMemory);
+ }
+
+ static bool isDeadObject(const sp<IMemory> &memory) {
+ if (memory.get() == nullptr || memory->pointer() == nullptr) return false;
+ return reinterpret_cast<SharedControl *>(memory->pointer())->isDeadObject();
+ }
+
+ // Sticky on enabling of shared memory MediaBuffers. By default we don't use
+ // shared memory for MediaBuffers, but we enable this for those processes
+ // that export MediaBuffers.
+ static void useSharedMemory() {
+ std::atomic_store_explicit(
+ &mUseSharedMemory, (int_least32_t)1, std::memory_order_seq_cst);
+ }
+
protected:
+ // MediaBuffer remote releases are handled through a
+ // pending release count variable stored in a SharedControl block
+ // at the start of the IMemory.
+
+ // Returns old value of pending release count.
+ inline int32_t addPendingRelease(int32_t value) {
+ return getSharedControl()->addPendingRelease(value);
+ }
+
+ // Issues all pending releases (works in parallel).
+ // Assumes there is a MediaBufferObserver.
+ inline void resolvePendingRelease() {
+ if (mMemory.get() == nullptr) return;
+ while (addPendingRelease(-1) > 0) {
+ release();
+ }
+ addPendingRelease(1);
+ }
+
+ // true if MediaBuffer is observed (part of a MediaBufferGroup).
+ inline bool isObserved() const {
+ return mObserver != nullptr;
+ }
+
virtual ~MediaBuffer();
+ sp<IMemory> mMemory;
+
private:
friend class MediaBufferGroup;
friend class OMXDecoder;
void claim();
MediaBufferObserver *mObserver;
- MediaBuffer *mNextBuffer;
int mRefCount;
void *mData;
MediaBuffer *mOriginal;
- void setNextBuffer(MediaBuffer *buffer);
- MediaBuffer *nextBuffer();
+ static std::atomic_int_least32_t mUseSharedMemory;
MediaBuffer(const MediaBuffer &);
MediaBuffer &operator=(const MediaBuffer &);
- sp<IMemory> mMemory;
+
+ // SharedControl block at the start of IMemory.
+ struct SharedControl {
+ enum {
+ FLAG_DEAD_OBJECT = (1 << 0),
+ };
+
+ // returns old value
+ inline int32_t addPendingRelease(int32_t value) {
+ return std::atomic_fetch_add_explicit(
+ &mPendingRelease, (int_least32_t)value, std::memory_order_seq_cst);
+ }
+
+ inline int32_t getPendingRelease() const {
+ return std::atomic_load_explicit(&mPendingRelease, std::memory_order_seq_cst);
+ }
+
+ inline void setPendingRelease(int32_t value) {
+ std::atomic_store_explicit(
+ &mPendingRelease, (int_least32_t)value, std::memory_order_seq_cst);
+ }
+
+ inline bool isDeadObject() const {
+ return (std::atomic_load_explicit(
+ &mFlags, std::memory_order_seq_cst) & FLAG_DEAD_OBJECT) != 0;
+ }
+
+ inline void setDeadObject() {
+ (void)std::atomic_fetch_or_explicit(
+ &mFlags, (int_least32_t)FLAG_DEAD_OBJECT, std::memory_order_seq_cst);
+ }
+
+ inline void clear() {
+ std::atomic_store_explicit(
+ &mFlags, (int_least32_t)0, std::memory_order_seq_cst);
+ std::atomic_store_explicit(
+ &mPendingRelease, (int_least32_t)0, std::memory_order_seq_cst);
+ }
+
+ private:
+ // Caution: atomic_int_fast32_t is 64 bits on LP64.
+ std::atomic_int_least32_t mFlags;
+ std::atomic_int_least32_t mPendingRelease;
+ int32_t unused[6]; // additional buffer space
+ };
+
+ inline SharedControl *getSharedControl() const {
+ return reinterpret_cast<SharedControl *>(mMemory->pointer());
+ }
};
} // namespace android
class MediaBufferGroup : public MediaBufferObserver {
public:
- MediaBufferGroup();
+ MediaBufferGroup(size_t growthLimit = 0);
+
+ // create a media buffer group with preallocated buffers
+ MediaBufferGroup(size_t buffers, size_t buffer_size, size_t growthLimit = 0);
+
~MediaBufferGroup();
void add_buffer(MediaBuffer *buffer);
+ bool has_buffers();
+
// If nonBlocking is false, it blocks until a buffer is available and
// passes it to the caller in *buffer, while returning OK.
// The returned buffer will have a reference count of 1.
status_t acquire_buffer(
MediaBuffer **buffer, bool nonBlocking = false, size_t requestedSize = 0);
+ size_t buffers() const { return mBuffers.size(); }
+
+ // freeBuffers is the number of free buffers allowed to remain.
+ void gc(size_t freeBuffers = 0);
+
protected:
virtual void signalBufferReturned(MediaBuffer *buffer);
Mutex mLock;
Condition mCondition;
-
- MediaBuffer *mFirstBuffer, *mLastBuffer;
+ size_t mGrowthLimit; // Do not automatically grow group larger than this.
+ std::list<MediaBuffer *> mBuffers;
MediaBufferGroup(const MediaBufferGroup &);
MediaBufferGroup &operator=(const MediaBufferGroup &);
bool isVideo() const { return mIsVideo; }
sp<IGraphicBufferProducer> getGraphicBufferProducer();
- void setInputBufferTimeOffset(int64_t timeOffsetUs);
+ status_t setInputBufferTimeOffset(int64_t timeOffsetUs);
+ int64_t getFirstSampleSystemTimeUs();
// MediaSource
virtual status_t start(MetaData *params = NULL);
kWhatStop,
kWhatPause,
kWhatSetInputBufferTimeOffset,
+ kWhatGetFirstSampleSystemTimeUs,
kWhatStopStalled,
};
uint32_t flags = 0);
status_t onStart(MetaData *params);
+ void onPause();
status_t init();
status_t initEncoder();
void releaseEncoder();
List<size_t> mAvailEncoderInputIndices;
List<int64_t> mDecodingTimeQueue; // decoding time (us) for video
int64_t mInputBufferTimeOffsetUs;
+ int64_t mFirstSampleSystemTimeUs;
+ bool mPausePending;
// audio drift time
int64_t mFirstSampleTimeUs;
// transfer Function, value defined by ColorAspects.Transfer.
kKeyColorMatrix = 'cMtx', // int32_t,
// color Matrix, value defined by ColorAspects.MatrixCoeffs.
+ kKeyTemporalLayerId = 'iLyr', // int32_t, temporal layer-id. 0-based (0 => base layer)
+ kKeyTemporalLayerCount = 'cLyr', // int32_t, number of temporal layers encoded
};
enum {
bool findBuffer(const char *name, sp<ABuffer> *buffer) const;
bool findMessage(const char *name, sp<AMessage> *obj) const;
+ // finds any numeric type cast to a float
+ bool findAsFloat(const char *name, float *value) const;
+
bool findRect(
const char *name,
int32_t *left, int32_t *top, int32_t *right, int32_t *bottom) const;
# media gid needed for /dev/fm (radio) and for /data/misc/media (tee)
group audio camera drmrpc inet media mediadrm net_bt net_bt_admin net_bw_acct
ioprio rt 4
- writepid /dev/cpuset/foreground/tasks
+ writepid /dev/cpuset/foreground/tasks /dev/stune/foreground/tasks
libutils \
liblog
+LOCAL_CFLAGS := -Wall -Werror
+
include $(BUILD_SHARED_LIBRARY)
}
}
-void CCHelper::CommonClockListener::onTimelineChanged(uint64_t timelineID) {
+void CCHelper::CommonClockListener::onTimelineChanged(uint64_t timelineID __unused) {
// do nothing; listener is only really used as a token so the server can
// find out when clients die.
}
size_t notificationFrames = mNotificationFramesReq;
size_t frameCount = mReqFrameCount;
- IAudioFlinger::track_flags_t trackFlags = IAudioFlinger::TRACK_DEFAULT;
+ audio_input_flags_t flags = mFlags;
pid_t tid = -1;
if (mFlags & AUDIO_INPUT_FLAG_FAST) {
- trackFlags |= IAudioFlinger::TRACK_FAST;
if (mAudioRecordThread != 0) {
tid = mAudioRecordThread->getTid();
}
mChannelMask,
opPackageName,
&temp,
- &trackFlags,
+ &flags,
mClientPid,
tid,
mClientUid,
mAwaitBoost = false;
if (mFlags & AUDIO_INPUT_FLAG_FAST) {
- if (trackFlags & IAudioFlinger::TRACK_FAST) {
+ if (flags & AUDIO_INPUT_FLAG_FAST) {
ALOGI("AUDIO_INPUT_FLAG_FAST successful; frameCount %zu", frameCount);
mAwaitBoost = true;
} else {
continue; // retry
}
}
+ mFlags = flags;
if (iMem == 0) {
ALOGE("Could not get control block");
if (read > 0) {
break;
}
+ if (err == TIMED_OUT || err == -EINTR) {
+ err = WOULD_BLOCK;
+ }
return ssize_t(err);
}
int32_t tryCounter = kMaxTries;
uint32_t pollUs = 10000;
do {
- int policy = sched_getscheduler(0);
+ int policy = sched_getscheduler(0) & ~SCHED_RESET_ON_FORK;
if (policy == SCHED_FIFO || policy == SCHED_RR) {
break;
}
}
}
- IAudioFlinger::track_flags_t trackFlags = IAudioFlinger::TRACK_DEFAULT;
+ audio_output_flags_t flags = mFlags;
pid_t tid = -1;
if (mFlags & AUDIO_OUTPUT_FLAG_FAST) {
- trackFlags |= IAudioFlinger::TRACK_FAST;
if (mAudioTrackThread != 0 && !mThreadCanCallJava) {
tid = mAudioTrackThread->getTid();
}
}
- if (mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
- trackFlags |= IAudioFlinger::TRACK_OFFLOAD;
- }
-
- if (mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
- trackFlags |= IAudioFlinger::TRACK_DIRECT;
- }
-
size_t temp = frameCount; // temp may be replaced by a revised value of frameCount,
// but we will still need the original value also
audio_session_t originalSessionId = mSessionId;
mFormat,
mChannelMask,
&temp,
- &trackFlags,
+ &flags,
mSharedBuffer,
output,
mClientPid,
mAwaitBoost = false;
if (mFlags & AUDIO_OUTPUT_FLAG_FAST) {
- if (trackFlags & IAudioFlinger::TRACK_FAST) {
+ if (flags & AUDIO_OUTPUT_FLAG_FAST) {
ALOGV("AUDIO_OUTPUT_FLAG_FAST successful; frameCount %zu", frameCount);
if (!mThreadCanCallJava) {
mAwaitBoost = true;
}
} else {
ALOGW("AUDIO_OUTPUT_FLAG_FAST denied by server; frameCount %zu", frameCount);
- mFlags = (audio_output_flags_t) (mFlags & ~AUDIO_OUTPUT_FLAG_FAST);
}
}
+ mFlags = flags;
// Make sure that application is notified with sufficient margin before underrun.
// The client can divide the AudioTrack buffer into sub-buffers,
// and expresses its desire to server as the notification frame count.
if (mSharedBuffer == 0 && audio_is_linear_pcm(mFormat)) {
size_t maxNotificationFrames;
- if (trackFlags & IAudioFlinger::TRACK_FAST) {
+ if (mFlags & AUDIO_OUTPUT_FLAG_FAST) {
// notify every HAL buffer, regardless of the size of the track buffer
maxNotificationFrames = afFrameCountHAL;
} else {
if (written > 0) {
break;
}
+ if (err == TIMED_OUT || err == -EINTR) {
+ err = WOULD_BLOCK;
+ }
return ssize_t(err);
}
int32_t tryCounter = kMaxTries;
uint32_t pollUs = 10000;
do {
- int policy = sched_getscheduler(0);
+ int policy = sched_getscheduler(0) & ~SCHED_RESET_ON_FORK;
if (policy == SCHED_FIFO || policy == SCHED_RR) {
break;
}
audio_format_t format,
audio_channel_mask_t channelMask,
size_t *pFrameCount,
- track_flags_t *flags,
+ audio_output_flags_t *flags,
const sp<IMemory>& sharedBuffer,
audio_io_handle_t output,
pid_t pid,
data.writeInt32(channelMask);
size_t frameCount = pFrameCount != NULL ? *pFrameCount : 0;
data.writeInt64(frameCount);
- track_flags_t lFlags = flags != NULL ? *flags : (track_flags_t) TRACK_DEFAULT;
+ audio_output_flags_t lFlags = flags != NULL ? *flags : AUDIO_OUTPUT_FLAG_NONE;
data.writeInt32(lFlags);
// haveSharedBuffer
if (sharedBuffer != 0) {
if (pFrameCount != NULL) {
*pFrameCount = frameCount;
}
- lFlags = reply.readInt32();
+ lFlags = (audio_output_flags_t)reply.readInt32();
if (flags != NULL) {
*flags = lFlags;
}
audio_channel_mask_t channelMask,
const String16& opPackageName,
size_t *pFrameCount,
- track_flags_t *flags,
+ audio_input_flags_t *flags,
pid_t pid,
pid_t tid,
int clientUid,
data.writeString16(opPackageName);
size_t frameCount = pFrameCount != NULL ? *pFrameCount : 0;
data.writeInt64(frameCount);
- track_flags_t lFlags = flags != NULL ? *flags : (track_flags_t) TRACK_DEFAULT;
+ audio_input_flags_t lFlags = flags != NULL ? *flags : AUDIO_INPUT_FLAG_NONE;
data.writeInt32(lFlags);
data.writeInt32((int32_t) pid);
data.writeInt32((int32_t) tid);
if (pFrameCount != NULL) {
*pFrameCount = frameCount;
}
- lFlags = reply.readInt32();
+ lFlags = (audio_input_flags_t)reply.readInt32();
if (flags != NULL) {
*flags = lFlags;
}
audio_format_t format = (audio_format_t) data.readInt32();
audio_channel_mask_t channelMask = data.readInt32();
size_t frameCount = data.readInt64();
- track_flags_t flags = (track_flags_t) data.readInt32();
+ audio_output_flags_t flags = (audio_output_flags_t) data.readInt32();
bool haveSharedBuffer = data.readInt32() != 0;
sp<IMemory> buffer;
if (haveSharedBuffer) {
audio_channel_mask_t channelMask = data.readInt32();
const String16& opPackageName = data.readString16();
size_t frameCount = data.readInt64();
- track_flags_t flags = (track_flags_t) data.readInt32();
+ audio_input_flags_t flags = (audio_input_flags_t) data.readInt32();
pid_t pid = (pid_t) data.readInt32();
pid_t tid = (pid_t) data.readInt32();
int clientUid = data.readInt32();
ALOGE("Error reading source from parcel");
return ret;
}
+ // If we make an extractor through Binder, enabled shared memory
+ // for MediaBuffers for this process.
+ MediaBuffer::useSharedMemory();
sp<IDataSource> source = interface_cast<IDataSource>(b);
const char *mime = data.readCString();
sp<IMediaExtractor> ex = makeExtractor(source, mime);
STOP,
PAUSE,
GETFORMAT,
- READ,
+ // READ, deprecated
READMULTIPLE,
- RELEASE_BUFFER
+ RELEASE_BUFFER,
+ SUPPORT_NONBLOCKING_READ,
};
enum {
NULL_BUFFER,
SHARED_BUFFER,
- INLINE_BUFFER
+ INLINE_BUFFER,
+ SHARED_BUFFER_INDEX,
};
-class RemoteMediaBufferReleaser : public BBinder {
+class RemoteMediaBufferWrapper : public MediaBuffer {
public:
- RemoteMediaBufferReleaser(MediaBuffer *buf, sp<BnMediaSource> owner) {
- mBuf = buf;
- mOwner = owner;
- }
- ~RemoteMediaBufferReleaser() {
- if (mBuf) {
- ALOGW("RemoteMediaBufferReleaser dtor called while still holding buffer");
- mBuf->release();
- }
- }
- virtual status_t onTransact( uint32_t code,
- const Parcel& data,
- Parcel* reply,
- uint32_t flags = 0) {
- if (code == RELEASE_BUFFER) {
- mBuf->release();
- mBuf = NULL;
- return OK;
- } else {
- return BBinder::onTransact(code, data, reply, flags);
- }
+ RemoteMediaBufferWrapper(const sp<IMemory> &mem)
+ : MediaBuffer(mem) {
+ ALOGV("RemoteMediaBufferWrapper: creating %p", this);
}
-private:
- MediaBuffer *mBuf;
- // Keep a ref to ensure MediaBuffer is released before the owner, i.e., BnMediaSource,
- // because BnMediaSource needs to delete MediaBufferGroup in its dtor and
- // MediaBufferGroup dtor requires all MediaBuffer's have 0 ref count.
- sp<BnMediaSource> mOwner;
-};
-
-class RemoteMediaBufferWrapper : public MediaBuffer {
-public:
- RemoteMediaBufferWrapper(sp<IMemory> mem, sp<IBinder> source);
protected:
- virtual ~RemoteMediaBufferWrapper();
-private:
- sp<IMemory> mMemory;
- sp<IBinder> mRemoteSource;
+ virtual ~RemoteMediaBufferWrapper() {
+ // Indicate to MediaBufferGroup to release.
+ int32_t old = addPendingRelease(1);
+ ALOGV("RemoteMediaBufferWrapper: releasing %p, old %d", this, old);
+ mMemory.clear(); // don't set the dead object flag.
+ }
};
-RemoteMediaBufferWrapper::RemoteMediaBufferWrapper(sp<IMemory> mem, sp<IBinder> source)
-: MediaBuffer(mem->pointer(), mem->size()) {
- mMemory = mem;
- mRemoteSource = source;
-}
-
-RemoteMediaBufferWrapper::~RemoteMediaBufferWrapper() {
- mMemory.clear();
- // Explicitly ask the remote side to release the buffer. We could also just clear
- // mRemoteSource, but that doesn't immediately release the reference on the remote side.
- Parcel data, reply;
- mRemoteSource->transact(RELEASE_BUFFER, data, &reply);
- mRemoteSource.clear();
-}
-
class BpMediaSource : public BpInterface<IMediaSource> {
public:
BpMediaSource(const sp<IBinder>& impl)
- : BpInterface<IMediaSource>(impl)
+ : BpInterface<IMediaSource>(impl), mBuffersSinceStop(0)
{
}
ALOGV("stop");
Parcel data, reply;
data.writeInterfaceToken(BpMediaSource::getInterfaceDescriptor());
- return remote()->transact(STOP, data, &reply);
+ status_t status = remote()->transact(STOP, data, &reply);
+ mMemoryCache.reset();
+ mBuffersSinceStop = 0;
+ return status;
}
virtual sp<MetaData> getFormat() {
}
virtual status_t read(MediaBuffer **buffer, const ReadOptions *options) {
- ALOGV("read");
- Parcel data, reply;
- data.writeInterfaceToken(BpMediaSource::getInterfaceDescriptor());
- if (options) {
- data.writeByteArray(sizeof(*options), (uint8_t*) options);
- }
- status_t ret = remote()->transact(READ, data, &reply);
- if (ret != NO_ERROR) {
- return ret;
- }
- // wrap the returned data in a MediaBuffer
- ret = reply.readInt32();
- int32_t buftype = reply.readInt32();
- if (buftype == SHARED_BUFFER) {
- sp<IBinder> remote = reply.readStrongBinder();
- sp<IBinder> binder = reply.readStrongBinder();
- sp<IMemory> mem = interface_cast<IMemory>(binder);
- if (mem == NULL) {
- ALOGE("received NULL IMemory for shared buffer");
- }
- size_t offset = reply.readInt32();
- size_t length = reply.readInt32();
- MediaBuffer *buf = new RemoteMediaBufferWrapper(mem, remote);
- buf->set_range(offset, length);
- buf->meta_data()->updateFromParcel(reply);
- *buffer = buf;
- } else if (buftype == NULL_BUFFER) {
- ALOGV("got status %d and NULL buffer", ret);
- *buffer = NULL;
- } else {
- int32_t len = reply.readInt32();
- ALOGV("got status %d and len %d", ret, len);
- *buffer = new MediaBuffer(len);
- reply.read((*buffer)->data(), len);
- (*buffer)->meta_data()->updateFromParcel(reply);
- }
+ Vector<MediaBuffer *> buffers;
+ status_t ret = readMultiple(&buffers, 1 /* maxNumBuffers */, options);
+ *buffer = buffers.size() == 0 ? nullptr : buffers[0];
+ ALOGV("read status %d, bufferCount %u, sinceStop %u",
+ ret, *buffer != nullptr, mBuffersSinceStop);
return ret;
}
- virtual status_t readMultiple(Vector<MediaBuffer *> *buffers, uint32_t maxNumBuffers) {
+ virtual status_t readMultiple(
+ Vector<MediaBuffer *> *buffers, uint32_t maxNumBuffers, const ReadOptions *options) {
ALOGV("readMultiple");
if (buffers == NULL || !buffers->isEmpty()) {
return BAD_VALUE;
Parcel data, reply;
data.writeInterfaceToken(BpMediaSource::getInterfaceDescriptor());
data.writeUint32(maxNumBuffers);
+ if (options != nullptr) {
+ data.writeByteArray(sizeof(*options), (uint8_t*) options);
+ }
status_t ret = remote()->transact(READMULTIPLE, data, &reply);
+ mMemoryCache.gc();
if (ret != NO_ERROR) {
return ret;
}
// wrap the returned data in a vector of MediaBuffers
- int32_t bufCount = 0;
- while (1) {
- if (reply.readInt32() == 0) {
- break;
+ int32_t buftype;
+ uint32_t bufferCount = 0;
+ while ((buftype = reply.readInt32()) != NULL_BUFFER) {
+ LOG_ALWAYS_FATAL_IF(bufferCount >= maxNumBuffers,
+ "Received %u+ buffers and requested %u buffers",
+ bufferCount + 1, maxNumBuffers);
+ MediaBuffer *buf;
+ if (buftype == SHARED_BUFFER || buftype == SHARED_BUFFER_INDEX) {
+ uint64_t index = reply.readUint64();
+ ALOGV("Received %s index %llu",
+ buftype == SHARED_BUFFER ? "SHARED_BUFFER" : "SHARED_BUFFER_INDEX",
+ (unsigned long long) index);
+ sp<IMemory> mem;
+ if (buftype == SHARED_BUFFER) {
+ sp<IBinder> binder = reply.readStrongBinder();
+ mem = interface_cast<IMemory>(binder);
+ LOG_ALWAYS_FATAL_IF(mem.get() == nullptr,
+ "Received NULL IMemory for shared buffer");
+ mMemoryCache.insert(index, mem);
+ } else {
+ mem = mMemoryCache.lookup(index);
+ LOG_ALWAYS_FATAL_IF(mem.get() == nullptr,
+ "Received invalid IMemory index for shared buffer: %llu",
+ (unsigned long long)index);
+ }
+ size_t offset = reply.readInt32();
+ size_t length = reply.readInt32();
+ buf = new RemoteMediaBufferWrapper(mem);
+ buf->set_range(offset, length);
+ buf->meta_data()->updateFromParcel(reply);
+ } else { // INLINE_BUFFER
+ int32_t len = reply.readInt32();
+ ALOGV("INLINE_BUFFER status %d and len %d", ret, len);
+ buf = new MediaBuffer(len);
+ reply.read(buf->data(), len);
+ buf->meta_data()->updateFromParcel(reply);
}
- int32_t len = reply.readInt32();
- ALOGV("got len %d", len);
- MediaBuffer *buf = new MediaBuffer(len);
- reply.read(buf->data(), len);
- buf->meta_data()->updateFromParcel(reply);
buffers->push_back(buf);
- ++bufCount;
+ ++bufferCount;
+ ++mBuffersSinceStop;
}
ret = reply.readInt32();
- ALOGV("got status %d, bufCount %d", ret, bufCount);
+ ALOGV("readMultiple status %d, bufferCount %u, sinceStop %u",
+ ret, bufferCount, mBuffersSinceStop);
return ret;
}
+ // Binder proxy adds readMultiple support.
+ virtual bool supportReadMultiple() {
+ return true;
+ }
+
+ virtual bool supportNonblockingRead() {
+ ALOGV("supportNonblockingRead");
+ Parcel data, reply;
+ data.writeInterfaceToken(BpMediaSource::getInterfaceDescriptor());
+ status_t ret = remote()->transact(SUPPORT_NONBLOCKING_READ, data, &reply);
+ if (ret == NO_ERROR) {
+ return reply.readInt32() != 0;
+ }
+ return false;
+ }
+
virtual status_t pause() {
ALOGV("pause");
Parcel data, reply;
}
private:
+
+ uint32_t mBuffersSinceStop; // Buffer tracking variable
+
// NuPlayer passes pointers-to-metadata around, so we use this to keep the metadata alive
// XXX: could we use this for caching, or does metadata change on the fly?
sp<MetaData> mMetaData;
+ // Cache all IMemory objects received from MediaExtractor.
+ // We gc IMemory objects that are no longer active (referenced by a MediaBuffer).
+
+ struct MemoryCache {
+ sp<IMemory> lookup(uint64_t index) {
+ auto p = mIndexToMemory.find(index);
+ if (p == mIndexToMemory.end()) {
+ ALOGE("cannot find index!");
+ return nullptr;
+ }
+ return p->second;
+ }
+
+ void insert(uint64_t index, const sp<IMemory> &mem) {
+ if (mIndexToMemory.find(index) != mIndexToMemory.end()) {
+ ALOGE("index %llu already present", (unsigned long long)index);
+ return;
+ }
+ (void)mIndexToMemory.emplace(index, mem);
+ }
+
+ void reset() {
+ mIndexToMemory.clear();
+ }
+
+ void gc() {
+ for (auto it = mIndexToMemory.begin(); it != mIndexToMemory.end(); ) {
+ if (MediaBuffer::isDeadObject(it->second)) {
+ it = mIndexToMemory.erase(it);
+ } else {
+ ++it;
+ }
+ }
+ }
+ private:
+ // C++14 unordered_map erase on iterator is stable; C++11 has no guarantee.
+ std::map<uint64_t, sp<IMemory>> mIndexToMemory;
+ } mMemoryCache;
};
IMPLEMENT_META_INTERFACE(MediaSource, "android.media.IMediaSource");
#define LOG_TAG "BnMediaSource"
BnMediaSource::BnMediaSource()
- : mGroup(NULL) {
+ : mBuffersSinceStop(0)
+ , mGroup(new MediaBufferGroup(kBinderMediaBuffers /* growthLimit */)) {
}
BnMediaSource::~BnMediaSource() {
- delete mGroup;
- mGroup = NULL;
}
status_t BnMediaSource::onTransact(
case STOP: {
ALOGV("stop");
CHECK_INTERFACE(IMediaSource, data, reply);
- return stop();
+ status_t status = stop();
+ mGroup->gc();
+ mIndexCache.reset();
+ mBuffersSinceStop = 0;
+ return status;
}
case PAUSE: {
ALOGV("pause");
}
return UNKNOWN_ERROR;
}
- case READ: {
- ALOGV("read");
+ case READMULTIPLE: {
+ ALOGV("readMultiple");
CHECK_INTERFACE(IMediaSource, data, reply);
- status_t ret;
- MediaBuffer *buf = NULL;
+
+ // Get max number of buffers to read.
+ uint32_t maxNumBuffers;
+ data.readUint32(&maxNumBuffers);
+ if (maxNumBuffers > kMaxNumReadMultiple) {
+ maxNumBuffers = kMaxNumReadMultiple;
+ }
+
+ // Get read options, if any.
ReadOptions opts;
uint32_t len;
- if (data.readUint32(&len) == NO_ERROR &&
- len == sizeof(opts) && data.read((void*)&opts, len) == NO_ERROR) {
- ret = read(&buf, &opts);
- } else {
- ret = read(&buf, NULL);
- }
+ const bool useOptions =
+ data.readUint32(&len) == NO_ERROR
+ && len == sizeof(opts)
+ && data.read((void *)&opts, len) == NO_ERROR;
+
+ mGroup->gc(kBinderMediaBuffers /* freeBuffers */);
+ mIndexCache.gc();
+ size_t inlineTransferSize = 0;
+ status_t ret = NO_ERROR;
+ uint32_t bufferCount = 0;
+ for (; bufferCount < maxNumBuffers; ++bufferCount, ++mBuffersSinceStop) {
+ MediaBuffer *buf = nullptr;
+ ret = read(&buf, useOptions ? &opts : nullptr);
+ opts.clearNonPersistent(); // Remove options that only apply to first buffer.
+ if (ret != NO_ERROR || buf == nullptr) {
+ break;
+ }
- reply->writeInt32(ret);
- if (buf != NULL) {
- size_t usedSize = buf->range_length();
- // even if we're using shared memory, we might not want to use it, since for small
+ // Even if we're using shared memory, we might not want to use it, since for small
// sizes it's faster to copy data through the Binder transaction
// On the other hand, if the data size is large enough, it's better to use shared
// memory. When data is too large, binder can't handle it.
- if (usedSize >= MediaBuffer::kSharedMemThreshold) {
- ALOGV("use shared memory: %zu", usedSize);
-
- MediaBuffer *transferBuf = buf;
- size_t offset = buf->range_offset();
- if (transferBuf->mMemory == NULL) {
- if (mGroup == NULL) {
- mGroup = new MediaBufferGroup;
- size_t allocateSize = usedSize;
- if (usedSize < SIZE_MAX / 3) {
- allocateSize = usedSize * 3 / 2;
- }
- mGroup->add_buffer(new MediaBuffer(allocateSize));
- }
-
- MediaBuffer *newBuf = NULL;
+ //
+ // TODO: reduce MediaBuffer::kSharedMemThreshold
+ MediaBuffer *transferBuf = nullptr;
+ const size_t length = buf->range_length();
+ size_t offset = buf->range_offset();
+ if (length >= (supportNonblockingRead() && buf->mMemory != nullptr ?
+ kTransferSharedAsSharedThreshold : kTransferInlineAsSharedThreshold)) {
+ if (buf->mMemory != nullptr) {
+ ALOGV("Use shared memory: %zu", length);
+ transferBuf = buf;
+ } else {
+ ALOGD("Large buffer %zu without IMemory!", length);
ret = mGroup->acquire_buffer(
- &newBuf, false /* nonBlocking */, usedSize);
- if (ret != OK || newBuf == NULL || newBuf->mMemory == NULL) {
- ALOGW("failed to acquire shared memory, ret %d", ret);
- buf->release();
- if (newBuf != NULL) {
- newBuf->release();
+ &transferBuf, false /* nonBlocking */, length);
+ if (ret != OK
+ || transferBuf == nullptr
+ || transferBuf->mMemory == nullptr) {
+ ALOGW("Failed to acquire shared memory, size %zu, ret %d",
+ length, ret);
+ if (transferBuf != nullptr) {
+ transferBuf->release();
+ transferBuf = nullptr;
+ }
+ // Current buffer transmit inline; no more additional buffers.
+ maxNumBuffers = 0;
+ } else {
+ memcpy(transferBuf->data(), (uint8_t*)buf->data() + offset, length);
+ offset = 0;
+ if (!mGroup->has_buffers()) {
+ maxNumBuffers = 0; // No more MediaBuffers, stop readMultiple.
}
- reply->writeInt32(NULL_BUFFER);
- return NO_ERROR;
}
- transferBuf = newBuf;
- memcpy(transferBuf->data(), (uint8_t*)buf->data() + buf->range_offset(),
- buf->range_length());
- offset = 0;
}
-
- reply->writeInt32(SHARED_BUFFER);
- RemoteMediaBufferReleaser *wrapper =
- new RemoteMediaBufferReleaser(transferBuf, this);
- reply->writeStrongBinder(wrapper);
- reply->writeStrongBinder(IInterface::asBinder(transferBuf->mMemory));
+ }
+ if (transferBuf != nullptr) { // Using shared buffers.
+ if (!transferBuf->isObserved()) {
+ // Transfer buffer must be part of a MediaBufferGroup.
+ ALOGV("adding shared memory buffer %p to local group", transferBuf);
+ mGroup->add_buffer(transferBuf);
+ transferBuf->add_ref(); // We have already acquired buffer.
+ }
+ uint64_t index = mIndexCache.lookup(transferBuf->mMemory);
+ if (index == 0) {
+ index = mIndexCache.insert(transferBuf->mMemory);
+ reply->writeInt32(SHARED_BUFFER);
+ reply->writeUint64(index);
+ reply->writeStrongBinder(IInterface::asBinder(transferBuf->mMemory));
+ ALOGV("SHARED_BUFFER(%p) %llu",
+ transferBuf, (unsigned long long)index);
+ } else {
+ reply->writeInt32(SHARED_BUFFER_INDEX);
+ reply->writeUint64(index);
+ ALOGV("SHARED_BUFFER_INDEX(%p) %llu",
+ transferBuf, (unsigned long long)index);
+ }
reply->writeInt32(offset);
- reply->writeInt32(usedSize);
+ reply->writeInt32(length);
buf->meta_data()->writeToParcel(*reply);
- if (buf->mMemory == NULL) {
+ if (transferBuf != buf) {
buf->release();
+ } else if (!supportNonblockingRead()) {
+ maxNumBuffers = 0; // stop readMultiple with one shared buffer.
}
} else {
- // buffer is small: copy it
- if (buf->mMemory != NULL) {
- ALOGV("%zu shared mem available, but only %zu used", buf->mMemory->size(), buf->range_length());
- }
+ ALOGV_IF(buf->mMemory != nullptr,
+ "INLINE(%p) %zu shared mem available, but only %zu used",
+ buf, buf->mMemory->size(), length);
reply->writeInt32(INLINE_BUFFER);
- reply->writeByteArray(buf->range_length(), (uint8_t*)buf->data() + buf->range_offset());
+ reply->writeByteArray(length, (uint8_t*)buf->data() + offset);
buf->meta_data()->writeToParcel(*reply);
buf->release();
+ inlineTransferSize += length;
+ if (inlineTransferSize > kInlineMaxTransfer) {
+ maxNumBuffers = 0; // stop readMultiple if inline transfer is too large.
+ }
}
- } else {
- ALOGV("ret %d, buf %p", ret, buf);
- reply->writeInt32(NULL_BUFFER);
}
+ reply->writeInt32(NULL_BUFFER); // Indicate no more MediaBuffers.
+ reply->writeInt32(ret);
+ ALOGV("readMultiple status %d, bufferCount %u, sinceStop %u",
+ ret, bufferCount, mBuffersSinceStop);
return NO_ERROR;
}
- case READMULTIPLE: {
- ALOGV("readmultiple");
+ case SUPPORT_NONBLOCKING_READ: {
+ ALOGV("supportNonblockingRead");
CHECK_INTERFACE(IMediaSource, data, reply);
- uint32_t maxNumBuffers;
- data.readUint32(&maxNumBuffers);
- status_t ret = NO_ERROR;
- uint32_t bufferCount = 0;
- if (maxNumBuffers > kMaxNumReadMultiple) {
- maxNumBuffers = kMaxNumReadMultiple;
- }
- while (bufferCount < maxNumBuffers) {
- if (reply->dataSize() >= MediaBuffer::kSharedMemThreshold) {
- break;
- }
-
- MediaBuffer *buf = NULL;
- ret = read(&buf, NULL);
- if (ret != NO_ERROR || buf == NULL) {
- break;
- }
- ++bufferCount;
- reply->writeInt32(1); // indicate one more MediaBuffer.
- reply->writeByteArray(
- buf->range_length(), (uint8_t*)buf->data() + buf->range_offset());
- buf->meta_data()->writeToParcel(*reply);
- buf->release();
- }
- reply->writeInt32(0); // indicate no more MediaBuffer.
- reply->writeInt32(ret);
+ reply->writeInt32((int32_t)supportNonblockingRead());
return NO_ERROR;
}
default:
{ .duration = 0, .waveFreq = { 0 }, 0, 0}},
.repeatCnt = ToneGenerator::TONEGEN_INF,
.repeatSegment = 0 }, // TONE_UK_RINGTONE
-
-
-
+ { .segments = { { .duration = 400, .waveFreq = { 400, 450, 0 }, 0, 0 },
+ { .duration = 200, .waveFreq = { 0 }, 0, 0 },
+ { .duration = 400, .waveFreq = { 400, 450, 0 }, 0, 0 },
+ { .duration = 2000, .waveFreq = { 0 }, 0, 0},
+ { .duration = 0, .waveFreq = { 0 }, 0, 0}},
+ .repeatCnt = ToneGenerator::TONEGEN_INF,
+ .repeatSegment = 0 }, // TONE_AUSTRALIA_RINGTONE
+ { .segments = { { .duration = 375, .waveFreq = { 425, 0 }, 0, 0 },
+ { .duration = 375, .waveFreq = { 0 }, 0, 0 },
+ { .duration = 0 , .waveFreq = { 0 }, 0, 0}},
+ .repeatCnt = ToneGenerator::TONEGEN_INF,
+ .repeatSegment = 0 }, // TONE_AUSTRALIA_BUSY
+ { .segments = { { .duration = 200, .waveFreq = { 425, 0 }, 0, 0 },
+ { .duration = 200, .waveFreq = { 0 }, 0, 0 },
+ { .duration = 200, .waveFreq = { 425, 0 }, 0, 0 },
+ { .duration = 4400, .waveFreq = { 0 }, 0, 0 },
+ { .duration = 0 , .waveFreq = { 0 }, 0, 0}},
+ .repeatCnt = ToneGenerator::TONEGEN_INF,
+ .repeatSegment = 0 }, // TONE_AUSTRALIA_CALL_WAITING
+ { .segments = { { .duration = 375, .waveFreq = { 425, 0 }, 0, 0 },
+ { .duration = 375, .waveFreq = { 0 }, 0, 0 },
+ { .duration = 0 , .waveFreq = { 0 }, 0, 0}},
+ .repeatCnt = ToneGenerator::TONEGEN_INF,
+ .repeatSegment = 0 }, // TONE_AUSTRALIA_CONGESTION
};
// Used by ToneGenerator::getToneForRegion() to convert user specified supervisory tone type
TONE_SUP_ERROR, // TONE_SUP_ERROR
TONE_SUP_CALL_WAITING, // TONE_SUP_CALL_WAITING
TONE_UK_RINGTONE // TONE_SUP_RINGTONE
+ },
+ { // AUSTRALIA
+ TONE_ANSI_DIAL, // TONE_SUP_DIAL
+ TONE_AUSTRALIA_BUSY, // TONE_SUP_BUSY
+ TONE_AUSTRALIA_CONGESTION, // TONE_SUP_CONGESTION
+ TONE_SUP_RADIO_ACK, // TONE_SUP_RADIO_ACK
+ TONE_SUP_RADIO_NOTAVAIL, // TONE_SUP_RADIO_NOTAVAIL
+ TONE_SUP_ERROR, // TONE_SUP_ERROR
+ TONE_AUSTRALIA_CALL_WAITING,// TONE_SUP_CALL_WAITING
+ TONE_AUSTRALIA_RINGTONE // TONE_SUP_RINGTONE
}
-
};
mProcessSize = (mSamplingRate * 20) / 1000;
char value[PROPERTY_VALUE_MAX];
- property_get("gsm.operator.iso-country", value, "");
- if (strcmp(value,"us") == 0 ||
- strcmp(value,"ca") == 0) {
+ if (property_get("gsm.operator.iso-country", value, "") == 0) {
+ property_get("gsm.sim.operator.iso-country", value, "");
+ }
+ if (strstr(value, "us") != NULL ||
+ strstr(value, "ca") != NULL) {
mRegion = ANSI;
- } else if (strcmp(value,"jp") == 0) {
+ } else if (strstr(value, "jp") != NULL) {
mRegion = JAPAN;
- } else if (strcmp(value,"uk") == 0 ||
- strcmp(value,"uk,uk") == 0) {
+ } else if (strstr(value, "uk") != NULL) {
mRegion = UK;
+ } else if (strstr(value, "au") != NULL) {
+ mRegion = AUSTRALIA;
} else {
mRegion = CEPT;
}
ALOGV("Delete Track: %p", mpAudioTrack.get());
mpAudioTrack.clear();
}
+ clearWaveGens();
}
////////////////////////////////////////////////////////////////////////////////
//
////////////////////////////////////////////////////////////////////////////////
bool ToneGenerator::initAudioTrack() {
-
- // Open audio track in mono, PCM 16bit, default sampling rate, default buffer size
+ // Open audio track in mono, PCM 16bit, default sampling rate.
mpAudioTrack = new AudioTrack();
- ALOGV("Create Track: %p", mpAudioTrack.get());
-
- mpAudioTrack->set(mStreamType,
- 0, // sampleRate
- AUDIO_FORMAT_PCM_16_BIT,
- AUDIO_CHANNEL_OUT_MONO,
- 0, // frameCount
- AUDIO_OUTPUT_FLAG_FAST,
- audioCallback,
- this, // user
- 0, // notificationFrames
- 0, // sharedBuffer
- mThreadCanCallJava,
- AUDIO_SESSION_ALLOCATE,
- AudioTrack::TRANSFER_CALLBACK);
-
- if (mpAudioTrack->initCheck() != NO_ERROR) {
- ALOGE("AudioTrack->initCheck failed");
- goto initAudioTrack_exit;
+ ALOGV("AudioTrack(%p) created", mpAudioTrack.get());
+
+ const size_t frameCount = mProcessSize;
+ status_t status = mpAudioTrack->set(
+ mStreamType,
+ 0, // sampleRate
+ AUDIO_FORMAT_PCM_16_BIT,
+ AUDIO_CHANNEL_OUT_MONO,
+ frameCount,
+ AUDIO_OUTPUT_FLAG_FAST,
+ audioCallback,
+ this, // user
+ 0, // notificationFrames
+ 0, // sharedBuffer
+ mThreadCanCallJava,
+ AUDIO_SESSION_ALLOCATE,
+ AudioTrack::TRANSFER_CALLBACK);
+
+ if (status != NO_ERROR) {
+ ALOGE("AudioTrack(%p) set failed with error %d", mpAudioTrack.get(), status);
+ mpAudioTrack.clear();
+ return false;
}
mpAudioTrack->setVolume(mVolume);
-
mState = TONE_INIT;
-
return true;
-
-initAudioTrack_exit:
-
- ALOGV("Init failed: %p", mpAudioTrack.get());
-
- // Cleanup
- mpAudioTrack.clear();
-
- return false;
}
-
////////////////////////////////////////////////////////////////////////////////
//
// Method: ToneGenerator::audioCallback()
return BAD_VALUE;
}
Mutex::Autolock _l(mLock);
- if (mPlayer == 0) return INVALID_OPERATION;
+ if (mPlayer == 0 || (mCurrentState & MEDIA_PLAYER_STOPPED)) {
+ return INVALID_OPERATION;
+ }
if (rate.mSpeed != 0.f && !(mCurrentState & MEDIA_PLAYER_STARTED)
&& (mCurrentState & (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_PAUSED
mCallbackData->setOutput(this);
}
delete newcbd;
- return OK;
+ return updateTrack();
}
}
mFrameSize = t->frameSize();
mTrack = t;
+ return updateTrack();
+}
+
+status_t MediaPlayerService::AudioOutput::updateTrack() {
+ if (mTrack == NULL) {
+ return NO_ERROR;
+ }
+
status_t res = NO_ERROR;
// Note some output devices may give us a direct track even though we don't specify it.
// Example: Line application b/17459982.
- if ((t->getFlags() & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT)) == 0) {
- res = t->setPlaybackRate(mPlaybackRate);
+ if ((mTrack->getFlags()
+ & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT)) == 0) {
+ res = mTrack->setPlaybackRate(mPlaybackRate);
if (res == NO_ERROR) {
- t->setAuxEffectSendLevel(mSendLevel);
- res = t->attachAuxEffect(mAuxEffectId);
+ mTrack->setAuxEffectSendLevel(mSendLevel);
+ res = mTrack->attachAuxEffect(mAuxEffectId);
}
}
- ALOGV("open() DONE status %d", res);
+ ALOGV("updateTrack() DONE status %d", res);
return res;
}
continue;
}
callbackData->mSwitching = true; // begin track switch
+ callbackData->setOutput(NULL);
#else
// tryBeginTrackSwitch() returns false if the callback has the lock.
if (!mCallbackData->tryBeginTrackSwitch()) {
int event, void *me, void *info);
void deleteRecycledTrack_l();
void close_l();
+ status_t updateTrack();
sp<AudioTrack> mTrack;
sp<AudioTrack> mRecycledTrack;
#include "WebmWriter.h"
#include "StagefrightRecorder.h"
+#include <algorithm>
+
#include <android/hardware/ICamera.h>
#include <binder/IPCThreadState.h>
namespace android {
+static const float kTypicalDisplayRefreshingRate = 60.f;
+// display refresh rate drops on battery saver
+static const float kMinTypicalDisplayRefreshingRate = kTypicalDisplayRefreshingRate / 2;
+static const int kMaxNumVideoTemporalLayers = 8;
+
// To collect the encoder usage for the battery app
static void addBatteryData(uint32_t params) {
sp<IBinder> binder =
break;
}
+ ALOGV("Recording frameRate: %d captureFps: %f",
+ mFrameRate, mCaptureFps);
+
return status;
}
format->setInt32("level", mVideoEncoderLevel);
}
+ uint32_t tsLayers = 1;
+ bool preferBFrames = true; // we like B-frames as it produces better quality per bitrate
format->setInt32("priority", 0 /* realtime */);
+ float maxPlaybackFps = mFrameRate; // assume video is only played back at normal speed
+
if (mCaptureFpsEnable) {
format->setFloat("operating-rate", mCaptureFps);
+
+ // enable layering for all time lapse and high frame rate recordings
+ if (mFrameRate / mCaptureFps >= 1.9) { // time lapse
+ preferBFrames = false;
+ tsLayers = 2; // use at least two layers as resulting video will likely be sped up
+ } else if (mCaptureFps > maxPlaybackFps) { // slow-mo
+ maxPlaybackFps = mCaptureFps; // assume video will be played back at full capture speed
+ preferBFrames = false;
+ }
+ }
+
+ for (uint32_t tryLayers = 1; tryLayers <= kMaxNumVideoTemporalLayers; ++tryLayers) {
+ if (tryLayers > tsLayers) {
+ tsLayers = tryLayers;
+ }
+ // keep going until the base layer fps falls below the typical display refresh rate
+ float baseLayerFps = maxPlaybackFps / (1 << (tryLayers - 1));
+ if (baseLayerFps < kMinTypicalDisplayRefreshingRate / 0.9) {
+ break;
+ }
+ }
+
+ if (tsLayers > 1) {
+ uint32_t bLayers = std::min(2u, tsLayers - 1); // use up-to 2 B-layers
+ uint32_t pLayers = tsLayers - bLayers;
+ format->setString(
+ "ts-schema", AStringPrintf("android.generic.%u+%u", pLayers, bLayers));
+
+ // TODO: some encoders do not support B-frames with temporal layering, and we have a
+ // different preference based on use-case. We could move this into camera profiles.
+ format->setInt32("android._prefer-b-frames", preferBFrames);
}
if (mMetaDataStoredInVideoBuffers != kMetadataBufferTypeInvalid) {
return OK;
}
- // 30 ms buffer to avoid timestamp overlap
- mTotalPausedDurationUs += (systemTime() / 1000) - mPauseStartTimeUs - 30000;
+ int64_t bufferStartTimeUs = 0;
+ bool allSourcesStarted = true;
+ for (const auto &source : { mAudioEncoderSource, mVideoEncoderSource }) {
+ if (source == nullptr) {
+ continue;
+ }
+ int64_t timeUs = source->getFirstSampleSystemTimeUs();
+ if (timeUs < 0) {
+ allSourcesStarted = false;
+ }
+ if (bufferStartTimeUs < timeUs) {
+ bufferStartTimeUs = timeUs;
+ }
+ }
+
+ if (allSourcesStarted) {
+ if (mPauseStartTimeUs < bufferStartTimeUs) {
+ mPauseStartTimeUs = bufferStartTimeUs;
+ }
+ // 30 ms buffer to avoid timestamp overlap
+ mTotalPausedDurationUs += (systemTime() / 1000) - mPauseStartTimeUs - 30000;
+ }
double timeOffset = -mTotalPausedDurationUs;
if (mCaptureFpsEnable) {
timeOffset *= mCaptureFps / mFrameRate;
}
- if (mAudioEncoderSource != NULL) {
- mAudioEncoderSource->setInputBufferTimeOffset((int64_t)timeOffset);
- mAudioEncoderSource->start();
- }
- if (mVideoEncoderSource != NULL) {
- mVideoEncoderSource->setInputBufferTimeOffset((int64_t)timeOffset);
- mVideoEncoderSource->start();
+ for (const auto &source : { mAudioEncoderSource, mVideoEncoderSource }) {
+ if (source == nullptr) {
+ continue;
+ }
+ source->setInputBufferTimeOffset((int64_t)timeOffset);
+ source->start();
}
mPauseStartTimeUs = 0;
}
#endif
+ if (trackType == MEDIA_TRACK_TYPE_VIDEO) {
+ int32_t layerId;
+ if (mb->meta_data()->findInt32(kKeyTemporalLayerId, &layerId)) {
+ meta->setInt32("temporal-layer-id", layerId);
+ }
+ }
+
if (trackType == MEDIA_TRACK_TYPE_TIMEDTEXT) {
const char *mime;
CHECK(mTimedTextTrack.mSource != NULL
if (mIsWidevine) {
maxBuffers = 2;
} else {
- maxBuffers = 4;
+ maxBuffers = 8; // too large of a number may influence seeks
}
break;
case MEDIA_TRACK_TYPE_AUDIO:
MediaSource::ReadOptions options;
bool seeking = false;
-
if (seekTimeUs >= 0) {
options.setSeekTo(seekTimeUs, MediaSource::ReadOptions::SEEK_PREVIOUS_SYNC);
seeking = true;
}
- if (mIsWidevine) {
+ const bool couldReadMultiple = (!mIsWidevine && track->mSource->supportReadMultiple());
+
+ if (mIsWidevine || couldReadMultiple) {
options.setNonBlocking();
}
- bool couldReadMultiple = (!mIsWidevine && trackType == MEDIA_TRACK_TYPE_AUDIO);
for (size_t numBuffers = 0; numBuffers < maxBuffers; ) {
Vector<MediaBuffer *> mediaBuffers;
status_t err = NO_ERROR;
- if (!seeking && couldReadMultiple) {
- err = track->mSource->readMultiple(&mediaBuffers, (maxBuffers - numBuffers));
+ if (couldReadMultiple) {
+ err = track->mSource->readMultiple(
+ &mediaBuffers, maxBuffers - numBuffers, &options);
} else {
MediaBuffer *mbuf = NULL;
err = track->mSource->read(&mbuf, &options);
}
}
- options.clearSeekTo();
+ options.clearNonPersistent();
size_t id = 0;
size_t count = mediaBuffers.size();
}
if (mVideoDecoder != NULL) {
- float rate = getFrameRate();
- if (rate > 0) {
- sp<AMessage> params = new AMessage();
- params->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed);
- mVideoDecoder->setParameters(params);
- }
+ sp<AMessage> params = new AMessage();
+ params->setFloat("playback-speed", mPlaybackSettings.mSpeed);
+ mVideoDecoder->setParameters(params);
}
sp<AMessage> response = new AMessage;
sp<AMessage> inputFormat =
mSource->getFormat(false /* audio */);
+ setVideoScalingMode(mVideoScalingMode);
updateVideoSize(inputFormat, format);
} else if (what == DecoderBase::kWhatShutdownCompleted) {
ALOGV("%s shutdown completed", audio ? "audio" : "video");
return err;
}
}
+
+ if (!audio) {
+ sp<AMessage> params = new AMessage();
+ float rate = getFrameRate();
+ if (rate > 0) {
+ params->setFloat("frame-rate-total", rate);
+ }
+
+ sp<MetaData> fileMeta = getFileMeta();
+ if (fileMeta != NULL) {
+ int32_t videoTemporalLayerCount;
+ if (fileMeta->findInt32(kKeyTemporalLayerCount, &videoTemporalLayerCount)
+ && videoTemporalLayerCount > 0) {
+ params->setInt32("temporal-layer-count", videoTemporalLayerCount);
+ }
+ }
+
+ if (params->countEntries() > 0) {
+ (*decoder)->setParameters(params);
+ }
+ }
return OK;
}
#include <utils/Log.h>
#include <inttypes.h>
+#include <algorithm>
+
#include "NuPlayerCCDecoder.h"
#include "NuPlayerDecoder.h"
#include "NuPlayerRenderer.h"
namespace android {
+static float kDisplayRefreshingRate = 60.f; // TODO: get this from the display
+
+// The default total video frame rate of a stream when that info is not available from
+// the source.
+static float kDefaultVideoFrameRateTotal = 30.f;
+
static inline bool getAudioDeepBufferSetting() {
return property_get_bool("media.stagefright.audio.deep", false /* default_value */);
}
mIsSecure(false),
mFormatChangePending(false),
mTimeChangePending(false),
+ mFrameRateTotal(kDefaultVideoFrameRateTotal),
+ mPlaybackSpeed(1.0f),
+ mNumVideoTemporalLayerTotal(1), // decode all layers
+ mNumVideoTemporalLayerAllowed(1),
+ mCurrentMaxVideoTemporalLayerId(0),
mResumePending(false),
mComponentName("decoder") {
mCodecLooper = new ALooper;
mCodecLooper->setName("NPDecoder-CL");
mCodecLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
+ mVideoTemporalLayerAggregateFps[0] = mFrameRateTotal;
}
NuPlayer::Decoder::~Decoder() {
}
void NuPlayer::Decoder::onSetParameters(const sp<AMessage> ¶ms) {
- if (mCodec == NULL) {
- ALOGW("onSetParameters called before codec is created.");
- return;
+ bool needAdjustLayers = false;
+ float frameRateTotal;
+ if (params->findFloat("frame-rate-total", &frameRateTotal)
+ && mFrameRateTotal != frameRateTotal) {
+ needAdjustLayers = true;
+ mFrameRateTotal = frameRateTotal;
+ }
+
+ int32_t numVideoTemporalLayerTotal;
+ if (params->findInt32("temporal-layer-count", &numVideoTemporalLayerTotal)
+ && numVideoTemporalLayerTotal >= 0
+ && numVideoTemporalLayerTotal <= kMaxNumVideoTemporalLayers
+ && mNumVideoTemporalLayerTotal != numVideoTemporalLayerTotal) {
+ needAdjustLayers = true;
+ mNumVideoTemporalLayerTotal = std::max(numVideoTemporalLayerTotal, 1);
+ }
+
+ if (needAdjustLayers && mNumVideoTemporalLayerTotal > 1) {
+ // TODO: For now, layer fps is calculated for some specific architectures.
+ // But it really should be extracted from the stream.
+ mVideoTemporalLayerAggregateFps[0] =
+ mFrameRateTotal / (float)(1ll << (mNumVideoTemporalLayerTotal - 1));
+ for (int32_t i = 1; i < mNumVideoTemporalLayerTotal; ++i) {
+ mVideoTemporalLayerAggregateFps[i] =
+ mFrameRateTotal / (float)(1ll << (mNumVideoTemporalLayerTotal - i))
+ + mVideoTemporalLayerAggregateFps[i - 1];
+ }
+ }
+
+ float playbackSpeed;
+ if (params->findFloat("playback-speed", &playbackSpeed)
+ && mPlaybackSpeed != playbackSpeed) {
+ needAdjustLayers = true;
+ mPlaybackSpeed = playbackSpeed;
+ }
+
+ if (needAdjustLayers) {
+ float decodeFrameRate = mFrameRateTotal;
+ // enable temporal layering optimization only if we know the layering depth
+ if (mNumVideoTemporalLayerTotal > 1) {
+ int32_t layerId;
+ for (layerId = 0; layerId < mNumVideoTemporalLayerTotal - 1; ++layerId) {
+ if (mVideoTemporalLayerAggregateFps[layerId] * mPlaybackSpeed
+ >= kDisplayRefreshingRate * 0.9) {
+ break;
+ }
+ }
+ mNumVideoTemporalLayerAllowed = layerId + 1;
+ decodeFrameRate = mVideoTemporalLayerAggregateFps[layerId];
+ }
+ ALOGV("onSetParameters: allowed layers=%d, decodeFps=%g",
+ mNumVideoTemporalLayerAllowed, decodeFrameRate);
+
+ if (mCodec == NULL) {
+ ALOGW("onSetParameters called before codec is created.");
+ return;
+ }
+
+ sp<AMessage> codecParams = new AMessage();
+ codecParams->setFloat("operating-rate", decodeFrameRate * mPlaybackSpeed);
+ mCodec->setParameters(codecParams);
}
- mCodec->setParameters(params);
}
void NuPlayer::Decoder::onSetRenderer(const sp<Renderer> &renderer) {
}
dropAccessUnit = false;
- if (!mIsAudio
- && !mIsSecure
- && mRenderer->getVideoLateByUs() > 100000ll
- && mIsVideoAVC
- && !IsAVCReferenceFrame(accessUnit)) {
- dropAccessUnit = true;
- ++mNumInputFramesDropped;
+ if (!mIsAudio && !mIsSecure) {
+ int32_t layerId = 0;
+ bool haveLayerId = accessUnit->meta()->findInt32("temporal-layer-id", &layerId);
+ if (mRenderer->getVideoLateByUs() > 100000ll
+ && mIsVideoAVC
+ && !IsAVCReferenceFrame(accessUnit)) {
+ dropAccessUnit = true;
+ } else if (haveLayerId && mNumVideoTemporalLayerTotal > 1) {
+ // Add only one layer each time.
+ if (layerId > mCurrentMaxVideoTemporalLayerId + 1
+ || layerId >= mNumVideoTemporalLayerAllowed) {
+ dropAccessUnit = true;
+ ALOGV("dropping layer(%d), speed=%g, allowed layer count=%d, max layerId=%d",
+ layerId, mPlaybackSpeed, mNumVideoTemporalLayerAllowed,
+ mCurrentMaxVideoTemporalLayerId);
+ } else if (layerId > mCurrentMaxVideoTemporalLayerId) {
+ mCurrentMaxVideoTemporalLayerId = layerId;
+ } else if (layerId == 0 && mNumVideoTemporalLayerTotal > 1 && IsIDR(accessUnit)) {
+ mCurrentMaxVideoTemporalLayerId = mNumVideoTemporalLayerTotal - 1;
+ }
+ }
+ if (dropAccessUnit) {
+ if (layerId <= mCurrentMaxVideoTemporalLayerId && layerId > 0) {
+ mCurrentMaxVideoTemporalLayerId = layerId - 1;
+ }
+ ++mNumInputFramesDropped;
+ }
}
} while (dropAccessUnit);
kWhatSetVideoSurface = 'sSur'
};
+ enum {
+ kMaxNumVideoTemporalLayers = 32,
+ };
+
sp<Surface> mSurface;
sp<Source> mSource;
bool mIsSecure;
bool mFormatChangePending;
bool mTimeChangePending;
+ float mFrameRateTotal;
+ float mPlaybackSpeed;
+ int32_t mNumVideoTemporalLayerTotal;
+ int32_t mNumVideoTemporalLayerAllowed;
+ int32_t mCurrentMaxVideoTemporalLayerId;
+ float mVideoTemporalLayerAggregateFps[kMaxNumVideoTemporalLayers];
bool mResumePending;
AString mComponentName;
namespace android {
-struct MemoryDealer;
+class MemoryDealer;
struct NuPlayer::NuPlayerStreamListener : public BnStreamListener {
NuPlayerStreamListener(
size_t numTracks = mTracks.size();
size_t preparedCount, underflowCount, overflowCount, startCount;
preparedCount = underflowCount = overflowCount = startCount = 0;
- for (size_t i = 0; i < numTracks; ++i) {
+
+ size_t count = numTracks;
+ for (size_t i = 0; i < count; ++i) {
status_t finalResult;
TrackInfo *info = &mTracks.editItemAt(i);
sp<AnotherPacketSource> src = info->mSource;
+ if (src == NULL) {
+ --numTracks;
+ continue;
+ }
int64_t bufferedDurationUs = src->getBufferedDurationUs(&finalResult);
// isFinished when duration is 0 checks for EOS result only
sp<MetaData> meta = source->getFormat();
status_t err = convertMetaDataToMessage(meta, &format);
- if (err != OK) {
+ if (err != OK) { // format may have been cleared on error
+ format = new AMessage;
format->setInt32("err", err);
}
return format;
if (mDataSource->getSize(&streamSize) == OK) {
while (offset < streamSize) {
if ((frameSize = getAdtsFrameLength(source, offset, NULL)) == 0) {
- return;
+ ALOGW("prematured AAC stream (%lld vs %lld)",
+ (long long)offset, (long long)streamSize);
+ break;
}
mOffsetVector.push(offset);
return OK;
}
+status_t ACodec::configureTemporalLayers(
+ const sp<AMessage> &msg, bool inConfigure, sp<AMessage> &outputFormat) {
+ if (!mIsVideo || !mIsEncoder) {
+ return INVALID_OPERATION;
+ }
+
+ AString tsSchema;
+ if (!msg->findString("ts-schema", &tsSchema)) {
+ return OK;
+ }
+
+ unsigned int numLayers = 0;
+ unsigned int numBLayers = 0;
+ int tags;
+ char dummy;
+ OMX_VIDEO_ANDROID_TEMPORALLAYERINGPATTERNTYPE pattern =
+ OMX_VIDEO_AndroidTemporalLayeringPatternNone;
+ if (sscanf(tsSchema.c_str(), "webrtc.vp8.%u-layer%c", &numLayers, &dummy) == 1
+ && numLayers > 0) {
+ pattern = OMX_VIDEO_AndroidTemporalLayeringPatternWebRTC;
+ } else if ((tags = sscanf(tsSchema.c_str(), "android.generic.%u%c%u%c",
+ &numLayers, &dummy, &numBLayers, &dummy))
+ && (tags == 1 || (tags == 3 && dummy == '+'))
+ && numLayers > 0 && numLayers < UINT32_MAX - numBLayers) {
+ numLayers += numBLayers;
+ pattern = OMX_VIDEO_AndroidTemporalLayeringPatternAndroid;
+ } else {
+ ALOGI("Ignoring unsupported ts-schema [%s]", tsSchema.c_str());
+ return BAD_VALUE;
+ }
+
+ OMX_VIDEO_PARAM_ANDROID_TEMPORALLAYERINGTYPE layerParams;
+ InitOMXParams(&layerParams);
+ layerParams.nPortIndex = kPortIndexOutput;
+
+ status_t err = mOMX->getParameter(
+ mNode, (OMX_INDEXTYPE)OMX_IndexParamAndroidVideoTemporalLayering,
+ &layerParams, sizeof(layerParams));
+
+ if (err != OK) {
+ return err;
+ } else if (!(layerParams.eSupportedPatterns & pattern)) {
+ return BAD_VALUE;
+ }
+
+ numLayers = min(numLayers, layerParams.nLayerCountMax);
+ numBLayers = min(numBLayers, layerParams.nBLayerCountMax);
+
+ if (!inConfigure) {
+ OMX_VIDEO_CONFIG_ANDROID_TEMPORALLAYERINGTYPE layerConfig;
+ InitOMXParams(&layerConfig);
+ layerConfig.nPortIndex = kPortIndexOutput;
+ layerConfig.ePattern = pattern;
+ layerConfig.nPLayerCountActual = numLayers - numBLayers;
+ layerConfig.nBLayerCountActual = numBLayers;
+ layerConfig.bBitrateRatiosSpecified = OMX_FALSE;
+
+ err = mOMX->setConfig(
+ mNode, (OMX_INDEXTYPE)OMX_IndexConfigAndroidVideoTemporalLayering,
+ &layerConfig, sizeof(layerConfig));
+ } else {
+ layerParams.ePattern = pattern;
+ layerParams.nPLayerCountActual = numLayers - numBLayers;
+ layerParams.nBLayerCountActual = numBLayers;
+ layerParams.bBitrateRatiosSpecified = OMX_FALSE;
+
+ err = mOMX->setParameter(
+ mNode, (OMX_INDEXTYPE)OMX_IndexParamAndroidVideoTemporalLayering,
+ &layerParams, sizeof(layerParams));
+ }
+
+ AString configSchema;
+ if (pattern == OMX_VIDEO_AndroidTemporalLayeringPatternAndroid) {
+ configSchema = AStringPrintf("android.generic.%u+%u", numLayers - numBLayers, numBLayers);
+ } else if (pattern == OMX_VIDEO_AndroidTemporalLayeringPatternWebRTC) {
+ configSchema = AStringPrintf("webrtc.vp8.%u", numLayers);
+ }
+
+ if (err != OK) {
+ ALOGW("Failed to set temporal layers to %s (requested %s)",
+ configSchema.c_str(), tsSchema.c_str());
+ return err;
+ }
+
+ err = mOMX->getParameter(
+ mNode, (OMX_INDEXTYPE)OMX_IndexParamAndroidVideoTemporalLayering,
+ &layerParams, sizeof(layerParams));
+
+ if (err == OK) {
+ ALOGD("Temporal layers requested:%s configured:%s got:%s(%u: P=%u, B=%u)",
+ tsSchema.c_str(), configSchema.c_str(),
+ asString(layerParams.ePattern), layerParams.ePattern,
+ layerParams.nPLayerCountActual, layerParams.nBLayerCountActual);
+
+ if (outputFormat.get() == mOutputFormat.get()) {
+ mOutputFormat = mOutputFormat->dup(); // trigger an output format change event
+ }
+ // assume we got what we configured
+ outputFormat->setString("ts-schema", configSchema);
+ }
+ return err;
+}
+
status_t ACodec::setMinBufferSize(OMX_U32 portIndex, size_t size) {
OMX_PARAM_PORTDEFINITIONTYPE def;
InitOMXParams(&def);
return ERROR_UNSUPPORTED;
}
+status_t ACodec::setPortBufferNum(OMX_U32 portIndex, int bufferNum) {
+ OMX_PARAM_PORTDEFINITIONTYPE def;
+ InitOMXParams(&def);
+ def.nPortIndex = portIndex;
+ status_t err;
+ ALOGD("Setting [%s] %s port buffer number: %d", mComponentName.c_str(),
+ portIndex == kPortIndexInput ? "input" : "output", bufferNum);
+ err = mOMX->getParameter(
+ mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
+ if (err != OK) {
+ return err;
+ }
+ def.nBufferCountActual = bufferNum;
+ err = mOMX->setParameter(
+ mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
+ if (err != OK) {
+ // Component could reject this request.
+ ALOGW("Fail to set [%s] %s port buffer number: %d", mComponentName.c_str(),
+ portIndex == kPortIndexInput ? "input" : "output", bufferNum);
+ }
+ return OK;
+}
+
status_t ACodec::setupVideoDecoder(
const char *mime, const sp<AMessage> &msg, bool haveNativeWindow,
bool usingSwRenderer, sp<AMessage> &outputFormat) {
return err;
}
+ // Set the component input buffer number to be |tmp|. If succeed,
+ // component will set input port buffer number to be |tmp|. If fail,
+ // component will keep the same buffer number as before.
+ if (msg->findInt32("android._num-input-buffers", &tmp)) {
+ err = setPortBufferNum(kPortIndexInput, tmp);
+ if (err != OK)
+ return err;
+ }
+
+ // Set the component output buffer number to be |tmp|. If succeed,
+ // component will set output port buffer number to be |tmp|. If fail,
+ // component will keep the same buffer number as before.
+ if (msg->findInt32("android._num-output-buffers", &tmp)) {
+ err = setPortBufferNum(kPortIndexOutput, tmp);
+ if (err != OK)
+ return err;
+ }
+
int32_t frameRateInt;
float frameRateFloat;
if (!msg->findFloat("frame-rate", &frameRateFloat)) {
case OMX_VIDEO_CodingVP8:
case OMX_VIDEO_CodingVP9:
- err = setupVPXEncoderParameters(msg);
+ err = setupVPXEncoderParameters(msg, outputFormat);
break;
default:
break;
}
+ if (err != OK) {
+ return err;
+ }
+
// Set up color aspects on input, but propagate them to the output format, as they will
// not be read back from encoder.
err = setColorAspectsForVideoEncoder(msg, outputFormat, inputFormat);
err = OK;
}
+ if (err != OK) {
+ return err;
+ }
+
+ switch (compressionFormat) {
+ case OMX_VIDEO_CodingAVC:
+ case OMX_VIDEO_CodingHEVC:
+ err = configureTemporalLayers(msg, true /* inConfigure */, outputFormat);
+ if (err != OK) {
+ err = OK; // ignore failure
+ }
+ break;
+
+ case OMX_VIDEO_CodingVP8:
+ case OMX_VIDEO_CodingVP9:
+ // TODO: do we need to support android.generic layering? webrtc layering is
+ // already set up in setupVPXEncoderParameters.
+ break;
+
+ default:
+ break;
+ }
+
if (err == OK) {
ALOGI("setupVideoEncoder succeeded");
}
return err;
}
-static OMX_U32 setPFramesSpacing(int32_t iFramesInterval, int32_t frameRate) {
- if (iFramesInterval < 0) {
- return 0xFFFFFFFF;
- } else if (iFramesInterval == 0) {
+static OMX_U32 setPFramesSpacing(
+ float iFramesInterval /* seconds */, int32_t frameRate, uint32_t BFramesSpacing = 0) {
+ // BFramesSpacing is the number of B frames between I/P frames
+ // PFramesSpacing (the value to be returned) is the number of P frames between I frames
+ //
+ // keyFrameInterval = ((PFramesSpacing + 1) * BFramesSpacing) + PFramesSpacing + 1
+ // ^^^ ^^^ ^^^
+ // number of B frames number of P I frame
+ //
+ // = (PFramesSpacing + 1) * (BFramesSpacing + 1)
+ //
+ // E.g.
+ // I P I : I-interval: 8, nPFrames 1, nBFrames 3
+ // BBB BBB
+
+ if (iFramesInterval < 0) { // just 1 key frame
+ return 0xFFFFFFFE; // don't use maxint as key-frame-interval calculation will add 1
+ } else if (iFramesInterval == 0) { // just key frames
return 0;
}
- OMX_U32 ret = frameRate * iFramesInterval;
- return ret;
+
+ // round down as key-frame-interval is an upper limit
+ uint32_t keyFrameInterval = uint32_t(frameRate * iFramesInterval);
+ OMX_U32 ret = keyFrameInterval / (BFramesSpacing + 1);
+ return ret > 0 ? ret - 1 : 0;
}
static OMX_VIDEO_CONTROLRATETYPE getBitrateMode(const sp<AMessage> &msg) {
}
status_t ACodec::setupMPEG4EncoderParameters(const sp<AMessage> &msg) {
- int32_t bitrate, iFrameInterval;
+ int32_t bitrate;
+ float iFrameInterval;
if (!msg->findInt32("bitrate", &bitrate)
- || !msg->findInt32("i-frame-interval", &iFrameInterval)) {
+ || !msg->findAsFloat("i-frame-interval", &iFrameInterval)) {
return INVALID_OPERATION;
}
mpeg4type.nAllowedPictureTypes =
OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
- mpeg4type.nPFrames = setPFramesSpacing(iFrameInterval, frameRate);
+ mpeg4type.nBFrames = 0;
+ mpeg4type.nPFrames = setPFramesSpacing(iFrameInterval, frameRate, mpeg4type.nBFrames);
if (mpeg4type.nPFrames == 0) {
mpeg4type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI;
}
- mpeg4type.nBFrames = 0;
mpeg4type.nIDCVLCThreshold = 0;
mpeg4type.bACPred = OMX_TRUE;
mpeg4type.nMaxPacketSize = 256;
}
status_t ACodec::setupH263EncoderParameters(const sp<AMessage> &msg) {
- int32_t bitrate, iFrameInterval;
+ int32_t bitrate;
+ float iFrameInterval;
if (!msg->findInt32("bitrate", &bitrate)
- || !msg->findInt32("i-frame-interval", &iFrameInterval)) {
+ || !msg->findAsFloat("i-frame-interval", &iFrameInterval)) {
return INVALID_OPERATION;
}
h263type.nAllowedPictureTypes =
OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
- h263type.nPFrames = setPFramesSpacing(iFrameInterval, frameRate);
+ h263type.nBFrames = 0;
+ h263type.nPFrames = setPFramesSpacing(iFrameInterval, frameRate, h263type.nBFrames);
if (h263type.nPFrames == 0) {
h263type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI;
}
- h263type.nBFrames = 0;
int32_t profile;
if (msg->findInt32("profile", &profile)) {
}
status_t ACodec::setupAVCEncoderParameters(const sp<AMessage> &msg) {
- int32_t bitrate, iFrameInterval;
+ int32_t bitrate;
+ float iFrameInterval;
if (!msg->findInt32("bitrate", &bitrate)
- || !msg->findInt32("i-frame-interval", &iFrameInterval)) {
+ || !msg->findAsFloat("i-frame-interval", &iFrameInterval)) {
return INVALID_OPERATION;
}
h264type.eProfile = static_cast<OMX_VIDEO_AVCPROFILETYPE>(profile);
h264type.eLevel = static_cast<OMX_VIDEO_AVCLEVELTYPE>(level);
} else {
- // Use baseline profile for AVC recording if profile is not specified.
h264type.eProfile = OMX_VIDEO_AVCProfileBaseline;
+#if 0 /* DON'T YET DEFAULT TO HIGHEST PROFILE */
+ // Use largest supported profile for AVC recording if profile is not specified.
+ for (OMX_VIDEO_AVCPROFILETYPE profile : {
+ OMX_VIDEO_AVCProfileHigh, OMX_VIDEO_AVCProfileMain }) {
+ if (verifySupportForProfileAndLevel(profile, 0) == OK) {
+ h264type.eProfile = profile;
+ break;
+ }
+ }
+#endif
}
ALOGI("setupAVCEncoderParameters with [profile: %s] [level: %s]",
h264type.bUseHadamard = OMX_TRUE;
h264type.nRefFrames = 1;
h264type.nBFrames = 0;
- h264type.nPFrames = setPFramesSpacing(iFrameInterval, frameRate);
+ h264type.nPFrames = setPFramesSpacing(iFrameInterval, frameRate, h264type.nBFrames);
if (h264type.nPFrames == 0) {
h264type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI;
}
h264type.bUseHadamard = OMX_TRUE;
h264type.nRefFrames = 2;
h264type.nBFrames = 1;
- h264type.nPFrames = setPFramesSpacing(iFrameInterval, frameRate);
+ h264type.nPFrames = setPFramesSpacing(iFrameInterval, frameRate, h264type.nBFrames);
h264type.nAllowedPictureTypes =
OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP | OMX_VIDEO_PictureTypeB;
h264type.nRefIdx10ActiveMinus1 = 0;
return err;
}
+ // TRICKY: if we are enabling temporal layering as well, some codecs may not support layering
+ // when B-frames are enabled. Detect this now so we can disable B frames if temporal layering
+ // is preferred.
+ AString tsSchema;
+ int32_t preferBFrames = (int32_t)false;
+ if (msg->findString("ts-schema", &tsSchema)
+ && (!msg->findInt32("android._prefer-b-frames", &preferBFrames) || !preferBFrames)) {
+ OMX_VIDEO_PARAM_ANDROID_TEMPORALLAYERINGTYPE layering;
+ InitOMXParams(&layering);
+ layering.nPortIndex = kPortIndexOutput;
+ if (mOMX->getParameter(
+ mNode, (OMX_INDEXTYPE)OMX_IndexParamAndroidVideoTemporalLayering,
+ &layering, sizeof(layering)) == OK
+ && layering.eSupportedPatterns
+ && layering.nBLayerCountMax == 0) {
+ h264type.nBFrames = 0;
+ h264type.nPFrames = setPFramesSpacing(iFrameInterval, frameRate, h264type.nBFrames);
+ h264type.nAllowedPictureTypes &= ~OMX_VIDEO_PictureTypeB;
+ ALOGI("disabling B-frames");
+ err = mOMX->setParameter(
+ mNode, OMX_IndexParamVideoAvc, &h264type, sizeof(h264type));
+
+ if (err != OK) {
+ return err;
+ }
+ }
+ }
+
return configureBitrate(bitrate, bitrateMode);
}
status_t ACodec::setupHEVCEncoderParameters(const sp<AMessage> &msg) {
- int32_t bitrate, iFrameInterval;
+ int32_t bitrate;
+ float iFrameInterval;
if (!msg->findInt32("bitrate", &bitrate)
- || !msg->findInt32("i-frame-interval", &iFrameInterval)) {
+ || !msg->findAsFloat("i-frame-interval", &iFrameInterval)) {
return INVALID_OPERATION;
}
hevcType.eLevel = static_cast<OMX_VIDEO_HEVCLEVELTYPE>(level);
}
// TODO: finer control?
- hevcType.nKeyFrameInterval = setPFramesSpacing(iFrameInterval, frameRate);
+ hevcType.nKeyFrameInterval = setPFramesSpacing(iFrameInterval, frameRate) + 1;
err = mOMX->setParameter(
mNode, (OMX_INDEXTYPE)OMX_IndexParamVideoHevc, &hevcType, sizeof(hevcType));
return configureBitrate(bitrate, bitrateMode);
}
-status_t ACodec::setupVPXEncoderParameters(const sp<AMessage> &msg) {
+status_t ACodec::setupVPXEncoderParameters(const sp<AMessage> &msg, sp<AMessage> &outputFormat) {
int32_t bitrate;
- int32_t iFrameInterval = 0;
+ float iFrameInterval = 0;
size_t tsLayers = 0;
OMX_VIDEO_ANDROID_VPXTEMPORALLAYERPATTERNTYPE pattern =
OMX_VIDEO_VPXTemporalLayerPatternNone;
if (!msg->findInt32("bitrate", &bitrate)) {
return INVALID_OPERATION;
}
- msg->findInt32("i-frame-interval", &iFrameInterval);
+ msg->findAsFloat("i-frame-interval", &iFrameInterval);
OMX_VIDEO_CONTROLRATETYPE bitrateMode = getBitrateMode(msg);
}
AString tsSchema;
+ OMX_VIDEO_ANDROID_TEMPORALLAYERINGPATTERNTYPE tsType =
+ OMX_VIDEO_AndroidTemporalLayeringPatternNone;
+
if (msg->findString("ts-schema", &tsSchema)) {
- if (tsSchema == "webrtc.vp8.1-layer") {
+ unsigned int numLayers = 0;
+ unsigned int numBLayers = 0;
+ int tags;
+ char dummy;
+ if (sscanf(tsSchema.c_str(), "webrtc.vp8.%u-layer%c", &numLayers, &dummy) == 1
+ && numLayers > 0) {
pattern = OMX_VIDEO_VPXTemporalLayerPatternWebRTC;
- tsLayers = 1;
- } else if (tsSchema == "webrtc.vp8.2-layer") {
+ tsType = OMX_VIDEO_AndroidTemporalLayeringPatternWebRTC;
+ tsLayers = numLayers;
+ } else if ((tags = sscanf(tsSchema.c_str(), "android.generic.%u%c%u%c",
+ &numLayers, &dummy, &numBLayers, &dummy))
+ && (tags == 1 || (tags == 3 && dummy == '+'))
+ && numLayers > 0 && numLayers < UINT32_MAX - numBLayers) {
pattern = OMX_VIDEO_VPXTemporalLayerPatternWebRTC;
- tsLayers = 2;
- } else if (tsSchema == "webrtc.vp8.3-layer") {
- pattern = OMX_VIDEO_VPXTemporalLayerPatternWebRTC;
- tsLayers = 3;
+ // VPX does not have a concept of B-frames, so just count all layers
+ tsType = OMX_VIDEO_AndroidTemporalLayeringPatternAndroid;
+ tsLayers = numLayers + numBLayers;
} else {
- ALOGW("Unsupported ts-schema [%s]", tsSchema.c_str());
+ ALOGW("Ignoring unsupported ts-schema [%s]", tsSchema.c_str());
}
+ tsLayers = min(tsLayers, (size_t)OMX_VIDEO_ANDROID_MAXVP8TEMPORALLAYERS);
}
OMX_VIDEO_PARAM_ANDROID_VP8ENCODERTYPE vp8type;
if (err == OK) {
if (iFrameInterval > 0) {
- vp8type.nKeyFrameInterval = setPFramesSpacing(iFrameInterval, frameRate);
+ vp8type.nKeyFrameInterval = setPFramesSpacing(iFrameInterval, frameRate) + 1;
}
vp8type.eTemporalPattern = pattern;
vp8type.nTemporalLayerCount = tsLayers;
&vp8type, sizeof(vp8type));
if (err != OK) {
ALOGW("Extended VP8 parameters set failed: %d", err);
+ } else if (tsType == OMX_VIDEO_AndroidTemporalLayeringPatternWebRTC) {
+ // advertise even single layer WebRTC layering, as it is defined
+ outputFormat->setString("ts-schema", AStringPrintf("webrtc.vp8.%u-layer", tsLayers));
+ } else if (tsLayers > 0) {
+ // tsType == OMX_VIDEO_AndroidTemporalLayeringPatternAndroid
+ outputFormat->setString("ts-schema", AStringPrintf("android.generic.%u", tsLayers));
}
}
sizeof(vp8type));
if (err == OK) {
- AString tsSchema = "none";
- if (vp8type.eTemporalPattern
- == OMX_VIDEO_VPXTemporalLayerPatternWebRTC) {
- switch (vp8type.nTemporalLayerCount) {
- case 1:
- {
- tsSchema = "webrtc.vp8.1-layer";
- break;
- }
- case 2:
- {
- tsSchema = "webrtc.vp8.2-layer";
- break;
- }
- case 3:
- {
- tsSchema = "webrtc.vp8.3-layer";
- break;
- }
- default:
- {
- break;
- }
+ if (vp8type.eTemporalPattern == OMX_VIDEO_VPXTemporalLayerPatternWebRTC
+ && vp8type.nTemporalLayerCount > 0
+ && vp8type.nTemporalLayerCount
+ <= OMX_VIDEO_ANDROID_MAXVP8TEMPORALLAYERS) {
+ // advertise as android.generic if we configured for android.generic
+ AString origSchema;
+ if (notify->findString("ts-schema", &origSchema)
+ && origSchema.startsWith("android.generic")) {
+ notify->setString("ts-schema", AStringPrintf(
+ "android.generic.%u", vp8type.nTemporalLayerCount));
+ } else {
+ notify->setString("ts-schema", AStringPrintf(
+ "webrtc.vp8.%u-layer", vp8type.nTemporalLayerCount));
}
}
- notify->setString("ts-schema", tsSchema);
}
// Fall through to set up mime.
}
}
}
+ int64_t timeOffsetUs;
+ if (params->findInt64("time-offset-us", &timeOffsetUs)) {
+ status_t err = mOMX->setInternalOption(
+ mNode,
+ kPortIndexInput,
+ IOMX::INTERNAL_OPTION_TIME_OFFSET,
+ &timeOffsetUs,
+ sizeof(timeOffsetUs));
+
+ if (err != OK) {
+ ALOGE("[%s] Unable to set input buffer time offset (err %d)",
+ mComponentName.c_str(),
+ err);
+ return err;
+ }
+ }
+
int64_t skipFramesBeforeUs;
if (params->findInt64("skip-frames-before", &skipFramesBeforeUs)) {
status_t err =
}
}
- return OK;
+ status_t err = configureTemporalLayers(params, false /* inConfigure */, mOutputFormat);
+ if (err != OK) {
+ err = OK; // ignore failure
+ }
+
+ return err;
}
void ACodec::onSignalEndOfInputStream() {
LOCAL_STATIC_LIBRARIES := \
libstagefright_color_conversion \
+ libyuv_static \
libstagefright_aacenc \
libstagefright_matroska \
libstagefright_mediafilter \
#include <byteswap.h>
#include "include/ID3.h"
+#include "include/avc_utils.h"
#ifndef UINT32_MAX
#define UINT32_MAX (4294967295U)
virtual sp<MetaData> getFormat();
virtual status_t read(MediaBuffer **buffer, const ReadOptions *options = NULL);
+ virtual bool supportNonblockingRead() { return true; }
virtual status_t fragmentedRead(MediaBuffer **buffer, const ReadOptions *options = NULL);
protected:
mInitCheck(NO_INIT),
mHasVideo(false),
mHeaderTimescale(0),
+ mIsQT(false),
mFirstTrack(NULL),
mLastTrack(NULL),
mFileMetaData(new MetaData),
case FOURCC('s', 'i', 'n', 'f'):
case FOURCC('s', 'c', 'h', 'i'):
case FOURCC('e', 'd', 't', 's'):
+ case FOURCC('w', 'a', 'v', 'e'):
{
if (chunk_type == FOURCC('m', 'o', 'o', 'v') && depth != 0) {
ALOGE("moov: depth %d", depth);
case FOURCC('s', 'a', 'm', 'r'):
case FOURCC('s', 'a', 'w', 'b'):
{
+ if (mIsQT && chunk_type == FOURCC('m', 'p', '4', 'a')
+ && depth >= 1 && mPath[depth - 1] == FOURCC('w', 'a', 'v', 'e')) {
+ // Ignore mp4a embedded in QT wave atom
+ *offset += chunk_size;
+ break;
+ }
+
uint8_t buffer[8 + 20];
if (chunk_data_size < (ssize_t)sizeof(buffer)) {
// Basic AudioSampleEntry size.
}
uint16_t data_ref_index __unused = U16_AT(&buffer[6]);
+ uint16_t version = U16_AT(&buffer[8]);
uint32_t num_channels = U16_AT(&buffer[16]);
uint16_t sample_size = U16_AT(&buffer[18]);
if (mLastTrack == NULL)
return ERROR_MALFORMED;
+ off64_t stop_offset = *offset + chunk_size;
+ *offset = data_offset + sizeof(buffer);
+
+ if (mIsQT && chunk_type == FOURCC('m', 'p', '4', 'a')) {
+ if (version == 1) {
+ if (mDataSource->readAt(*offset, buffer, 16) < 16) {
+ return ERROR_IO;
+ }
+
+#if 0
+ U32_AT(buffer); // samples per packet
+ U32_AT(&buffer[4]); // bytes per packet
+ U32_AT(&buffer[8]); // bytes per frame
+ U32_AT(&buffer[12]); // bytes per sample
+#endif
+ *offset += 16;
+ } else if (version == 2) {
+ uint8_t v2buffer[36];
+ if (mDataSource->readAt(*offset, v2buffer, 36) < 36) {
+ return ERROR_IO;
+ }
+
+#if 0
+ U32_AT(v2buffer); // size of struct only
+ sample_rate = (uint32_t)U64_AT(&v2buffer[4]); // audio sample rate
+ num_channels = U32_AT(&v2buffer[12]); // num audio channels
+ U32_AT(&v2buffer[16]); // always 0x7f000000
+ sample_size = (uint16_t)U32_AT(&v2buffer[20]); // const bits per channel
+ U32_AT(&v2buffer[24]); // format specifc flags
+ U32_AT(&v2buffer[28]); // const bytes per audio packet
+ U32_AT(&v2buffer[32]); // const LPCM frames per audio packet
+#endif
+ *offset += 36;
+ }
+ }
+
if (chunk_type != FOURCC('e', 'n', 'c', 'a')) {
// if the chunk type is enca, we'll get the type from the sinf/frma box later
mLastTrack->meta->setCString(kKeyMIMEType, FourCC2MIME(chunk_type));
mLastTrack->meta->setInt32(kKeyChannelCount, num_channels);
mLastTrack->meta->setInt32(kKeySampleRate, sample_rate);
- off64_t stop_offset = *offset + chunk_size;
- *offset = data_offset + sizeof(buffer);
while (*offset < stop_offset) {
status_t err = parseChunk(offset, depth + 1);
if (err != OK) {
// Worst case the location string length would be 18,
// for instance +90.0000-180.0000, without the trailing "/" and
- // the string length + language code.
- char buffer[18];
+ // the string length + language code, and some devices include
+ // an additional 8 bytes of altitude, e.g. +007.186
+ char buffer[18 + 8];
// Substracting 5 from the data size is because the text string length +
// language code takes 4 bytes, and the trailing slash "/" takes 1 byte.
return UNKNOWN_ERROR; // stop parsing after sidx
}
+ case FOURCC('f', 't', 'y', 'p'):
+ {
+ if (chunk_data_size < 8 || depth != 0) {
+ return ERROR_MALFORMED;
+ }
+
+ off64_t stop_offset = *offset + chunk_size;
+ uint32_t numCompatibleBrands = (chunk_data_size - 8) / 4;
+ for (size_t i = 0; i < numCompatibleBrands + 2; ++i) {
+ if (i == 1) {
+ // Skip this index, it refers to the minorVersion,
+ // not a brand.
+ continue;
+ }
+
+ uint32_t brand;
+ if (mDataSource->readAt(data_offset + 4 * i, &brand, 4) < 4) {
+ return ERROR_MALFORMED;
+ }
+
+ brand = ntohl(brand);
+ if (brand == FOURCC('q', 't', ' ', ' ')) {
+ mIsQT = true;
+ break;
+ }
+ }
+
+ *offset = stop_offset;
+
+ break;
+ }
+
default:
{
// check if we're parsing 'ilst' for meta keys
if (!strcasecmp(mMetaKeyMap[index].c_str(), "com.android.capture.fps")) {
mFileMetaData->setFloat(kKeyCaptureFramerate, *(float *)&val);
}
+ } else if (dataType == 67 && dataSize >= 4) {
+ // BE signed int32
+ uint32_t val;
+ if (!mDataSource->getUInt32(offset, &val)) {
+ return ERROR_MALFORMED;
+ }
+ if (!strcasecmp(mMetaKeyMap[index].c_str(), "com.android.video.temporal_layers_count")) {
+ mFileMetaData->setInt32(kKeyTemporalLayerCount, val);
+ }
} else {
// add more keys if needed
ALOGV("ignoring key: type %d, size %d", dataType, dataSize);
// A somewhat arbitrary limit that should be sufficient for 8k video frames
// If you see the message below for a valid input stream: increase the limit
- if (max_size > 64 * 1024 * 1024) {
- ALOGE("bogus max input size: %zu", max_size);
+ const size_t kMaxBufferSize = 64 * 1024 * 1024;
+ if (max_size > kMaxBufferSize) {
+ ALOGE("bogus max input size: %zu > %zu", max_size, kMaxBufferSize);
+ return ERROR_MALFORMED;
+ }
+ if (max_size == 0) {
+ ALOGE("zero max input size");
return ERROR_MALFORMED;
}
- mGroup = new MediaBufferGroup;
- mGroup->add_buffer(new MediaBuffer(max_size));
+ // Allow up to kMaxBuffers, but not if the total exceeds kMaxBufferSize.
+ const size_t kMaxBuffers = 8;
+ const size_t buffers = min(kMaxBufferSize / max_size, kMaxBuffers);
+ mGroup = new MediaBufferGroup(buffers, max_size);
mSrcBuffer = new (std::nothrow) uint8_t[max_size];
if (mSrcBuffer == NULL) {
// file probably specified a bad max size
CHECK(mStarted);
+ if (options != nullptr && options->getNonBlocking() && !mGroup->has_buffers()) {
+ *out = nullptr;
+ return WOULD_BLOCK;
+ }
+
if (mFirstMoofOffset > 0) {
return fragmentedRead(out, options);
}
kKeyTargetTime, targetSampleTimeUs);
}
+ if (mIsAVC) {
+ uint32_t layerId = FindAVCLayerId(
+ (const uint8_t *)mBuffer->data(), mBuffer->range_length());
+ mBuffer->meta_data()->setInt32(kKeyTemporalLayerId, layerId);
+ }
+
if (isSyncSample) {
mBuffer->meta_data()->setInt32(kKeyIsSyncFrame, 1);
}
kKeyTargetTime, targetSampleTimeUs);
}
+ if (mIsAVC) {
+ uint32_t layerId = FindAVCLayerId(
+ (const uint8_t *)mBuffer->data(), mBuffer->range_length());
+ mBuffer->meta_data()->setInt32(kKeyTemporalLayerId, layerId);
+ }
+
if (isSyncSample) {
mBuffer->meta_data()->setInt32(kKeyIsSyncFrame, 1);
}
//#define LOG_NDEBUG 0
#define LOG_TAG "MPEG4Writer"
+#include <algorithm>
+
#include <arpa/inet.h>
#include <fcntl.h>
#include <inttypes.h>
#include <utils/Log.h>
+#include <functional>
+
#include <media/stagefright/foundation/ADebug.h>
#include <media/stagefright/foundation/AMessage.h>
+#include <media/stagefright/foundation/AUtils.h>
#include <media/stagefright/foundation/ColorUtils.h>
#include <media/stagefright/MPEG4Writer.h>
#include <media/stagefright/MediaBuffer.h>
#include "include/ESDS.h"
#include "include/HevcUtils.h"
+#include "include/avc_utils.h"
#ifndef __predict_false
#define __predict_false(exp) __builtin_expect((exp) != 0, 0)
static const char kMetaKey_Build[] = "com.android.build";
#endif
static const char kMetaKey_CaptureFps[] = "com.android.capture.fps";
+static const char kMetaKey_TemporalLayerCount[] = "com.android.video.temporal_layers_count";
static const uint8_t kMandatoryHevcNalUnitTypes[3] = {
kHevcNalUnitTypeVps,
};
// A helper class to handle faster write box with table entries
- template<class TYPE>
+ template<class TYPE, unsigned ENTRY_SIZE>
+ // ENTRY_SIZE: # of values in each entry
struct ListTableEntries {
- ListTableEntries(uint32_t elementCapacity, uint32_t entryCapacity)
+ static_assert(ENTRY_SIZE > 0, "ENTRY_SIZE must be positive");
+ ListTableEntries(uint32_t elementCapacity)
: mElementCapacity(elementCapacity),
- mEntryCapacity(entryCapacity),
mTotalNumTableEntries(0),
mNumValuesInCurrEntry(0),
mCurrTableEntriesElement(NULL) {
CHECK_GT(mElementCapacity, 0);
- CHECK_GT(mEntryCapacity, 0);
// Ensure no integer overflow on allocation in add().
- CHECK_LT(mEntryCapacity, UINT32_MAX / mElementCapacity);
+ CHECK_LT(ENTRY_SIZE, UINT32_MAX / mElementCapacity);
}
// Free the allocated memory.
// @arg value must be in network byte order
// @arg pos location the value must be in.
void set(const TYPE& value, uint32_t pos) {
- CHECK_LT(pos, mTotalNumTableEntries * mEntryCapacity);
+ CHECK_LT(pos, mTotalNumTableEntries * ENTRY_SIZE);
typename List<TYPE *>::iterator it = mTableEntryList.begin();
- uint32_t iterations = (pos / (mElementCapacity * mEntryCapacity));
+ uint32_t iterations = (pos / (mElementCapacity * ENTRY_SIZE));
while (it != mTableEntryList.end() && iterations > 0) {
++it;
--iterations;
CHECK(it != mTableEntryList.end());
CHECK_EQ(iterations, 0);
- (*it)[(pos % (mElementCapacity * mEntryCapacity))] = value;
+ (*it)[(pos % (mElementCapacity * ENTRY_SIZE))] = value;
}
// Get the value at the given position by the given value.
// @arg pos location the value must be in.
// @return true if a value is found.
bool get(TYPE& value, uint32_t pos) const {
- if (pos >= mTotalNumTableEntries * mEntryCapacity) {
+ if (pos >= mTotalNumTableEntries * ENTRY_SIZE) {
return false;
}
typename List<TYPE *>::iterator it = mTableEntryList.begin();
- uint32_t iterations = (pos / (mElementCapacity * mEntryCapacity));
+ uint32_t iterations = (pos / (mElementCapacity * ENTRY_SIZE));
while (it != mTableEntryList.end() && iterations > 0) {
++it;
--iterations;
CHECK(it != mTableEntryList.end());
CHECK_EQ(iterations, 0);
- value = (*it)[(pos % (mElementCapacity * mEntryCapacity))];
+ value = (*it)[(pos % (mElementCapacity * ENTRY_SIZE))];
return true;
}
+ // adjusts all values by |adjust(value)|
+ void adjustEntries(
+ std::function<void(size_t /* ix */, TYPE(& /* entry */)[ENTRY_SIZE])> update) {
+ size_t nEntries = mTotalNumTableEntries + mNumValuesInCurrEntry / ENTRY_SIZE;
+ size_t ix = 0;
+ for (TYPE *entryArray : mTableEntryList) {
+ size_t num = std::min(nEntries, (size_t)mElementCapacity);
+ for (size_t i = 0; i < num; ++i) {
+ update(ix++, (TYPE(&)[ENTRY_SIZE])(*entryArray));
+ entryArray += ENTRY_SIZE;
+ }
+ nEntries -= num;
+ }
+ }
+
// Store a single value.
// @arg value must be in network byte order.
void add(const TYPE& value) {
CHECK_LT(mNumValuesInCurrEntry, mElementCapacity);
uint32_t nEntries = mTotalNumTableEntries % mElementCapacity;
- uint32_t nValues = mNumValuesInCurrEntry % mEntryCapacity;
+ uint32_t nValues = mNumValuesInCurrEntry % ENTRY_SIZE;
if (nEntries == 0 && nValues == 0) {
- mCurrTableEntriesElement = new TYPE[mEntryCapacity * mElementCapacity];
+ mCurrTableEntriesElement = new TYPE[ENTRY_SIZE * mElementCapacity];
CHECK(mCurrTableEntriesElement != NULL);
mTableEntryList.push_back(mCurrTableEntriesElement);
}
- uint32_t pos = nEntries * mEntryCapacity + nValues;
+ uint32_t pos = nEntries * ENTRY_SIZE + nValues;
mCurrTableEntriesElement[pos] = value;
++mNumValuesInCurrEntry;
- if ((mNumValuesInCurrEntry % mEntryCapacity) == 0) {
+ if ((mNumValuesInCurrEntry % ENTRY_SIZE) == 0) {
++mTotalNumTableEntries;
mNumValuesInCurrEntry = 0;
}
// 2. followed by the values in the table enties in order
// @arg writer the writer to actual write to the storage
void write(MPEG4Writer *writer) const {
- CHECK_EQ(mNumValuesInCurrEntry % mEntryCapacity, 0);
+ CHECK_EQ(mNumValuesInCurrEntry % ENTRY_SIZE, 0);
uint32_t nEntries = mTotalNumTableEntries;
writer->writeInt32(nEntries);
for (typename List<TYPE *>::iterator it = mTableEntryList.begin();
it != mTableEntryList.end(); ++it) {
CHECK_GT(nEntries, 0);
if (nEntries >= mElementCapacity) {
- writer->write(*it, sizeof(TYPE) * mEntryCapacity, mElementCapacity);
+ writer->write(*it, sizeof(TYPE) * ENTRY_SIZE, mElementCapacity);
nEntries -= mElementCapacity;
} else {
- writer->write(*it, sizeof(TYPE) * mEntryCapacity, nEntries);
+ writer->write(*it, sizeof(TYPE) * ENTRY_SIZE, nEntries);
break;
}
}
private:
uint32_t mElementCapacity; // # entries in an element
- uint32_t mEntryCapacity; // # of values in each entry
uint32_t mTotalNumTableEntries;
- uint32_t mNumValuesInCurrEntry; // up to mEntryCapacity
+ uint32_t mNumValuesInCurrEntry; // up to ENTRY_SIZE
TYPE *mCurrTableEntriesElement;
mutable List<TYPE *> mTableEntryList;
int32_t mTrackId;
int64_t mTrackDurationUs;
int64_t mMaxChunkDurationUs;
+ int64_t mLastDecodingTimeUs;
int64_t mEstimatedTrackSizeBytes;
int64_t mMdatSizeBytes;
List<MediaBuffer *> mChunkSamples;
bool mSamplesHaveSameSize;
- ListTableEntries<uint32_t> *mStszTableEntries;
+ ListTableEntries<uint32_t, 1> *mStszTableEntries;
- ListTableEntries<uint32_t> *mStcoTableEntries;
- ListTableEntries<off64_t> *mCo64TableEntries;
- ListTableEntries<uint32_t> *mStscTableEntries;
- ListTableEntries<uint32_t> *mStssTableEntries;
- ListTableEntries<uint32_t> *mSttsTableEntries;
- ListTableEntries<uint32_t> *mCttsTableEntries;
+ ListTableEntries<uint32_t, 1> *mStcoTableEntries;
+ ListTableEntries<off64_t, 1> *mCo64TableEntries;
+ ListTableEntries<uint32_t, 3> *mStscTableEntries;
+ ListTableEntries<uint32_t, 1> *mStssTableEntries;
+ ListTableEntries<uint32_t, 2> *mSttsTableEntries;
+ ListTableEntries<uint32_t, 2> *mCttsTableEntries;
int64_t mMinCttsOffsetTimeUs;
int64_t mMaxCttsOffsetTimeUs;
}
}
+off64_t MPEG4Writer::addMultipleLengthPrefixedSamples_l(MediaBuffer *buffer) {
+ off64_t old_offset = mOffset;
+
+ const size_t kExtensionNALSearchRange = 64; // bytes to look for non-VCL NALUs
+
+ const uint8_t *dataStart = (const uint8_t *)buffer->data() + buffer->range_offset();
+ const uint8_t *currentNalStart = dataStart;
+ const uint8_t *nextNalStart;
+ const uint8_t *data = dataStart;
+ size_t nextNalSize;
+ size_t searchSize = buffer->range_length() > kExtensionNALSearchRange ?
+ kExtensionNALSearchRange : buffer->range_length();
+
+ while (getNextNALUnit(&data, &searchSize, &nextNalStart,
+ &nextNalSize, true) == OK) {
+ size_t currentNalSize = nextNalStart - currentNalStart - 4 /* strip start-code */;
+ MediaBuffer *nalBuf = new MediaBuffer((void *)currentNalStart, currentNalSize);
+ addLengthPrefixedSample_l(nalBuf);
+ nalBuf->release();
+
+ currentNalStart = nextNalStart;
+ }
+
+ size_t currentNalOffset = currentNalStart - dataStart;
+ buffer->set_range(buffer->range_offset() + currentNalOffset,
+ buffer->range_length() - currentNalOffset);
+ addLengthPrefixedSample_l(buffer);
+
+ return old_offset;
+}
+
off64_t MPEG4Writer::addLengthPrefixedSample_l(MediaBuffer *buffer) {
off64_t old_offset = mOffset;
return OK;
}
+status_t MPEG4Writer::setTemporalLayerCount(uint32_t layerCount) {
+ if (layerCount > 9) {
+ return BAD_VALUE;
+ }
+
+ if (layerCount > 0) {
+ mMetaKeys->setInt32(kMetaKey_TemporalLayerCount, layerCount);
+ mMoovExtraSize += sizeof(kMetaKey_TemporalLayerCount) + 4 + 32;
+ }
+
+ return OK;
+}
+
void MPEG4Writer::write(const void *data, size_t size) {
write(data, 1, size);
}
mTrackDurationUs(0),
mEstimatedTrackSizeBytes(0),
mSamplesHaveSameSize(true),
- mStszTableEntries(new ListTableEntries<uint32_t>(1000, 1)),
- mStcoTableEntries(new ListTableEntries<uint32_t>(1000, 1)),
- mCo64TableEntries(new ListTableEntries<off64_t>(1000, 1)),
- mStscTableEntries(new ListTableEntries<uint32_t>(1000, 3)),
- mStssTableEntries(new ListTableEntries<uint32_t>(1000, 1)),
- mSttsTableEntries(new ListTableEntries<uint32_t>(1000, 2)),
- mCttsTableEntries(new ListTableEntries<uint32_t>(1000, 2)),
+ mStszTableEntries(new ListTableEntries<uint32_t, 1>(1000)),
+ mStcoTableEntries(new ListTableEntries<uint32_t, 1>(1000)),
+ mCo64TableEntries(new ListTableEntries<off64_t, 1>(1000)),
+ mStscTableEntries(new ListTableEntries<uint32_t, 3>(1000)),
+ mStssTableEntries(new ListTableEntries<uint32_t, 1>(1000)),
+ mSttsTableEntries(new ListTableEntries<uint32_t, 2>(1000)),
+ mCttsTableEntries(new ListTableEntries<uint32_t, 2>(1000)),
mCodecSpecificData(NULL),
mCodecSpecificDataSize(0),
mGotAllCodecSpecificData(false),
mIsMPEG4 = !strcasecmp(mime, MEDIA_MIMETYPE_VIDEO_MPEG4) ||
!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AAC);
+ // store temporal layer count
+ if (!mIsAudio) {
+ int32_t count;
+ if (mMeta->findInt32(kKeyTemporalLayerCount, &count) && count > 1) {
+ mOwner->setTemporalLayerCount(count);
+ }
+ }
+
setTimeScale();
}
List<MediaBuffer *>::iterator it = chunk->mSamples.begin();
off64_t offset = (chunk->mTrack->isAvc() || chunk->mTrack->isHevc())
- ? addLengthPrefixedSample_l(*it)
+ ? addMultipleLengthPrefixedSamples_l(*it)
: addSample_l(*it);
if (isFirstSample) {
mEstimatedTrackSizeBytes = 0;
mMdatSizeBytes = 0;
mMaxChunkDurationUs = 0;
+ mLastDecodingTimeUs = -1;
pthread_create(&mThread, &attr, ThreadWrapper, this);
pthread_attr_destroy(&attr);
int64_t decodingTimeUs;
CHECK(meta_data->findInt64(kKeyDecodingTime, &decodingTimeUs));
decodingTimeUs -= previousPausedDurationUs;
+
+ // ensure non-negative, monotonic decoding time
+ if (mLastDecodingTimeUs < 0) {
+ decodingTimeUs = std::max((int64_t)0, decodingTimeUs);
+ } else {
+ // increase decoding time by at least 1 tick
+ decodingTimeUs = std::max(
+ mLastDecodingTimeUs + divUp(1000000, mTimeScale), decodingTimeUs);
+ }
+
+ mLastDecodingTimeUs = decodingTimeUs;
cttsOffsetTimeUs =
timestampUs + kMaxCttsOffsetTimeUs - decodingTimeUs;
if (WARN_UNLESS(cttsOffsetTimeUs >= 0ll, "for %s track", trackName)) {
trackProgressStatus(timestampUs);
}
if (!hasMultipleTracks) {
- off64_t offset = (mIsAvc || mIsHevc) ? mOwner->addLengthPrefixedSample_l(copy)
+ off64_t offset = (mIsAvc || mIsHevc) ? mOwner->addMultipleLengthPrefixedSamples_l(copy)
: mOwner->addSample_l(copy);
uint32_t count = (mOwner->use32BitFileOffset()
mOwner->writeInt16(0x18); // depth
mOwner->writeInt16(-1); // predefined
- CHECK_LT(23 + mCodecSpecificDataSize, 128);
-
if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
writeMp4vEsdsBox();
} else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
void MPEG4Writer::Track::writeMp4vEsdsBox() {
CHECK(mCodecSpecificData);
CHECK_GT(mCodecSpecificDataSize, 0);
+
+ // Make sure all sizes encode to a single byte.
+ CHECK_LT(23 + mCodecSpecificDataSize, 128);
+
mOwner->beginBox("esds");
mOwner->writeInt32(0); // version=0, flags=0
mOwner->beginBox("ctts");
mOwner->writeInt32(0); // version=0, flags=0
- uint32_t duration;
- CHECK(mCttsTableEntries->get(duration, 1));
- duration = htonl(duration); // Back host byte order
- mCttsTableEntries->set(htonl(duration + getStartTimeOffsetScaledTime() - mMinCttsOffsetTimeUs), 1);
+ uint32_t delta = mMinCttsOffsetTimeUs - getStartTimeOffsetScaledTime();
+ mCttsTableEntries->adjustEntries([delta](size_t /* ix */, uint32_t (&value)[2]) {
+ // entries are <count, ctts> pairs; adjust only ctts
+ uint32_t duration = htonl(value[1]); // back to host byte order
+ value[1] = htonl(duration - delta);
+ });
mCttsTableEntries->write(mOwner);
mOwner->endBox(); // ctts
}
return NULL;
}
-void MediaCodecSource::setInputBufferTimeOffset(int64_t timeOffsetUs) {
+status_t MediaCodecSource::setInputBufferTimeOffset(int64_t timeOffsetUs) {
sp<AMessage> msg = new AMessage(kWhatSetInputBufferTimeOffset, mReflector);
msg->setInt64("time-offset-us", timeOffsetUs);
- postSynchronouslyAndReturnError(msg);
+ return postSynchronouslyAndReturnError(msg);
+}
+
+int64_t MediaCodecSource::getFirstSampleSystemTimeUs() {
+ sp<AMessage> msg = new AMessage(kWhatGetFirstSampleSystemTimeUs, mReflector);
+ sp<AMessage> response;
+ msg->postAndAwaitResponse(&response);
+ int64_t timeUs;
+ if (!response->findInt64("time-us", &timeUs)) {
+ timeUs = -1ll;
+ }
+ return timeUs;
}
status_t MediaCodecSource::start(MetaData* params) {
mEncoderDataSpace(0),
mGraphicBufferConsumer(consumer),
mInputBufferTimeOffsetUs(0),
+ mFirstSampleSystemTimeUs(-1ll),
+ mPausePending(false),
mFirstSampleTimeUs(-1ll),
mGeneration(0) {
CHECK(mLooper != NULL);
if (mbuf != NULL) {
CHECK(mbuf->meta_data()->findInt64(kKeyTime, &timeUs));
+ if (mFirstSampleSystemTimeUs < 0ll) {
+ mFirstSampleSystemTimeUs = systemTime() / 1000;
+ if (mPausePending) {
+ mPausePending = false;
+ onPause();
+ mbuf->release();
+ mAvailEncoderInputIndices.push_back(bufferIndex);
+ return OK;
+ }
+ }
+
timeUs += mInputBufferTimeOffsetUs;
// push decoding time for video, or drift time for audio
if (mFirstSampleTimeUs < 0ll) {
mFirstSampleTimeUs = timeUs;
}
-
int64_t driftTimeUs = 0;
if (mbuf->meta_data()->findInt64(kKeyDriftTime, &driftTimeUs)
&& driftTimeUs) {
if (mStarted) {
ALOGI("MediaCodecSource (%s) resuming", mIsVideo ? "video" : "audio");
+ if (mPausePending) {
+ mPausePending = false;
+ return OK;
+ }
if (mIsVideo) {
mEncoder->requestIDRFrame();
}
return OK;
}
+void MediaCodecSource::onPause() {
+ if (mFlags & FLAG_USE_SURFACE_INPUT) {
+ suspend();
+ } else {
+ CHECK(mPuller != NULL);
+ mPuller->pause();
+ }
+}
+
void MediaCodecSource::onMessageReceived(const sp<AMessage> &msg) {
switch (msg->what()) {
case kWhatPullerNotify:
}
MediaBuffer *mbuf = new MediaBuffer(outbuf->size());
- memcpy(mbuf->data(), outbuf->data(), outbuf->size());
+ mbuf->add_ref();
if (!(flags & MediaCodec::BUFFER_FLAG_CODECCONFIG)) {
if (mIsVideo) {
int64_t decodingTimeUs;
if (mFlags & FLAG_USE_SURFACE_INPUT) {
- // Time offset is not applied at
- // feedEncoderInputBuffer() in surface input case.
- timeUs += mInputBufferTimeOffsetUs;
+ if (mFirstSampleSystemTimeUs < 0ll) {
+ mFirstSampleSystemTimeUs = systemTime() / 1000;
+ if (mPausePending) {
+ mPausePending = false;
+ onPause();
+ mbuf->release();
+ break;
+ }
+ }
+ // Timestamp offset is already adjusted in GraphicBufferSource.
// GraphicBufferSource is supposed to discard samples
// queued before start, and offset timeUs by start time
CHECK_GE(timeUs, 0ll);
if (flags & MediaCodec::BUFFER_FLAG_SYNCFRAME) {
mbuf->meta_data()->setInt32(kKeyIsSyncFrame, true);
}
+ memcpy(mbuf->data(), outbuf->data(), outbuf->size());
mbuf->setObserver(this);
- mbuf->add_ref();
{
Mutexed<Output>::Locked output(mOutput);
case kWhatPause:
{
- if (mFlags & FLAG_USE_SURFACE_INPUT) {
- suspend();
+ if (mFirstSampleSystemTimeUs < 0) {
+ mPausePending = true;
} else {
- CHECK(mPuller != NULL);
- mPuller->pause();
+ onPause();
}
break;
}
{
sp<AReplyToken> replyID;
CHECK(msg->senderAwaitsResponse(&replyID));
-
+ status_t err = OK;
CHECK(msg->findInt64("time-offset-us", &mInputBufferTimeOffsetUs));
+ // Propagate the timestamp offset to GraphicBufferSource.
+ if (mIsVideo) {
+ sp<AMessage> params = new AMessage;
+ params->setInt64("time-offset-us", mInputBufferTimeOffsetUs);
+ err = mEncoder->setParameters(params);
+ }
+
+ sp<AMessage> response = new AMessage;
+ response->setInt32("err", err);
+ response->postReply(replyID);
+ break;
+ }
+ case kWhatGetFirstSampleSystemTimeUs:
+ {
+ sp<AReplyToken> replyID;
+ CHECK(msg->senderAwaitsResponse(&replyID));
+
sp<AMessage> response = new AMessage;
+ response->setInt64("time-us", mFirstSampleSystemTimeUs);
response->postReply(replyID);
break;
}
CHECK(meta->findCString(kKeyMIMEType, &mime));
sp<AMessage> format = new AMessage;
- convertMetaDataToMessage(source->getFormat(), &format);
+ if (convertMetaDataToMessage(source->getFormat(), &format) != OK) {
+ return NULL;
+ }
Vector<AString> matchingCodecs;
MediaCodecList::findMatchingCodecs(
".mpeg", ".ogg", ".mid", ".smf", ".imy", ".wma", ".aac",
".wav", ".amr", ".midi", ".xmf", ".rtttl", ".rtx", ".ota",
".mkv", ".mka", ".webm", ".ts", ".fl", ".flac", ".mxmf",
- ".avi", ".mpeg", ".mpg", ".awb", ".mpga"
+ ".avi", ".mpeg", ".mpg", ".awb", ".mpga", ".mov"
};
static const size_t kNumValidExtensions =
sizeof(kValidExtensions) / sizeof(kValidExtensions[0]);
{ "writer", METADATA_KEY_WRITER },
{ "compilation", METADATA_KEY_COMPILATION },
{ "isdrm", METADATA_KEY_IS_DRM },
+ { "date", METADATA_KEY_DATE },
{ "width", METADATA_KEY_VIDEO_WIDTH },
{ "height", METADATA_KEY_VIDEO_HEIGHT },
};
// TODO: Use Flexible color instead
videoFormat->setInt32("color-format", OMX_COLOR_FormatYUV420Planar);
+ // For the thumbnail extraction case, try to allocate single buffer
+ // in both input and output ports. NOTE: This request may fail if
+ // component requires more than that for decoding.
+ videoFormat->setInt32("android._num-input-buffers", 1);
+ videoFormat->setInt32("android._num-output-buffers", 1);
+
status_t err;
sp<ALooper> looper = new ALooper;
looper->start();
break;
}
+ if (img == NULL) {
+ ALOGE("error pushing blank frames: lock returned NULL buffer");
+ break;
+ }
*img = 0;
err = buf->unlock();
}
convertMessageToMetaDataColorAspects(msg, meta);
+
+ AString tsSchema;
+ if (msg->findString("ts-schema", &tsSchema)) {
+ unsigned int numLayers = 0;
+ unsigned int numBLayers = 0;
+ char dummy;
+ int tags = sscanf(tsSchema.c_str(), "android.generic.%u%c%u%c",
+ &numLayers, &dummy, &numBLayers, &dummy);
+ if ((tags == 1 || (tags == 3 && dummy == '+'))
+ && numLayers > 0 && numLayers < UINT32_MAX - numBLayers
+ && numLayers + numBLayers <= INT32_MAX) {
+ meta->setInt32(kKeyTemporalLayerCount, numLayers + numBLayers);
+ }
+ }
} else if (mime.startsWith("audio/")) {
int32_t numChannels;
if (msg->findInt32("channel-count", &numChannels)) {
virtual status_t read(
MediaBuffer **buffer, const ReadOptions *options = NULL);
+ virtual bool supportNonblockingRead() { return true; }
+
protected:
virtual ~WAVSource();
CHECK(!mStarted);
- mGroup = new MediaBufferGroup;
- mGroup->add_buffer(new MediaBuffer(kMaxFrameSize));
+ // some WAV files may have large audio buffers that use shared memory transfer.
+ mGroup = new MediaBufferGroup(4 /* buffers */, kMaxFrameSize);
if (mBitsPerSample == 8) {
// As a temporary buffer for 8->16 bit conversion.
MediaBuffer **out, const ReadOptions *options) {
*out = NULL;
+ if (options != nullptr && options->getNonBlocking() && !mGroup->has_buffers()) {
+ return WOULD_BLOCK;
+ }
+
int64_t seekTimeUs;
ReadOptions::SeekMode mode;
if (options != NULL && options->getSeekTo(&seekTimeUs, &mode)) {
return true;
}
+uint32_t FindAVCLayerId(const uint8_t *data, size_t size) {
+ CHECK(data != NULL);
+
+ const unsigned kSvcNalType = 0xE;
+ const unsigned kSvcNalSearchRange = 32;
+ // SVC NAL
+ // |---0 1110|1--- ----|---- ----|iii- ---|
+ // ^ ^
+ // NAL-type = 0xE layer-Id
+ //
+ // layer_id 0 is for base layer, while 1, 2, ... are enhancement layers.
+ // Layer n uses reference frames from layer 0, 1, ..., n-1.
+
+ uint32_t layerId = 0;
+ sp<ABuffer> svcNAL = FindNAL(
+ data, size > kSvcNalSearchRange ? kSvcNalSearchRange : size, kSvcNalType);
+ if (svcNAL != NULL && svcNAL->size() >= 4) {
+ layerId = (*(svcNAL->data() + 3) >> 5) & 0x7;
+ }
+ return layerId;
+}
+
sp<MetaData> MakeAACCodecSpecificData(
unsigned profile, unsigned sampling_freq_index,
unsigned channel_configuration) {
mSignalledError(false),
mStride(mWidth){
initPorts(
- kNumBuffers, INPUT_BUF_SIZE, kNumBuffers, CODEC_MIME_TYPE);
+ 1 /* numMinInputBuffers */, kNumBuffers, INPUT_BUF_SIZE,
+ 1 /* numMinOutputBuffers */, kNumBuffers, CODEC_MIME_TYPE);
GETTIME(&mTimeStart, NULL);
resetPlugin();
}
+bool SoftAVC::getVUIParams() {
+ IV_API_CALL_STATUS_T status;
+ ih264d_ctl_get_vui_params_ip_t s_ctl_get_vui_params_ip;
+ ih264d_ctl_get_vui_params_op_t s_ctl_get_vui_params_op;
+
+ s_ctl_get_vui_params_ip.e_cmd = IVD_CMD_VIDEO_CTL;
+ s_ctl_get_vui_params_ip.e_sub_cmd =
+ (IVD_CONTROL_API_COMMAND_TYPE_T)IH264D_CMD_CTL_GET_VUI_PARAMS;
+
+ s_ctl_get_vui_params_ip.u4_size =
+ sizeof(ih264d_ctl_get_vui_params_ip_t);
+
+ s_ctl_get_vui_params_op.u4_size = sizeof(ih264d_ctl_get_vui_params_op_t);
+
+ status = ivdec_api_function(
+ (iv_obj_t *)mCodecCtx, (void *)&s_ctl_get_vui_params_ip,
+ (void *)&s_ctl_get_vui_params_op);
+
+ if (status != IV_SUCCESS) {
+ ALOGW("Error in getting VUI params: 0x%x",
+ s_ctl_get_vui_params_op.u4_error_code);
+ return false;
+ }
+
+ int32_t primaries = s_ctl_get_vui_params_op.u1_colour_primaries;
+ int32_t transfer = s_ctl_get_vui_params_op.u1_tfr_chars;
+ int32_t coeffs = s_ctl_get_vui_params_op.u1_matrix_coeffs;
+ bool fullRange = s_ctl_get_vui_params_op.u1_video_full_range_flag;
+
+ ColorAspects colorAspects;
+ ColorUtils::convertIsoColorAspectsToCodecAspects(
+ primaries, transfer, coeffs, fullRange, colorAspects);
+
+ // Update color aspects if necessary.
+ if (colorAspectsDiffer(colorAspects, mBitstreamColorAspects)) {
+ mBitstreamColorAspects = colorAspects;
+ status_t err = handleColorAspectsChange();
+ CHECK(err == OK);
+ }
+ return true;
+}
+
bool SoftAVC::setDecodeArgs(
ivd_video_decode_ip_t *ps_dec_ip,
ivd_video_decode_op_t *ps_dec_op,
bool resChanged = (IVD_RES_CHANGED == (s_dec_op.u4_error_code & 0xFF));
+ getVUIParams();
+
GETTIME(&mTimeEnd, NULL);
/* Compute time taken for decode() */
TIME_DIFF(mTimeStart, mTimeEnd, timeTaken);
continue;
}
+ // Combine the resolution change and coloraspects change in one PortSettingChange event
+ // if necessary.
if ((0 < s_dec_op.u4_pic_wd) && (0 < s_dec_op.u4_pic_ht)) {
uint32_t width = s_dec_op.u4_pic_wd;
uint32_t height = s_dec_op.u4_pic_ht;
bool portWillReset = false;
handlePortSettingsChange(&portWillReset, width, height);
-
if (portWillReset) {
resetDecoder();
return;
}
+ } else if (mUpdateColorAspects) {
+ notify(OMX_EventPortSettingsChanged, kOutputPortIndex,
+ kDescribeColorAspectsIndex, NULL);
+ mUpdateColorAspects = false;
+ return;
}
if (s_dec_op.u4_output_present) {
}
}
+int SoftAVC::getColorAspectPreference() {
+ return kPreferBitstream;
+}
+
} // namespace android
android::SoftOMXComponent *createSoftOMXComponent(
virtual void onQueueFilled(OMX_U32 portIndex);
virtual void onPortFlushCompleted(OMX_U32 portIndex);
virtual void onReset();
+ virtual int getColorAspectPreference();
private:
// Number of input and output buffers
enum {
OMX_BUFFERHEADERTYPE *outHeader,
size_t timeStampIx);
+ bool getVUIParams();
+
DISALLOW_EVIL_CONSTRUCTORS(SoftAVC);
};
if ((avcType->nAllowedPictureTypes & OMX_VIDEO_PictureTypeB) &&
avcType->nPFrames) {
- mBframes = avcType->nBFrames / avcType->nPFrames;
+ mBframes = avcType->nBFrames;
}
- mIInterval = avcType->nPFrames + avcType->nBFrames;
+ mIInterval = (avcType->nPFrames + 1) * (avcType->nBFrames + 1);
mConstrainedIntraFlag = avcType->bconstIpred;
if (OMX_VIDEO_AVCLoopFilterDisable == avcType->eLoopFilterMode)
|| avcType->bDirect8x8Inference != OMX_FALSE
|| avcType->bDirectSpatialTemporal != OMX_FALSE
|| avcType->nCabacInitIdc != 0) {
- return OMX_ErrorUndefined;
+ // OMX does not allow a way to signal what values are wrong, so it's
+ // best for components to just do best effort in supporting these values
+ ALOGV("ignoring unsupported settings");
}
if (OK != ConvertOmxAvcLevelToAvcSpecLevel(avcType->eLevel, &mAVCEncLevel)) {
return OK;
}
+bool SoftHEVC::getVUIParams() {
+ IV_API_CALL_STATUS_T status;
+ ihevcd_cxa_ctl_get_vui_params_ip_t s_ctl_get_vui_params_ip;
+ ihevcd_cxa_ctl_get_vui_params_op_t s_ctl_get_vui_params_op;
+
+ s_ctl_get_vui_params_ip.e_cmd = IVD_CMD_VIDEO_CTL;
+ s_ctl_get_vui_params_ip.e_sub_cmd =
+ (IVD_CONTROL_API_COMMAND_TYPE_T)IHEVCD_CXA_CMD_CTL_GET_VUI_PARAMS;
+
+ s_ctl_get_vui_params_ip.u4_size =
+ sizeof(ihevcd_cxa_ctl_get_vui_params_ip_t);
+
+ s_ctl_get_vui_params_op.u4_size = sizeof(ihevcd_cxa_ctl_get_vui_params_op_t);
+
+ status = ivdec_api_function(
+ (iv_obj_t *)mCodecCtx, (void *)&s_ctl_get_vui_params_ip,
+ (void *)&s_ctl_get_vui_params_op);
+
+ if (status != IV_SUCCESS) {
+ ALOGW("Error in getting VUI params: 0x%x",
+ s_ctl_get_vui_params_op.u4_error_code);
+ return false;
+ }
+
+ int32_t primaries = s_ctl_get_vui_params_op.u1_colour_primaries;
+ int32_t transfer = s_ctl_get_vui_params_op.u1_transfer_characteristics;
+ int32_t coeffs = s_ctl_get_vui_params_op.u1_matrix_coefficients;
+ bool fullRange = s_ctl_get_vui_params_op.u1_video_full_range_flag;
+
+ ColorAspects colorAspects;
+ ColorUtils::convertIsoColorAspectsToCodecAspects(
+ primaries, transfer, coeffs, fullRange, colorAspects);
+
+ // Update color aspects if necessary.
+ if (colorAspectsDiffer(colorAspects, mBitstreamColorAspects)) {
+ mBitstreamColorAspects = colorAspects;
+ status_t err = handleColorAspectsChange();
+ CHECK(err == OK);
+ }
+ return true;
+}
+
status_t SoftHEVC::resetDecoder() {
ivd_ctl_reset_ip_t s_ctl_ip;
ivd_ctl_reset_op_t s_ctl_op;
bool resChanged = (IVD_RES_CHANGED == (s_dec_op.u4_error_code & 0xFF));
+ getVUIParams();
+
GETTIME(&mTimeEnd, NULL);
/* Compute time taken for decode() */
TIME_DIFF(mTimeStart, mTimeEnd, timeTaken);
continue;
}
+ // Combine the resolution change and coloraspects change in one PortSettingChange event
+ // if necessary.
if ((0 < s_dec_op.u4_pic_wd) && (0 < s_dec_op.u4_pic_ht)) {
uint32_t width = s_dec_op.u4_pic_wd;
uint32_t height = s_dec_op.u4_pic_ht;
resetDecoder();
return;
}
+ } else if (mUpdateColorAspects) {
+ notify(OMX_EventPortSettingsChanged, kOutputPortIndex,
+ kDescribeColorAspectsIndex, NULL);
+ mUpdateColorAspects = false;
+ return;
}
if (s_dec_op.u4_output_present) {
}
}
+int SoftHEVC::getColorAspectPreference() {
+ return kPreferBitstream;
+}
+
} // namespace android
android::SoftOMXComponent *createSoftOMXComponent(const char *name,
virtual void onQueueFilled(OMX_U32 portIndex);
virtual void onPortFlushCompleted(OMX_U32 portIndex);
virtual void onReset();
+ virtual int getColorAspectPreference();
private:
// Number of input and output buffers
enum {
OMX_BUFFERHEADERTYPE *outHeader,
size_t timeStampIx);
+ bool getVUIParams();
+
DISALLOW_EVIL_CONSTRUCTORS (SoftHEVC);
};
176 /* width */, 144 /* height */,
callbacks, appData, component),
mEncodeMode(COMBINE_MODE_WITH_ERR_RES),
- mIDRFrameRefreshIntervalInSec(1),
+ mKeyFrameInterval(30),
mNumInputFrames(-1),
mStarted(false),
mSawInputEOS(false),
}
// Set IDR frame refresh interval
- if (mIDRFrameRefreshIntervalInSec < 0) {
- mEncParams->intraPeriod = -1;
- } else if (mIDRFrameRefreshIntervalInSec == 0) {
- mEncParams->intraPeriod = 1; // All I frames
- } else {
- mEncParams->intraPeriod =
- (mIDRFrameRefreshIntervalInSec * mFramerate) >> 16;
- }
+ mEncParams->intraPeriod = mKeyFrameInterval;
mEncParams->numIntraMB = 0;
mEncParams->sceneDetect = PV_ON;
return OMX_ErrorUndefined;
}
+ mKeyFrameInterval = int32_t(mpeg4type->nPFrames + 1);
+
return OMX_ErrorNone;
}
} InputBufferInfo;
MP4EncodingMode mEncodeMode;
- int32_t mIDRFrameRefreshIntervalInSec;
+ int32_t mKeyFrameInterval; // 1: all I-frames, <0: infinite
int64_t mNumInputFrames;
bool mStarted;
resetPlugin();
}
+bool SoftMPEG2::getSeqInfo() {
+ IV_API_CALL_STATUS_T status;
+ impeg2d_ctl_get_seq_info_ip_t s_ctl_get_seq_info_ip;
+ impeg2d_ctl_get_seq_info_op_t s_ctl_get_seq_info_op;
+
+ s_ctl_get_seq_info_ip.e_cmd = IVD_CMD_VIDEO_CTL;
+ s_ctl_get_seq_info_ip.e_sub_cmd =
+ (IVD_CONTROL_API_COMMAND_TYPE_T)IMPEG2D_CMD_CTL_GET_SEQ_INFO;
+
+ s_ctl_get_seq_info_ip.u4_size = sizeof(impeg2d_ctl_get_seq_info_ip_t);
+ s_ctl_get_seq_info_op.u4_size = sizeof(impeg2d_ctl_get_seq_info_op_t);
+
+ status = ivdec_api_function(
+ (iv_obj_t *)mCodecCtx, (void *)&s_ctl_get_seq_info_ip,
+ (void *)&s_ctl_get_seq_info_op);
+
+ if (status != IV_SUCCESS) {
+ ALOGW("Error in getting Sequence info: 0x%x",
+ s_ctl_get_seq_info_op.u4_error_code);
+ return false;
+ }
+
+
+ int32_t primaries = s_ctl_get_seq_info_op.u1_colour_primaries;
+ int32_t transfer = s_ctl_get_seq_info_op.u1_transfer_characteristics;
+ int32_t coeffs = s_ctl_get_seq_info_op.u1_matrix_coefficients;
+ bool fullRange = false; // mpeg2 video has limited range.
+
+ ColorAspects colorAspects;
+ ColorUtils::convertIsoColorAspectsToCodecAspects(
+ primaries, transfer, coeffs, fullRange, colorAspects);
+
+ // Update color aspects if necessary.
+ if (colorAspectsDiffer(colorAspects, mBitstreamColorAspects)) {
+ mBitstreamColorAspects = colorAspects;
+ status_t err = handleColorAspectsChange();
+ CHECK(err == OK);
+ }
+ return true;
+}
+
OMX_ERRORTYPE SoftMPEG2::internalSetParameter(OMX_INDEXTYPE index, const OMX_PTR params) {
const uint32_t oldWidth = mWidth;
const uint32_t oldHeight = mHeight;
bool unsupportedDimensions = (IMPEG2D_UNSUPPORTED_DIMENSIONS == s_dec_op.u4_error_code);
bool resChanged = (IVD_RES_CHANGED == (s_dec_op.u4_error_code & 0xFF));
+ getSeqInfo();
+
GETTIME(&mTimeEnd, NULL);
/* Compute time taken for decode() */
TIME_DIFF(mTimeStart, mTimeEnd, timeTaken);
continue;
}
+ // Combine the resolution change and coloraspects change in one PortSettingChange event
+ // if necessary.
if ((0 < s_dec_op.u4_pic_wd) && (0 < s_dec_op.u4_pic_ht)) {
uint32_t width = s_dec_op.u4_pic_wd;
uint32_t height = s_dec_op.u4_pic_ht;
resetDecoder();
return;
}
+ } else if (mUpdateColorAspects) {
+ notify(OMX_EventPortSettingsChanged, kOutputPortIndex,
+ kDescribeColorAspectsIndex, NULL);
+ mUpdateColorAspects = false;
+ return;
}
if (s_dec_op.u4_output_present) {
}
}
+int SoftMPEG2::getColorAspectPreference() {
+ return kPreferBitstream;
+}
+
} // namespace android
android::SoftOMXComponent *createSoftOMXComponent(
virtual void onQueueFilled(OMX_U32 portIndex);
virtual void onPortFlushCompleted(OMX_U32 portIndex);
virtual void onReset();
+ virtual int getColorAspectPreference();
virtual OMX_ERRORTYPE internalSetParameter(OMX_INDEXTYPE index, const OMX_PTR params);
private:
// Number of input and output buffers
OMX_BUFFERHEADERTYPE *outHeader,
size_t timeStampIx);
+ bool getSeqInfo();
+
DISALLOW_EVIL_CONSTRUCTORS(SoftMPEG2);
};
LOCAL_C_INCLUDES := \
$(TOP)/frameworks/native/include/media/openmax \
- $(TOP)/hardware/msm7k
+ $(TOP)/hardware/msm7k \
+ $(TOP)/external/libyuv/files/include
+
+LOCAL_STATIC_LIBRARIES := \
+ libyuv_static \
LOCAL_CFLAGS += -Werror
LOCAL_CLANG := true
#include <media/stagefright/ColorConverter.h>
#include <media/stagefright/MediaErrors.h>
+#include "libyuv/convert_from.h"
+
+#define USE_LIBYUV
+
namespace android {
ColorConverter::ColorConverter(
switch (mSrcFormat) {
case OMX_COLOR_FormatYUV420Planar:
+#ifdef USE_LIBYUV
+ err = convertYUV420PlanarUseLibYUV(src, dst);
+#else
err = convertYUV420Planar(src, dst);
+#endif
break;
case OMX_COLOR_FormatCbYCrY:
return OK;
}
+status_t ColorConverter::convertYUV420PlanarUseLibYUV(
+ const BitmapParams &src, const BitmapParams &dst) {
+ if (!((src.mCropLeft & 1) == 0
+ && src.cropWidth() == dst.cropWidth()
+ && src.cropHeight() == dst.cropHeight())) {
+ return ERROR_UNSUPPORTED;
+ }
+
+ uint16_t *dst_ptr = (uint16_t *)dst.mBits
+ + dst.mCropTop * dst.mWidth + dst.mCropLeft;
+
+ const uint8_t *src_y =
+ (const uint8_t *)src.mBits + src.mCropTop * src.mWidth + src.mCropLeft;
+
+ const uint8_t *src_u =
+ (const uint8_t *)src_y + src.mWidth * src.mHeight
+ + src.mCropTop * (src.mWidth / 2) + src.mCropLeft / 2;
+
+ const uint8_t *src_v =
+ src_u + (src.mWidth / 2) * (src.mHeight / 2);
+
+
+ libyuv::I420ToRGB565(src_y, src.mWidth, src_u, src.mWidth / 2, src_v, src.mWidth / 2,
+ (uint8 *)dst_ptr, dst.mWidth * 2, dst.mWidth, dst.mHeight);
+
+ return OK;
+}
+
status_t ColorConverter::convertYUV420Planar(
const BitmapParams &src, const BitmapParams &dst) {
if (!((src.mCropLeft & 1) == 0
return NULL;
}
+bool AMessage::findAsFloat(const char *name, float *value) const {
+ size_t i = findItemIndex(name, strlen(name));
+ if (i < mNumItems) {
+ const Item *item = &mItems[i];
+ switch (item->mType) {
+ case kTypeFloat:
+ *value = item->u.floatValue;
+ return true;
+ case kTypeDouble:
+ *value = (float)item->u.doubleValue;
+ return true;
+ case kTypeInt64:
+ *value = (float)item->u.int64Value;
+ return true;
+ case kTypeInt32:
+ *value = (float)item->u.int32Value;
+ return true;
+ case kTypeSize:
+ *value = (float)item->u.sizeValue;
+ return true;
+ default:
+ return false;
+ }
+ }
+ return false;
+}
+
bool AMessage::contains(const char *name) const {
size_t i = findItemIndex(name, strlen(name));
return i < mNumItems;
namespace android {
+/* static */
+std::atomic_int_least32_t MediaBuffer::mUseSharedMemory(0);
+
MediaBuffer::MediaBuffer(void *data, size_t size)
: mObserver(NULL),
- mNextBuffer(NULL),
mRefCount(0),
mData(data),
mSize(size),
MediaBuffer::MediaBuffer(size_t size)
: mObserver(NULL),
- mNextBuffer(NULL),
mRefCount(0),
mData(NULL),
mSize(size),
mOwnsData(true),
mMetaData(new MetaData),
mOriginal(NULL) {
- if (size < kSharedMemThreshold) {
+ if (size < kSharedMemThreshold
+ || std::atomic_load_explicit(&mUseSharedMemory, std::memory_order_seq_cst) == 0) {
mData = malloc(size);
} else {
- sp<MemoryDealer> memoryDealer = new MemoryDealer(size, "MediaBuffer");
- mMemory = memoryDealer->allocate(size);
+ ALOGV("creating memoryDealer");
+ sp<MemoryDealer> memoryDealer =
+ new MemoryDealer(size + sizeof(SharedControl), "MediaBuffer");
+ mMemory = memoryDealer->allocate(size + sizeof(SharedControl));
if (mMemory == NULL) {
ALOGW("Failed to allocate shared memory, trying regular allocation!");
mData = malloc(size);
ALOGE("Out of memory");
}
} else {
- mData = mMemory->pointer();
+ getSharedControl()->clear();
+ mData = (uint8_t *)mMemory->pointer() + sizeof(SharedControl);
ALOGV("Allocated shared mem buffer of size %zu @ %p", size, mData);
}
}
MediaBuffer::MediaBuffer(const sp<GraphicBuffer>& graphicBuffer)
: mObserver(NULL),
- mNextBuffer(NULL),
mRefCount(0),
mData(NULL),
mSize(1),
MediaBuffer::MediaBuffer(const sp<ABuffer> &buffer)
: mObserver(NULL),
- mNextBuffer(NULL),
mRefCount(0),
mData(buffer->data()),
mSize(buffer->size()),
void MediaBuffer::release() {
if (mObserver == NULL) {
+ if (mMemory.get() != nullptr) {
+ // See if there is a pending release and there are no observers.
+ // Ideally this never happens.
+ while (addPendingRelease(-1) > 0) {
+ __sync_fetch_and_sub(&mRefCount, 1);
+ }
+ addPendingRelease(1);
+ }
CHECK_EQ(mRefCount, 0);
delete this;
return;
mOriginal->release();
mOriginal = NULL;
}
+
+ if (mMemory.get() != nullptr) {
+ getSharedControl()->setDeadObject();
+ }
}
void MediaBuffer::setObserver(MediaBufferObserver *observer) {
mObserver = observer;
}
-void MediaBuffer::setNextBuffer(MediaBuffer *buffer) {
- mNextBuffer = buffer;
-}
-
-MediaBuffer *MediaBuffer::nextBuffer() {
- return mNextBuffer;
-}
-
int MediaBuffer::refcount() const {
return mRefCount;
}
namespace android {
-MediaBufferGroup::MediaBufferGroup()
- : mFirstBuffer(NULL),
- mLastBuffer(NULL) {
+// std::min is not constexpr in C++11
+template<typename T>
+constexpr T MIN(const T &a, const T &b) { return a <= b ? a : b; }
+
+// MediaBufferGroup may create shared memory buffers at a
+// smaller threshold than an isolated new MediaBuffer.
+static const size_t kSharedMemoryThreshold = MIN(
+ (size_t)MediaBuffer::kSharedMemThreshold, (size_t)(4 * 1024));
+
+MediaBufferGroup::MediaBufferGroup(size_t growthLimit) :
+ mGrowthLimit(growthLimit) {
}
-MediaBufferGroup::~MediaBufferGroup() {
- MediaBuffer *next;
- for (MediaBuffer *buffer = mFirstBuffer; buffer != NULL;
- buffer = next) {
- next = buffer->nextBuffer();
+MediaBufferGroup::MediaBufferGroup(size_t buffers, size_t buffer_size, size_t growthLimit)
+ : mGrowthLimit(growthLimit) {
- CHECK_EQ(buffer->refcount(), 0);
+ if (buffer_size >= kSharedMemoryThreshold) {
+ ALOGD("creating MemoryDealer");
+ // Using a single MemoryDealer is efficient for a group of shared memory objects.
+ // This loop guarantees that we use shared memory (no fallback to malloc).
- buffer->setObserver(NULL);
+ size_t alignment = MemoryDealer::getAllocationAlignment();
+ size_t augmented_size = buffer_size + sizeof(MediaBuffer::SharedControl);
+ size_t total = (augmented_size + alignment - 1) / alignment * alignment * buffers;
+ sp<MemoryDealer> memoryDealer = new MemoryDealer(total, "MediaBufferGroup");
+
+ for (size_t i = 0; i < buffers; ++i) {
+ sp<IMemory> mem = memoryDealer->allocate(augmented_size);
+ if (mem.get() == nullptr) {
+ ALOGW("Only allocated %zu shared buffers of size %zu", i, buffer_size);
+ break;
+ }
+ MediaBuffer *buffer = new MediaBuffer(mem);
+ buffer->getSharedControl()->clear();
+ add_buffer(buffer);
+ }
+ return;
+ }
+
+ // Non-shared memory allocation.
+ for (size_t i = 0; i < buffers; ++i) {
+ MediaBuffer *buffer = new MediaBuffer(buffer_size);
+ if (buffer->data() == nullptr) {
+ delete buffer; // don't call release, it's not properly formed
+ ALOGW("Only allocated %zu malloc buffers of size %zu", i, buffer_size);
+ break;
+ }
+ add_buffer(buffer);
+ }
+}
+
+MediaBufferGroup::~MediaBufferGroup() {
+ for (MediaBuffer *buffer : mBuffers) {
+ buffer->resolvePendingRelease();
+ // If we don't release it, perhaps noone will release it.
+ LOG_ALWAYS_FATAL_IF(buffer->refcount() != 0,
+ "buffer refcount %p = %d != 0", buffer, buffer->refcount());
+ // actually delete it.
+ buffer->setObserver(nullptr);
buffer->release();
}
}
Mutex::Autolock autoLock(mLock);
buffer->setObserver(this);
+ mBuffers.emplace_back(buffer);
+ // optionally: mGrowthLimit = max(mGrowthLimit, mBuffers.size());
+}
+
+void MediaBufferGroup::gc(size_t freeBuffers) {
+ Mutex::Autolock autoLock(mLock);
- if (mLastBuffer) {
- mLastBuffer->setNextBuffer(buffer);
- } else {
- mFirstBuffer = buffer;
+ size_t freeCount = 0;
+ for (auto it = mBuffers.begin(); it != mBuffers.end(); ) {
+ (*it)->resolvePendingRelease();
+ if ((*it)->isDeadObject()) {
+ // The MediaBuffer has been deleted, why is it in the MediaBufferGroup?
+ LOG_ALWAYS_FATAL("buffer(%p) has dead object with refcount %d",
+ (*it), (*it)->refcount());
+ } else if ((*it)->refcount() == 0 && ++freeCount > freeBuffers) {
+ (*it)->setObserver(nullptr);
+ (*it)->release();
+ it = mBuffers.erase(it);
+ } else {
+ ++it;
+ }
}
+}
- mLastBuffer = buffer;
+bool MediaBufferGroup::has_buffers() {
+ if (mBuffers.size() < mGrowthLimit) {
+ return true; // We can add more buffers internally.
+ }
+ for (MediaBuffer *buffer : mBuffers) {
+ buffer->resolvePendingRelease();
+ if (buffer->refcount() == 0) {
+ return true;
+ }
+ }
+ return false;
}
status_t MediaBufferGroup::acquire_buffer(
MediaBuffer **out, bool nonBlocking, size_t requestedSize) {
Mutex::Autolock autoLock(mLock);
-
for (;;) {
- MediaBuffer *freeBuffer = NULL;
- MediaBuffer *freeBufferPrevious = NULL;
- MediaBuffer *buffer = NULL;
- MediaBuffer *bufferPrevious = NULL;
size_t smallest = requestedSize;
- for (buffer = mFirstBuffer;
- buffer != NULL; buffer = buffer->nextBuffer()) {
- if (buffer->refcount() == 0) {
- if (buffer->size() >= requestedSize) {
- break;
- } else if (buffer->size() < smallest) {
- freeBuffer = buffer;
- freeBufferPrevious = bufferPrevious;
- }
+ MediaBuffer *buffer = nullptr;
+ auto free = mBuffers.end();
+ for (auto it = mBuffers.begin(); it != mBuffers.end(); ++it) {
+ (*it)->resolvePendingRelease();
+ if ((*it)->refcount() == 0) {
+ const size_t size = (*it)->size();
+ if (size >= requestedSize) {
+ buffer = *it;
+ break;
+ }
+ if (size < smallest) {
+ smallest = size; // always free the smallest buf
+ free = it;
+ }
}
- bufferPrevious = buffer;
}
-
- if (buffer == NULL && freeBuffer != NULL) {
- ALOGV("allocate new buffer, requested size %zu vs available %zu",
- requestedSize, freeBuffer->size());
- size_t allocateSize = requestedSize;
- if (requestedSize < SIZE_MAX / 3) {
- allocateSize = requestedSize * 3 / 2;
- }
- MediaBuffer *newBuffer = new MediaBuffer(allocateSize);
- newBuffer->setObserver(this);
- if (freeBuffer == mFirstBuffer) {
- mFirstBuffer = newBuffer;
- }
- if (freeBuffer == mLastBuffer) {
- mLastBuffer = newBuffer;
+ if (buffer == nullptr
+ && (free != mBuffers.end() || mBuffers.size() < mGrowthLimit)) {
+ // We alloc before we free so failure leaves group unchanged.
+ const size_t allocateSize = requestedSize < SIZE_MAX / 3 * 2 /* NB: ordering */ ?
+ requestedSize * 3 / 2 : requestedSize;
+ buffer = new MediaBuffer(allocateSize);
+ if (buffer->data() == nullptr) {
+ ALOGE("Allocation failure for size %zu", allocateSize);
+ delete buffer; // Invalid alloc, prefer not to call release.
+ buffer = nullptr;
+ } else {
+ buffer->setObserver(this);
+ if (free != mBuffers.end()) {
+ ALOGV("reallocate buffer, requested size %zu vs available %zu",
+ requestedSize, (*free)->size());
+ (*free)->setObserver(nullptr);
+ (*free)->release();
+ *free = buffer; // in-place replace
+ } else {
+ ALOGV("allocate buffer, requested size %zu", requestedSize);
+ mBuffers.emplace_back(buffer);
+ }
}
- newBuffer->setNextBuffer(freeBuffer->nextBuffer());
- if (freeBufferPrevious != NULL) {
- freeBufferPrevious->setNextBuffer(newBuffer);
- }
- freeBuffer->setObserver(NULL);
- freeBuffer->release();
-
- buffer = newBuffer;
}
-
- if (buffer != NULL) {
+ if (buffer != nullptr) {
buffer->add_ref();
buffer->reset();
-
*out = buffer;
- goto exit;
+ return OK;
}
-
if (nonBlocking) {
- *out = NULL;
+ *out = nullptr;
return WOULD_BLOCK;
}
-
- // All buffers are in use. Block until one of them is returned to us.
+ // All buffers are in use, block until one of them is returned.
mCondition.wait(mLock);
}
-
-exit:
- return OK;
+ // Never gets here.
}
void MediaBufferGroup::signalBufferReturned(MediaBuffer *) {
- Mutex::Autolock autoLock(mLock);
mCondition.signal();
}
status_t mInitCheck;
bool mHasVideo;
uint32_t mHeaderTimescale;
+ bool mIsQT;
Track *mFirstTrack, *mLastTrack;
#include "SimpleSoftOMXComponent.h"
#include <media/stagefright/foundation/AHandlerReflector.h>
+#include <media/stagefright/foundation/ColorUtils.h>
#include <media/IOMX.h>
#include <utils/RefBase.h>
OMX_COMPONENTTYPE **component);
protected:
+ enum {
+ kDescribeColorAspectsIndex = kPrepareForAdaptivePlaybackIndex + 1,
+ };
+
+ enum {
+ kNotSupported,
+ kPreferBitstream,
+ kPreferContainer,
+ };
+
virtual void onPortEnableCompleted(OMX_U32 portIndex, bool enabled);
virtual void onReset();
virtual OMX_ERRORTYPE getConfig(
OMX_INDEXTYPE index, OMX_PTR params);
+ virtual OMX_ERRORTYPE setConfig(
+ OMX_INDEXTYPE index, const OMX_PTR params);
+
virtual OMX_ERRORTYPE getExtensionIndex(
const char *name, OMX_INDEXTYPE *index);
+ virtual bool supportsDescribeColorAspects();
+
+ virtual int getColorAspectPreference();
+
+ // This function sets both minimum buffer count and actual buffer count of
+ // input port to be |numInputBuffers|. It will also set both minimum buffer
+ // count and actual buffer count of output port to be |numOutputBuffers|.
void initPorts(OMX_U32 numInputBuffers,
OMX_U32 inputBufferSize,
OMX_U32 numOutputBuffers,
const char *mimeType,
OMX_U32 minCompressionRatio = 1u);
+ // This function sets input port's minimum buffer count to |numMinInputBuffers|,
+ // sets input port's actual buffer count to |numInputBuffers|, sets output port's
+ // minimum buffer count to |numMinOutputBuffers| and sets output port's actual buffer
+ // count to be |numOutputBuffers|.
+ void initPorts(OMX_U32 numMinInputBuffers,
+ OMX_U32 numInputBuffers,
+ OMX_U32 inputBufferSize,
+ OMX_U32 numMinOutputBuffers,
+ OMX_U32 numOutputBuffers,
+ const char *mimeType,
+ OMX_U32 minCompressionRatio = 1u);
+
virtual void updatePortDefinitions(bool updateCrop = true, bool updateInputSize = false);
uint32_t outputBufferWidth();
kCropSet,
kCropChanged,
};
+
+ // This function will handle several port change events which include
+ // size changed, crop changed, stride changed and coloraspects changed.
+ // It will trigger OMX_EventPortSettingsChanged event if necessary.
void handlePortSettingsChange(
bool *portWillReset, uint32_t width, uint32_t height,
CropSettingsMode cropSettingsMode = kCropUnSet, bool fakeStride = false);
AWAITING_ENABLED
} mOutputPortSettingsChange;
+ bool mUpdateColorAspects;
+
+ Mutex mColorAspectsLock;
+ // color aspects passed from the framework.
+ ColorAspects mDefaultColorAspects;
+ // color aspects parsed from the bitstream.
+ ColorAspects mBitstreamColorAspects;
+ // final color aspects after combining the above two aspects.
+ ColorAspects mFinalColorAspects;
+
+ bool colorAspectsDiffer(const ColorAspects &a, const ColorAspects &b);
+
+ // This functions takes two color aspects and updates the mFinalColorAspects
+ // based on |preferredAspects|.
+ void updateFinalColorAspects(
+ const ColorAspects &otherAspects, const ColorAspects &preferredAspects);
+
+ // This function will update the mFinalColorAspects based on codec preference.
+ status_t handleColorAspectsChange();
+
+ // Helper function to dump the ColorAspects.
+ void dumpColorAspects(const ColorAspects &colorAspects);
+
private:
uint32_t mMinInputBufferSize;
uint32_t mMinCompressionRatio;
bool IsIDR(const sp<ABuffer> &accessUnit);
bool IsAVCReferenceFrame(const sp<ABuffer> &accessUnit);
+uint32_t FindAVCLayerId(const uint8_t *data, size_t size);
const char *AVCProfileToString(uint8_t profile);
mTimePerCaptureUs(-1ll),
mTimePerFrameUs(-1ll),
mPrevCaptureUs(-1ll),
- mPrevFrameUs(-1ll) {
+ mPrevFrameUs(-1ll),
+ mInputBufferTimeOffsetUs(0ll) {
ALOGV("GraphicBufferSource w=%u h=%u c=%u",
bufferWidth, bufferHeight, bufferCount);
int64_t GraphicBufferSource::getTimestamp(const BufferItem &item) {
int64_t timeUs = item.mTimestamp / 1000;
+ timeUs += mInputBufferTimeOffsetUs;
- if (mTimePerCaptureUs > 0ll) {
+ if (mTimePerCaptureUs > 0ll
+ && (mTimePerCaptureUs > 2 * mTimePerFrameUs
+ || mTimePerFrameUs > 2 * mTimePerCaptureUs)) {
// Time lapse or slow motion mode
if (mPrevCaptureUs < 0ll) {
// first capture
static_cast<long long>(mPrevFrameUs));
return mPrevFrameUs;
- } else if (mMaxTimestampGapUs > 0ll) {
- /* Cap timestamp gap between adjacent frames to specified max
- *
- * In the scenario of cast mirroring, encoding could be suspended for
- * prolonged periods. Limiting the pts gap to workaround the problem
- * where encoder's rate control logic produces huge frames after a
- * long period of suspension.
- */
-
+ } else {
int64_t originalTimeUs = timeUs;
- if (mPrevOriginalTimeUs >= 0ll) {
- if (originalTimeUs < mPrevOriginalTimeUs) {
+ if (originalTimeUs <= mPrevOriginalTimeUs) {
// Drop the frame if it's going backward in time. Bad timestamp
// could disrupt encoder's rate control completely.
- ALOGW("Dropping frame that's going backward in time");
- return -1;
- }
- int64_t timestampGapUs = originalTimeUs - mPrevOriginalTimeUs;
- timeUs = (timestampGapUs < mMaxTimestampGapUs ?
+ ALOGW("Dropping frame that's going backward in time");
+ return -1;
+ }
+
+ if (mMaxTimestampGapUs > 0ll) {
+ //TODO: Fix the case when mMaxTimestampGapUs and mTimePerCaptureUs are both set.
+
+ /* Cap timestamp gap between adjacent frames to specified max
+ *
+ * In the scenario of cast mirroring, encoding could be suspended for
+ * prolonged periods. Limiting the pts gap to workaround the problem
+ * where encoder's rate control logic produces huge frames after a
+ * long period of suspension.
+ */
+ if (mPrevOriginalTimeUs >= 0ll) {
+ int64_t timestampGapUs = originalTimeUs - mPrevOriginalTimeUs;
+ timeUs = (timestampGapUs < mMaxTimestampGapUs ?
timestampGapUs : mMaxTimestampGapUs) + mPrevModifiedTimeUs;
+ mOriginalTimeUs.add(timeUs, originalTimeUs);
+ ALOGV("IN timestamp: %lld -> %lld",
+ static_cast<long long>(originalTimeUs),
+ static_cast<long long>(timeUs));
+ }
}
+
mPrevOriginalTimeUs = originalTimeUs;
mPrevModifiedTimeUs = timeUs;
- mOriginalTimeUs.add(timeUs, originalTimeUs);
- ALOGV("IN timestamp: %lld -> %lld",
- static_cast<long long>(originalTimeUs),
- static_cast<long long>(timeUs));
}
return timeUs;
return OK;
}
+status_t GraphicBufferSource::setInputBufferTimeOffset(int64_t timeOffsetUs) {
+ Mutex::Autolock autoLock(mMutex);
+
+ // timeOffsetUs must be negative for adjustment.
+ if (timeOffsetUs >= 0ll) {
+ return INVALID_OPERATION;
+ }
+
+ mInputBufferTimeOffsetUs = timeOffsetUs;
+ return OK;
+}
+
status_t GraphicBufferSource::setMaxFps(float maxFps) {
Mutex::Autolock autoLock(mMutex);
// of suspension on input.
status_t setMaxTimestampGapUs(int64_t maxGapUs);
+ // Sets the input buffer timestamp offset.
+ // When set, the sample's timestamp will be adjusted with the timeOffsetUs.
+ status_t setInputBufferTimeOffset(int64_t timeOffsetUs);
+
// When set, the max frame rate fed to the encoder will be capped at maxFps.
status_t setMaxFps(float maxFps);
int64_t mPrevCaptureUs;
int64_t mPrevFrameUs;
+ int64_t mInputBufferTimeOffsetUs;
+
MetadataBufferType mMetadataBufferType;
ColorAspects mColorAspects;
case IOMX::INTERNAL_OPTION_MAX_FPS: return "MAX_FPS";
case IOMX::INTERNAL_OPTION_START_TIME: return "START_TIME";
case IOMX::INTERNAL_OPTION_TIME_LAPSE: return "TIME_LAPSE";
+ case IOMX::INTERNAL_OPTION_TIME_OFFSET: return "TIME_OFFSET";
default: return def;
}
}
case IOMX::INTERNAL_OPTION_MAX_FPS:
case IOMX::INTERNAL_OPTION_START_TIME:
case IOMX::INTERNAL_OPTION_TIME_LAPSE:
+ case IOMX::INTERNAL_OPTION_TIME_OFFSET:
case IOMX::INTERNAL_OPTION_COLOR_ASPECTS:
{
const sp<GraphicBufferSource> &bufferSource =
CLOG_CONFIG(setInternalOption, "delayUs=%lld", (long long)delayUs);
return bufferSource->setRepeatPreviousFrameDelayUs(delayUs);
+ } else if (type == IOMX::INTERNAL_OPTION_TIME_OFFSET) {
+ int64_t timeOffsetUs;
+ if (!getInternalOption(data, size, &timeOffsetUs)) {
+ return INVALID_OPERATION;
+ }
+ CLOG_CONFIG(setInternalOption, "bufferOffsetUs=%lld", (long long)timeOffsetUs);
+ return bufferSource->setInputBufferTimeOffset(timeOffsetUs);
} else if (type == IOMX::INTERNAL_OPTION_MAX_TIMESTAMP_GAP) {
int64_t maxGapUs;
if (!getInternalOption(data, size, &maxGapUs)) {
mCodingType(codingType),
mProfileLevels(profileLevels),
mNumProfileLevels(numProfileLevels) {
+
+ // init all the color aspects to be Unspecified.
+ memset(&mDefaultColorAspects, 0, sizeof(ColorAspects));
+ memset(&mBitstreamColorAspects, 0, sizeof(ColorAspects));
+ memset(&mFinalColorAspects, 0, sizeof(ColorAspects));
}
void SoftVideoDecoderOMXComponent::initPorts(
OMX_U32 numOutputBuffers,
const char *mimeType,
OMX_U32 minCompressionRatio) {
+ initPorts(numInputBuffers, numInputBuffers, inputBufferSize,
+ numOutputBuffers, numOutputBuffers, mimeType, minCompressionRatio);
+}
+
+void SoftVideoDecoderOMXComponent::initPorts(
+ OMX_U32 numMinInputBuffers,
+ OMX_U32 numInputBuffers,
+ OMX_U32 inputBufferSize,
+ OMX_U32 numMinOutputBuffers,
+ OMX_U32 numOutputBuffers,
+ const char *mimeType,
+ OMX_U32 minCompressionRatio) {
mMinInputBufferSize = inputBufferSize;
mMinCompressionRatio = minCompressionRatio;
def.nPortIndex = kInputPortIndex;
def.eDir = OMX_DirInput;
- def.nBufferCountMin = numInputBuffers;
- def.nBufferCountActual = def.nBufferCountMin;
+ def.nBufferCountMin = numMinInputBuffers;
+ def.nBufferCountActual = numInputBuffers;
def.nBufferSize = inputBufferSize;
def.bEnabled = OMX_TRUE;
def.bPopulated = OMX_FALSE;
def.nPortIndex = kOutputPortIndex;
def.eDir = OMX_DirOutput;
- def.nBufferCountMin = numOutputBuffers;
- def.nBufferCountActual = def.nBufferCountMin;
+ def.nBufferCountMin = numMinOutputBuffers;
+ def.nBufferCountActual = numOutputBuffers;
def.bEnabled = OMX_TRUE;
def.bPopulated = OMX_FALSE;
def.eDomain = OMX_PortDomainVideo;
notify(OMX_EventPortSettingsChanged, kOutputPortIndex,
OMX_IndexConfigCommonOutputCrop, NULL);
}
+ } else if (mUpdateColorAspects) {
+ notify(OMX_EventPortSettingsChanged, kOutputPortIndex,
+ kDescribeColorAspectsIndex, NULL);
+ mUpdateColorAspects = false;
+ }
+}
+
+void SoftVideoDecoderOMXComponent::dumpColorAspects(const ColorAspects &colorAspects) {
+ ALOGD("dumpColorAspects: (R:%d(%s), P:%d(%s), M:%d(%s), T:%d(%s)) ",
+ colorAspects.mRange, asString(colorAspects.mRange),
+ colorAspects.mPrimaries, asString(colorAspects.mPrimaries),
+ colorAspects.mMatrixCoeffs, asString(colorAspects.mMatrixCoeffs),
+ colorAspects.mTransfer, asString(colorAspects.mTransfer));
+}
+
+bool SoftVideoDecoderOMXComponent::colorAspectsDiffer(
+ const ColorAspects &a, const ColorAspects &b) {
+ if (a.mRange != b.mRange
+ || a.mPrimaries != b.mPrimaries
+ || a.mTransfer != b.mTransfer
+ || a.mMatrixCoeffs != b.mMatrixCoeffs) {
+ return true;
+ }
+ return false;
+}
+
+void SoftVideoDecoderOMXComponent::updateFinalColorAspects(
+ const ColorAspects &otherAspects, const ColorAspects &preferredAspects) {
+ Mutex::Autolock autoLock(mColorAspectsLock);
+ ColorAspects newAspects;
+ newAspects.mRange = preferredAspects.mRange != ColorAspects::RangeUnspecified ?
+ preferredAspects.mRange : otherAspects.mRange;
+ newAspects.mPrimaries = preferredAspects.mPrimaries != ColorAspects::PrimariesUnspecified ?
+ preferredAspects.mPrimaries : otherAspects.mPrimaries;
+ newAspects.mTransfer = preferredAspects.mTransfer != ColorAspects::TransferUnspecified ?
+ preferredAspects.mTransfer : otherAspects.mTransfer;
+ newAspects.mMatrixCoeffs = preferredAspects.mMatrixCoeffs != ColorAspects::MatrixUnspecified ?
+ preferredAspects.mMatrixCoeffs : otherAspects.mMatrixCoeffs;
+
+ // Check to see if need update mFinalColorAspects.
+ if (colorAspectsDiffer(mFinalColorAspects, newAspects)) {
+ mFinalColorAspects = newAspects;
+ mUpdateColorAspects = true;
+ }
+}
+
+status_t SoftVideoDecoderOMXComponent::handleColorAspectsChange() {
+ int perference = getColorAspectPreference();
+ ALOGD("Color Aspects preference: %d ", perference);
+
+ if (perference == kPreferBitstream) {
+ updateFinalColorAspects(mDefaultColorAspects, mBitstreamColorAspects);
+ } else if (perference == kPreferContainer) {
+ updateFinalColorAspects(mBitstreamColorAspects, mDefaultColorAspects);
+ } else {
+ return OMX_ErrorUnsupportedSetting;
}
+ return OK;
}
void SoftVideoDecoderOMXComponent::copyYV12FrameToOutputBuffer(
OMX_ERRORTYPE SoftVideoDecoderOMXComponent::getConfig(
OMX_INDEXTYPE index, OMX_PTR params) {
- switch (index) {
+ switch ((int)index) {
case OMX_IndexConfigCommonOutputCrop:
{
OMX_CONFIG_RECTTYPE *rectParams = (OMX_CONFIG_RECTTYPE *)params;
return OMX_ErrorNone;
}
+ case kDescribeColorAspectsIndex:
+ {
+ if (!supportsDescribeColorAspects()) {
+ return OMX_ErrorUnsupportedIndex;
+ }
+
+ DescribeColorAspectsParams* colorAspectsParams =
+ (DescribeColorAspectsParams *)params;
+
+ if (colorAspectsParams->nPortIndex != kOutputPortIndex) {
+ return OMX_ErrorBadParameter;
+ }
+
+ colorAspectsParams->sAspects = mFinalColorAspects;
+ if (colorAspectsParams->bRequestingDataSpace || colorAspectsParams->bDataSpaceChanged) {
+ return OMX_ErrorUnsupportedSetting;
+ }
+
+ return OMX_ErrorNone;
+ }
default:
return OMX_ErrorUnsupportedIndex;
}
}
+OMX_ERRORTYPE SoftVideoDecoderOMXComponent::setConfig(
+ OMX_INDEXTYPE index, const OMX_PTR params){
+ switch ((int)index) {
+ case kDescribeColorAspectsIndex:
+ {
+ if (!supportsDescribeColorAspects()) {
+ return OMX_ErrorUnsupportedIndex;
+ }
+ const DescribeColorAspectsParams* colorAspectsParams =
+ (const DescribeColorAspectsParams *)params;
+
+ if (!isValidOMXParam(colorAspectsParams)) {
+ return OMX_ErrorBadParameter;
+ }
+
+ if (colorAspectsParams->nPortIndex != kOutputPortIndex) {
+ return OMX_ErrorBadParameter;
+ }
+
+ // Update color aspects if necessary.
+ if (colorAspectsDiffer(colorAspectsParams->sAspects, mDefaultColorAspects)) {
+ mDefaultColorAspects = colorAspectsParams->sAspects;
+ status_t err = handleColorAspectsChange();
+ CHECK(err == OK);
+ }
+ return OMX_ErrorNone;
+ }
+
+ default:
+ return OMX_ErrorUnsupportedIndex;
+ }
+}
+
+
OMX_ERRORTYPE SoftVideoDecoderOMXComponent::getExtensionIndex(
const char *name, OMX_INDEXTYPE *index) {
if (!strcmp(name, "OMX.google.android.index.prepareForAdaptivePlayback")) {
*(int32_t*)index = kPrepareForAdaptivePlaybackIndex;
return OMX_ErrorNone;
+ } else if (!strcmp(name, "OMX.google.android.index.describeColorAspects")
+ && supportsDescribeColorAspects()) {
+ *(int32_t*)index = kDescribeColorAspectsIndex;
+ return OMX_ErrorNone;
}
return SimpleSoftOMXComponent::getExtensionIndex(name, index);
}
+bool SoftVideoDecoderOMXComponent::supportsDescribeColorAspects() {
+ return getColorAspectPreference() != kNotSupported;
+}
+
+int SoftVideoDecoderOMXComponent::getColorAspectPreference() {
+ return kNotSupported;
+}
+
void SoftVideoDecoderOMXComponent::onReset() {
mOutputPortSettingsChange = NONE;
}
char key[32];
snprintf(key, sizeof(key), "a=rtpmap:%lu", x);
-
- CHECK(findAttribute(index, key, desc));
-
- snprintf(key, sizeof(key), "a=fmtp:%lu", x);
- if (!findAttribute(index, key, params)) {
+ if (findAttribute(index, key, desc)) {
+ snprintf(key, sizeof(key), "a=fmtp:%lu", x);
+ if (!findAttribute(index, key, params)) {
+ params->clear();
+ }
+ } else {
+ desc->clear();
params->clear();
}
}
return;
}
- sp<ABuffer> accessUnit;
- CHECK(msg->findBuffer("access-unit", &accessUnit));
-
- uint32_t seqNum = (uint32_t)accessUnit->int32Data();
-
if (mSeekPending) {
ALOGV("we're seeking, dropping stale packet.");
break;
}
- if (track->mNewSegment) {
- // The sequence number from RTP packet has only 16 bits and is extended
- // by ARTPSource. Only the low 16 bits of seq in RTP-Info of reply of
- // RTSP "PLAY" command should be used to detect the first RTP packet
- // after seeking.
- if (track->mAllowedStaleAccessUnits > 0) {
- if ((((seqNum ^ track->mFirstSeqNumInSegment) & 0xffff) != 0)) {
- // Not the first rtp packet of the stream after seeking, discarding.
- track->mAllowedStaleAccessUnits--;
- ALOGV("discarding stale access unit (0x%x : 0x%x)",
- seqNum, track->mFirstSeqNumInSegment);
- break;
- }
- } else { // track->mAllowedStaleAccessUnits <= 0
- mNumAccessUnitsReceived = 0;
- ALOGW_IF(track->mAllowedStaleAccessUnits == 0,
- "Still no first rtp packet after %d stale ones",
- kMaxAllowedStaleAccessUnits);
- track->mAllowedStaleAccessUnits = -1;
- break;
- }
-
- // Now found the first rtp packet of the stream after seeking.
- track->mFirstSeqNumInSegment = seqNum;
- track->mNewSegment = false;
- }
-
- if (seqNum < track->mFirstSeqNumInSegment) {
- ALOGV("dropping stale access-unit (%d < %d)",
- seqNum, track->mFirstSeqNumInSegment);
- break;
- }
-
+ sp<ABuffer> accessUnit;
+ CHECK(msg->findBuffer("access-unit", &accessUnit));
onAccessUnitComplete(trackIndex, accessUnit);
break;
}
int32_t trackIndex, const sp<ABuffer> &accessUnit) {
ALOGV("onAccessUnitComplete track %d", trackIndex);
+ TrackInfo *track = &mTracks.editItemAt(trackIndex);
if(!mPlayResponseParsed){
- ALOGI("play response is not parsed, storing accessunit");
- TrackInfo *track = &mTracks.editItemAt(trackIndex);
+ uint32_t seqNum = (uint32_t)accessUnit->int32Data();
+ ALOGI("play response is not parsed, storing accessunit %u", seqNum);
track->mPackets.push_back(accessUnit);
return;
}
handleFirstAccessUnit();
- TrackInfo *track = &mTracks.editItemAt(trackIndex);
-
if (!mAllTracksHaveTime) {
ALOGV("storing accessUnit, no time established yet");
track->mPackets.push_back(accessUnit);
sp<ABuffer> accessUnit = *track->mPackets.begin();
track->mPackets.erase(track->mPackets.begin());
+ uint32_t seqNum = (uint32_t)accessUnit->int32Data();
+ if (track->mNewSegment) {
+ // The sequence number from RTP packet has only 16 bits and is extended
+ // by ARTPSource. Only the low 16 bits of seq in RTP-Info of reply of
+ // RTSP "PLAY" command should be used to detect the first RTP packet
+ // after seeking.
+ if (track->mAllowedStaleAccessUnits > 0) {
+ uint32_t seqNum16 = seqNum & 0xffff;
+ uint32_t firstSeqNumInSegment16 = track->mFirstSeqNumInSegment & 0xffff;
+ if (seqNum16 > firstSeqNumInSegment16 + kMaxAllowedStaleAccessUnits
+ || seqNum16 < firstSeqNumInSegment16) {
+ // Not the first rtp packet of the stream after seeking, discarding.
+ track->mAllowedStaleAccessUnits--;
+ ALOGV("discarding stale access unit (0x%x : 0x%x)",
+ seqNum, track->mFirstSeqNumInSegment);
+ continue;
+ }
+ ALOGW_IF(seqNum16 != firstSeqNumInSegment16,
+ "Missing the first packet(%u), now take packet(%u) as first one",
+ track->mFirstSeqNumInSegment, seqNum);
+ } else { // track->mAllowedStaleAccessUnits <= 0
+ mNumAccessUnitsReceived = 0;
+ ALOGW_IF(track->mAllowedStaleAccessUnits == 0,
+ "Still no first rtp packet after %d stale ones",
+ kMaxAllowedStaleAccessUnits);
+ track->mAllowedStaleAccessUnits = -1;
+ return;
+ }
+
+ // Now found the first rtp packet of the stream after seeking.
+ track->mFirstSeqNumInSegment = seqNum;
+ track->mNewSegment = false;
+ }
+
+ if (seqNum < track->mFirstSeqNumInSegment) {
+ ALOGV("dropping stale access-unit (%d < %d)",
+ seqNum, track->mFirstSeqNumInSegment);
+ continue;
+ }
+
+
if (addMediaTimestamp(trackIndex, track, accessUnit)) {
postQueueAccessUnit(trackIndex, accessUnit);
}
user media
group audio camera inet net_bt net_bt_admin net_bw_acct drmrpc mediadrm
ioprio rt 4
- writepid /dev/cpuset/foreground/tasks
+ writepid /dev/cpuset/foreground/tasks /dev/stune/foreground/tasks
LOCAL_CFLAGS += -fvisibility=hidden -D EXPORT='__attribute__ ((visibility ("default")))'
-LOCAL_CFLAGS += -Werror
+LOCAL_CFLAGS += -Werror -Wall
LOCAL_SHARED_LIBRARIES := \
libbinder \
liblog \
libbinder
+LOCAL_CFLAGS := -Wall -Werror
+
include $(BUILD_SHARED_LIBRARY)
include $(CLEAR_VARS)
case AUDIO_FORMAT_AC3: return "ac-3";
case AUDIO_FORMAT_E_AC3: return "e-ac-3";
case AUDIO_FORMAT_IEC61937: return "iec61937";
+ case AUDIO_FORMAT_DTS: return "dts";
+ case AUDIO_FORMAT_DTS_HD: return "dts-hd";
+ case AUDIO_FORMAT_DOLBY_TRUEHD: return "dolby-truehd";
default:
break;
}
audio_format_t format,
audio_channel_mask_t channelMask,
size_t *frameCount,
- IAudioFlinger::track_flags_t *flags,
+ audio_output_flags_t *flags,
const sp<IMemory>& sharedBuffer,
audio_io_handle_t output,
pid_t pid,
sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
if (mPlaybackThreads.keyAt(i) != output) {
uint32_t sessions = t->hasAudioSession(lSessionId);
- if (sessions & PlaybackThread::EFFECT_SESSION) {
+ if (sessions & ThreadBase::EFFECT_SESSION) {
effectThread = t.get();
break;
}
ALOGV("%d died, releasing its sessions", pid);
size_t num = mAudioSessionRefs.size();
bool removed = false;
- for (size_t i = 0; i < num; ) {
+ for (size_t i = 0; i< num; ) {
AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
ALOGV(" pid %d @ %zu", ref->mPid, i);
if (ref->mPid == pid) {
audio_channel_mask_t channelMask,
const String16& opPackageName,
size_t *frameCount,
- IAudioFlinger::track_flags_t *flags,
+ audio_input_flags_t *flags,
pid_t pid,
pid_t tid,
int clientUid,
uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
{
Mutex::Autolock _l(mLock);
- PlaybackThread *thread = primaryPlaybackThread_l();
+ PlaybackThread *thread = fastPlaybackThread_l();
return thread != NULL ? thread->sampleRate() : 0;
}
size_t AudioFlinger::getPrimaryOutputFrameCount()
{
Mutex::Autolock _l(mLock);
- PlaybackThread *thread = primaryPlaybackThread_l();
+ PlaybackThread *thread = fastPlaybackThread_l();
return thread != NULL ? thread->frameCountHAL() : 0;
}
for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
sp<PlaybackThread> thread = mPlaybackThreads.valueAt(i);
uint32_t sessions = thread->hasAudioSession(sessionId);
- if (sessions & PlaybackThread::TRACK_SESSION) {
+ if (sessions & ThreadBase::TRACK_SESSION) {
AudioParameter param = AudioParameter();
param.addInt(String8(AUDIO_PARAMETER_STREAM_HW_AV_SYNC), value);
thread->setParameters(param.toString());
}
#endif
- AudioStreamIn *inputStream = new AudioStreamIn(inHwDev, inStream);
+ AudioStreamIn *inputStream = new AudioStreamIn(inHwDev, inStream, flags);
// Start record thread
// RecordThread requires both input and output device indication to forward to audio
}
size_t num = mAudioSessionRefs.size();
- for (size_t i = 0; i < num; i++) {
+ for (size_t i = 0; i< num; i++) {
AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
if (ref->mSessionid == audioSession && ref->mPid == caller) {
ref->mCnt++;
caller = pid;
}
size_t num = mAudioSessionRefs.size();
- for (size_t i = 0; i < num; i++) {
+ for (size_t i = 0; i< num; i++) {
AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
if (ref->mSessionid == audioSession && ref->mPid == caller) {
ref->mCnt--;
ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller);
}
-bool AudioFlinger::isSessionAcquired_l(audio_session_t audioSession)
-{
- size_t num = mAudioSessionRefs.size();
- for (size_t i = 0; i < num; i++) {
- AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
- if (ref->mSessionid == audioSession) {
- return true;
- }
- }
- return false;
-}
-
void AudioFlinger::purgeStaleEffects_l() {
ALOGV("purging stale effects");
return thread->outDevice();
}
+AudioFlinger::PlaybackThread *AudioFlinger::fastPlaybackThread_l() const
+{
+ size_t minFrameCount = 0;
+ PlaybackThread *minThread = NULL;
+ for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
+ PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
+ if (!thread->isDuplicating()) {
+ size_t frameCount = thread->frameCountHAL();
+ if (frameCount != 0 && (minFrameCount == 0 || frameCount < minFrameCount ||
+ (frameCount == minFrameCount && thread->hasFastMixer() &&
+ /*minThread != NULL &&*/ !minThread->hasFastMixer()))) {
+ minFrameCount = frameCount;
+ minThread = thread;
+ }
+ }
+ }
+ return minThread;
+}
+
sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
audio_session_t triggerSession,
audio_session_t listenerSession,
sp<Client> client = registerPid(pid);
// create effect on selected output thread
- bool pinned = (sessionId > AUDIO_SESSION_OUTPUT_MIX) && isSessionAcquired_l(sessionId);
handle = thread->createEffect_l(client, effectClient, priority, sessionId,
- &desc, enabled, &lStatus, pinned);
+ &desc, enabled, &lStatus);
if (handle != 0 && id != NULL) {
*id = handle->id();
}
return INVALID_OPERATION;
}
- // Check whether the destination thread has a channel count of FCC_2, which is
- // currently required for (most) effects. Prevent moving the effect chain here rather
- // than disabling the addEffect_l() call in dstThread below.
- if ((dstThread->type() == ThreadBase::MIXER || dstThread->isDuplicating()) &&
- dstThread->mChannelCount != FCC_2) {
+ // Check whether the destination thread and all effects in the chain are compatible
+ if (!chain->isCompatibleWithThread_l(dstThread)) {
ALOGW("moveEffectChain_l() effect chain failed because"
- " destination thread %p channel count(%u) != %u",
- dstThread, dstThread->mChannelCount, FCC_2);
+ " destination thread %p is not compatible with effects in the chain",
+ dstThread);
return INVALID_OPERATION;
}
ALOGV("updateOrphanEffectChains session %d index %zd", session, index);
if (index >= 0) {
sp<EffectChain> chain = mOrphanEffectChains.valueAt(index);
- if (chain->removeEffect_l(effect, true) == 0) {
+ if (chain->removeEffect_l(effect) == 0) {
ALOGV("updateOrphanEffectChains removing effect chain at index %zd", index);
mOrphanEffectChains.removeItemsAt(index);
}
}
} else {
if (fd >= 0) {
- dprintf(fd, "unable to rotate tees in %.*s: %s\n", teePathLen, teePath,
+ dprintf(fd, "unable to rotate tees in %.*s: %s\n", (int) teePathLen, teePath,
strerror(errno));
}
}
audio_format_t format,
audio_channel_mask_t channelMask,
size_t *pFrameCount,
- IAudioFlinger::track_flags_t *flags,
+ audio_output_flags_t *flags,
const sp<IMemory>& sharedBuffer,
audio_io_handle_t output,
pid_t pid,
audio_channel_mask_t channelMask,
const String16& opPackageName,
size_t *pFrameCount,
- IAudioFlinger::track_flags_t *flags,
+ audio_input_flags_t *flags,
pid_t pid,
pid_t tid,
int clientUid,
PlaybackThread *primaryPlaybackThread_l() const;
audio_devices_t primaryOutputDevice_l() const;
+ // return the playback thread with smallest HAL buffer size, and prefer fast
+ PlaybackThread *fastPlaybackThread_l() const;
+
sp<PlaybackThread> getEffectThread_l(audio_session_t sessionId, int EffectId);
void removeNotificationClient(pid_t pid);
bool isNonOffloadableGlobalEffectEnabled_l();
void onNonOffloadableGlobalEffectEnable();
- bool isSessionAcquired_l(audio_session_t audioSession);
// Store an effect chain to mOrphanEffectChains keyed vector.
// Called when a thread exits and effects are still attached to it.
struct AudioStreamIn {
AudioHwDevice* const audioHwDev;
audio_stream_in_t* const stream;
+ audio_input_flags_t flags;
audio_hw_device_t* hwDev() const { return audioHwDev->hwDevice(); }
- AudioStreamIn(AudioHwDevice *dev, audio_stream_in_t *in) :
- audioHwDev(dev), stream(in) {}
+ AudioStreamIn(AudioHwDevice *dev, audio_stream_in_t *in, audio_input_flags_t flags) :
+ audioHwDev(dev), stream(in), flags(flags) {}
};
// for mAudioSessionRefs only
{
ALOGV("AudioMixer::deleteTrackName(%d)", name);
name -= TRACK0;
- ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name);
+ LOG_ALWAYS_FATAL_IF(name < 0 || name >= (int)MAX_NUM_TRACKS, "bad track name %d", name);
ALOGV("deleteTrackName(%d)", name);
track_t& track(mState.tracks[ name ]);
if (track.enabled) {
const wp<AudioFlinger::EffectChain>& chain,
effect_descriptor_t *desc,
int id,
- audio_session_t sessionId,
- bool pinned)
- : mPinned(pinned),
+ audio_session_t sessionId)
+ : mPinned(sessionId > AUDIO_SESSION_OUTPUT_MIX),
mThread(thread), mChain(chain), mId(id), mSessionId(sessionId),
mDescriptor(*desc),
// mConfig is set by configure() and not used before then
mSuspended(false),
mAudioFlinger(thread->mAudioFlinger)
{
- ALOGV("Constructor %p pinned %d", this, pinned);
+ ALOGV("Constructor %p", this);
int lStatus;
// create effect engine from effect factory
goto Error;
}
- setOffloaded(thread->type() == ThreadBase::OFFLOAD, thread->id());
-
ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface);
return;
Error:
{
ALOGV("Destructor %p", this);
if (mEffectInterface != NULL) {
- ALOGW("EffectModule %p destructor called with unreleased interface", this);
- release_l();
+ remove_effect_from_hal_l();
+ // release effect engine
+ EffectRelease(mEffectInterface);
}
}
size_t i;
for (i = 0; i < size; i++) {
EffectHandle *h = mHandles[i];
- if (h == NULL || h->disconnected()) {
+ if (h == NULL || h->destroyed_l()) {
continue;
}
// first non destroyed handle is considered in control
return status;
}
-ssize_t AudioFlinger::EffectModule::removeHandle(EffectHandle *handle)
+size_t AudioFlinger::EffectModule::removeHandle(EffectHandle *handle)
{
Mutex::Autolock _l(mLock);
- return removeHandle_l(handle);
-}
-
-ssize_t AudioFlinger::EffectModule::removeHandle_l(EffectHandle *handle)
-{
size_t size = mHandles.size();
size_t i;
for (i = 0; i < size; i++) {
}
}
if (i == size) {
- ALOGW("%s %p handle not found %p", __FUNCTION__, this, handle);
- return BAD_VALUE;
+ return size;
}
- ALOGV("removeHandle_l() %p removed handle %p in position %zu", this, handle, i);
+ ALOGV("removeHandle() %p removed handle %p in position %zu", this, handle, i);
mHandles.removeAt(i);
// if removed from first place, move effect control from this handle to next in line
// the first valid handle in the list has control over the module
for (size_t i = 0; i < mHandles.size(); i++) {
EffectHandle *h = mHandles[i];
- if (h != NULL && !h->disconnected()) {
+ if (h != NULL && !h->destroyed_l()) {
return h;
}
}
return NULL;
}
-// unsafe method called when the effect parent thread has been destroyed
-ssize_t AudioFlinger::EffectModule::disconnectHandle(EffectHandle *handle, bool unpinIfLast)
+size_t AudioFlinger::EffectModule::disconnect(EffectHandle *handle, bool unpinIfLast)
{
ALOGV("disconnect() %p handle %p", this, handle);
- Mutex::Autolock _l(mLock);
- ssize_t numHandles = removeHandle_l(handle);
- if ((numHandles == 0) && (!mPinned || unpinIfLast)) {
- AudioSystem::unregisterEffect(mId);
- sp<AudioFlinger> af = mAudioFlinger.promote();
- if (af != 0) {
- mLock.unlock();
- af->updateOrphanEffectChains(this);
- mLock.lock();
+ // keep a strong reference on this EffectModule to avoid calling the
+ // destructor before we exit
+ sp<EffectModule> keep(this);
+ {
+ if (removeHandle(handle) == 0) {
+ if (!isPinned() || unpinIfLast) {
+ sp<ThreadBase> thread = mThread.promote();
+ if (thread != 0) {
+ Mutex::Autolock _l(thread->mLock);
+ thread->removeEffect_l(this);
+ }
+ sp<AudioFlinger> af = mAudioFlinger.promote();
+ if (af != 0) {
+ af->updateOrphanEffectChains(this);
+ }
+ AudioSystem::unregisterEffect(mId);
+ }
}
}
- return numHandles;
+ return mHandles.size();
}
-void AudioFlinger::EffectModule::updateState() {
+bool AudioFlinger::EffectModule::updateState() {
Mutex::Autolock _l(mLock);
+ bool started = false;
switch (mState) {
case RESTART:
reset_l();
}
if (start_l() == NO_ERROR) {
mState = ACTIVE;
+ started = true;
} else {
mState = IDLE;
}
default: //IDLE , ACTIVE, DESTROYED
break;
}
+
+ return started;
}
void AudioFlinger::EffectModule::process()
}
}
+// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
status_t AudioFlinger::EffectModule::start()
{
- Mutex::Autolock _l(mLock);
- return start_l();
+ sp<EffectChain> chain;
+ status_t status;
+ {
+ Mutex::Autolock _l(mLock);
+ status = start_l();
+ if (status == NO_ERROR) {
+ chain = mChain.promote();
+ }
+ }
+ if (chain != 0) {
+ chain->resetVolume_l();
+ }
+ return status;
}
status_t AudioFlinger::EffectModule::start_l()
}
if (status == 0) {
addEffectToHal_l();
- sp<EffectChain> chain = mChain.promote();
- if (chain != 0) {
- chain->forceVolume();
- }
}
return status;
}
return status;
}
-// must be called with EffectChain::mLock held
-void AudioFlinger::EffectModule::release_l()
-{
- if (mEffectInterface != NULL) {
- remove_effect_from_hal_l();
- // release effect engine
- EffectRelease(mEffectInterface);
- mEffectInterface = NULL;
- }
-}
-
status_t AudioFlinger::EffectModule::remove_effect_from_hal_l()
{
if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
uint32_t size = (replySize == NULL) ? 0 : *replySize;
for (size_t i = 1; i < mHandles.size(); i++) {
EffectHandle *h = mHandles[i];
- if (h != NULL && !h->disconnected()) {
+ if (h != NULL && !h->destroyed_l()) {
h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData);
}
}
}
for (size_t i = 1; i < mHandles.size(); i++) {
EffectHandle *h = mHandles[i];
- if (h != NULL && !h->disconnected()) {
+ if (h != NULL && !h->destroyed_l()) {
h->setEnabled(enabled);
}
}
Mutex::Autolock _l(mLock);
for (size_t i = 0; i < mHandles.size(); i++) {
EffectHandle *handle = mHandles[i];
- if (handle != NULL && !handle->disconnected()) {
+ if (handle != NULL && !handle->destroyed_l()) {
+ handle->effect().clear();
if (handle->hasControl()) {
enabled = handle->enabled();
}
result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
for (size_t i = 0; i < mHandles.size(); ++i) {
EffectHandle *handle = mHandles[i];
- if (handle != NULL && !handle->disconnected()) {
+ if (handle != NULL && !handle->destroyed_l()) {
handle->dumpToBuffer(buffer, SIZE);
result.append(buffer);
}
int32_t priority)
: BnEffect(),
mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
- mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false)
+ mPriority(priority), mHasControl(false), mEnabled(false), mDestroyed(false)
{
ALOGV("constructor %p", this);
AudioFlinger::EffectHandle::~EffectHandle()
{
ALOGV("Destructor %p", this);
+
+ if (mEffect == 0) {
+ mDestroyed = true;
+ return;
+ }
+ mEffect->lock();
+ mDestroyed = true;
+ mEffect->unlock();
disconnect(false);
}
status_t AudioFlinger::EffectHandle::enable()
{
- AutoMutex _l(mLock);
ALOGV("enable %p", this);
- sp<EffectModule> effect = mEffect.promote();
- if (effect == 0 || mDisconnected) {
- return DEAD_OBJECT;
- }
if (!mHasControl) {
return INVALID_OPERATION;
}
+ if (mEffect == 0) {
+ return DEAD_OBJECT;
+ }
if (mEnabled) {
return NO_ERROR;
mEnabled = true;
- sp<ThreadBase> thread = effect->thread().promote();
+ sp<ThreadBase> thread = mEffect->thread().promote();
if (thread != 0) {
- thread->checkSuspendOnEffectEnabled(effect, true, effect->sessionId());
+ thread->checkSuspendOnEffectEnabled(mEffect, true, mEffect->sessionId());
}
// checkSuspendOnEffectEnabled() can suspend this same effect when enabled
- if (effect->suspended()) {
+ if (mEffect->suspended()) {
return NO_ERROR;
}
- status_t status = effect->setEnabled(true);
+ status_t status = mEffect->setEnabled(true);
if (status != NO_ERROR) {
if (thread != 0) {
- thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
+ thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
}
mEnabled = false;
} else {
Mutex::Autolock _l(t->mLock);
t->broadcast_l();
}
- if (!effect->isOffloadable()) {
+ if (!mEffect->isOffloadable()) {
if (thread->type() == ThreadBase::OFFLOAD) {
PlaybackThread *t = (PlaybackThread *)thread.get();
t->invalidateTracks(AUDIO_STREAM_MUSIC);
}
- if (effect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) {
+ if (mEffect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) {
thread->mAudioFlinger->onNonOffloadableGlobalEffectEnable();
}
}
status_t AudioFlinger::EffectHandle::disable()
{
ALOGV("disable %p", this);
- AutoMutex _l(mLock);
- sp<EffectModule> effect = mEffect.promote();
- if (effect == 0 || mDisconnected) {
- return DEAD_OBJECT;
- }
if (!mHasControl) {
return INVALID_OPERATION;
}
+ if (mEffect == 0) {
+ return DEAD_OBJECT;
+ }
if (!mEnabled) {
return NO_ERROR;
}
mEnabled = false;
- if (effect->suspended()) {
+ if (mEffect->suspended()) {
return NO_ERROR;
}
- status_t status = effect->setEnabled(false);
+ status_t status = mEffect->setEnabled(false);
- sp<ThreadBase> thread = effect->thread().promote();
+ sp<ThreadBase> thread = mEffect->thread().promote();
if (thread != 0) {
- thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
+ thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
if (thread->type() == ThreadBase::OFFLOAD) {
PlaybackThread *t = (PlaybackThread *)thread.get();
Mutex::Autolock _l(t->mLock);
void AudioFlinger::EffectHandle::disconnect()
{
- ALOGV("%s %p", __FUNCTION__, this);
disconnect(true);
}
void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
{
- AutoMutex _l(mLock);
- ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
- if (mDisconnected) {
- if (unpinIfLast) {
- android_errorWriteLog(0x534e4554, "32707507");
- }
+ ALOGV("disconnect(%s)", unpinIfLast ? "true" : "false");
+ if (mEffect == 0) {
return;
}
- mDisconnected = true;
- sp<ThreadBase> thread;
- {
- sp<EffectModule> effect = mEffect.promote();
- if (effect != 0) {
- thread = effect->thread().promote();
- }
- }
- if (thread != 0) {
- thread->disconnectEffectHandle(this, unpinIfLast);
- } else {
- ALOGW("%s Effect handle %p disconnected after thread destruction", __FUNCTION__, this);
- // try to cleanup as much as we can
- sp<EffectModule> effect = mEffect.promote();
- if (effect != 0) {
- effect->disconnectHandle(this, unpinIfLast);
+ // restore suspended effects if the disconnected handle was enabled and the last one.
+ if ((mEffect->disconnect(this, unpinIfLast) == 0) && mEnabled) {
+ sp<ThreadBase> thread = mEffect->thread().promote();
+ if (thread != 0) {
+ thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
}
}
+ // release sp on module => module destructor can be called now
+ mEffect.clear();
if (mClient != 0) {
if (mCblk != NULL) {
// unlike ~TrackBase(), mCblk is never a local new, so don't delete
void *pReplyData)
{
ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
- cmdCode, mHasControl, mEffect.unsafe_get());
-
- // reject commands reserved for internal use by audio framework if coming from outside
- // of audioserver
- switch(cmdCode) {
- case EFFECT_CMD_ENABLE:
- case EFFECT_CMD_DISABLE:
- case EFFECT_CMD_SET_PARAM:
- case EFFECT_CMD_SET_PARAM_DEFERRED:
- case EFFECT_CMD_SET_PARAM_COMMIT:
- case EFFECT_CMD_GET_PARAM:
- break;
- default:
- if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
- break;
- }
- android_errorWriteLog(0x534e4554, "62019992");
- return BAD_VALUE;
- }
+ cmdCode, mHasControl, (mEffect == 0) ? 0 : mEffect.get());
- if (cmdCode == EFFECT_CMD_ENABLE) {
- if (*replySize < sizeof(int)) {
- android_errorWriteLog(0x534e4554, "32095713");
- return BAD_VALUE;
- }
- *(int *)pReplyData = NO_ERROR;
- *replySize = sizeof(int);
- return enable();
- } else if (cmdCode == EFFECT_CMD_DISABLE) {
- if (*replySize < sizeof(int)) {
- android_errorWriteLog(0x534e4554, "32095713");
- return BAD_VALUE;
- }
- *(int *)pReplyData = NO_ERROR;
- *replySize = sizeof(int);
- return disable();
- }
-
- AutoMutex _l(mLock);
- sp<EffectModule> effect = mEffect.promote();
- if (effect == 0 || mDisconnected) {
- return DEAD_OBJECT;
- }
// only get parameter command is permitted for applications not controlling the effect
if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
return INVALID_OPERATION;
}
+ if (mEffect == 0) {
+ return DEAD_OBJECT;
+ }
if (mClient == 0) {
return INVALID_OPERATION;
}
// handle commands that are not forwarded transparently to effect engine
if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
- if (*replySize < sizeof(int)) {
- android_errorWriteLog(0x534e4554, "32095713");
- return BAD_VALUE;
- }
- *(int *)pReplyData = NO_ERROR;
- *replySize = sizeof(int);
-
// No need to trylock() here as this function is executed in the binder thread serving a
// particular client process: no risk to block the whole media server process or mixer
// threads if we are stuck here
Mutex::Autolock _l(mCblk->lock);
+
// keep local copy of index in case of client corruption b/32220769
const uint32_t clientIndex = mCblk->clientIndex;
const uint32_t serverIndex = mCblk->serverIndex;
int reply = 0;
uint32_t rsize = sizeof(reply);
- status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
+ status_t ret = mEffect->command(EFFECT_CMD_SET_PARAM,
size,
param,
&rsize,
mCblk->serverIndex = 0;
mCblk->clientIndex = 0;
return status;
+ } else if (cmdCode == EFFECT_CMD_ENABLE) {
+ *(int *)pReplyData = NO_ERROR;
+ return enable();
+ } else if (cmdCode == EFFECT_CMD_DISABLE) {
+ *(int *)pReplyData = NO_ERROR;
+ return disable();
}
- return effect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
+ return mEffect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
}
void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
audio_session_t sessionId)
: mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
mOwnInBuffer(false), mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
- mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX), mForceVolume(false)
+ mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX)
{
mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
if (thread == NULL) {
if (mOwnInBuffer) {
delete mInBuffer;
}
+
}
// getEffectFromDesc_l() must be called with ThreadBase::mLock held
mEffects[i]->process();
}
}
+ bool doResetVolume = false;
for (size_t i = 0; i < size; i++) {
- mEffects[i]->updateState();
- }
-}
-
-// createEffect_l() must be called with ThreadBase::mLock held
-status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
- ThreadBase *thread,
- effect_descriptor_t *desc,
- int id,
- audio_session_t sessionId,
- bool pinned)
-{
- Mutex::Autolock _l(mLock);
- effect = new EffectModule(thread, this, desc, id, sessionId, pinned);
- status_t lStatus = effect->status();
- if (lStatus == NO_ERROR) {
- lStatus = addEffect_ll(effect);
+ doResetVolume = mEffects[i]->updateState() || doResetVolume;
}
- if (lStatus != NO_ERROR) {
- effect.clear();
+ if (doResetVolume) {
+ resetVolume_l();
}
- return lStatus;
}
-// addEffect_l() must be called with ThreadBase::mLock held
+// addEffect_l() must be called with PlaybackThread::mLock held
status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
{
- Mutex::Autolock _l(mLock);
- return addEffect_ll(effect);
-}
-// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
-status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
-{
effect_descriptor_t desc = effect->desc();
uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
+ Mutex::Autolock _l(mLock);
effect->setChain(this);
sp<ThreadBase> thread = mThread.promote();
if (thread == 0) {
return NO_ERROR;
}
-// removeEffect_l() must be called with ThreadBase::mLock held
-size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
- bool release)
+// removeEffect_l() must be called with PlaybackThread::mLock held
+size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect)
{
Mutex::Autolock _l(mLock);
size_t size = mEffects.size();
mEffects[i]->state() == EffectModule::STOPPING) {
mEffects[i]->stop();
}
- if (release) {
- mEffects[i]->release_l();
- }
-
if (type == EFFECT_FLAG_TYPE_AUXILIARY) {
delete[] effect->inBuffer();
} else {
mEffects.removeAt(i);
ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
this, i);
-
break;
}
}
return mEffects.size();
}
-// setDevice_l() must be called with ThreadBase::mLock held
+// setDevice_l() must be called with PlaybackThread::mLock held
void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device)
{
size_t size = mEffects.size();
}
}
-// setMode_l() must be called with ThreadBase::mLock held
+// setMode_l() must be called with PlaybackThread::mLock held
void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
{
size_t size = mEffects.size();
}
}
-// setAudioSource_l() must be called with ThreadBase::mLock held
+// setAudioSource_l() must be called with PlaybackThread::mLock held
void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
{
size_t size = mEffects.size();
}
}
-// setVolume_l() must be called with PlaybackThread::mLock held
-bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right)
+// setVolume_l() must be called with PlaybackThread::mLock or EffectChain::mLock held
+bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
{
uint32_t newLeft = *left;
uint32_t newRight = *right;
}
}
- if (!isVolumeForced() && ctrlIdx == mVolumeCtrlIdx &&
+ if (!force && ctrlIdx == mVolumeCtrlIdx &&
*left == mLeftVolume && *right == mRightVolume) {
if (hasControl) {
*left = mNewLeftVolume;
return hasControl;
}
+// resetVolume_l() must be called with PlaybackThread::mLock or EffectChain::mLock held
+void AudioFlinger::EffectChain::resetVolume_l()
+{
+ if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
+ uint32_t left = mLeftVolume;
+ uint32_t right = mRightVolume;
+ (void)setVolume_l(&left, &right, true);
+ }
+}
+
void AudioFlinger::EffectChain::syncHalEffectsState()
{
Mutex::Autolock _l(mLock);
effect->setSuspended(false);
effect->lock();
EffectHandle *handle = effect->controlHandle_l();
- if (handle != NULL && !handle->disconnected()) {
+ if (handle != NULL && !handle->destroyed_l()) {
effect->setEnabled_l(handle->enabled());
}
effect->unlock();
}
}
+bool AudioFlinger::EffectChain::hasSoftwareEffect() const
+{
+ Mutex::Autolock _l(mLock);
+ for (size_t i = 0; i < mEffects.size(); i++) {
+ if (mEffects[i]->isImplementationSoftware()) {
+ return true;
+ }
+ }
+ return false;
+}
+
+// isCompatibleWithThread_l() must be called with thread->mLock held
+bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
+{
+ Mutex::Autolock _l(mLock);
+ for (size_t i = 0; i < mEffects.size(); i++) {
+ if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
+ return false;
+ }
+ }
+ return true;
+}
+
} // namespace android
const wp<AudioFlinger::EffectChain>& chain,
effect_descriptor_t *desc,
int id,
- audio_session_t sessionId,
- bool pinned);
+ audio_session_t sessionId);
virtual ~EffectModule();
enum effect_state {
int id() const { return mId; }
void process();
- void updateState();
+ bool updateState();
status_t command(uint32_t cmdCode,
uint32_t cmdSize,
void *pCmdData,
const wp<ThreadBase>& thread() { return mThread; }
status_t addHandle(EffectHandle *handle);
- ssize_t disconnectHandle(EffectHandle *handle, bool unpinIfLast);
- ssize_t removeHandle(EffectHandle *handle);
- ssize_t removeHandle_l(EffectHandle *handle);
+ size_t disconnect(EffectHandle *handle, bool unpinIfLast);
+ size_t removeHandle(EffectHandle *handle);
const effect_descriptor_t& desc() const { return mDescriptor; }
wp<EffectChain>& chain() { return mChain; }
void unlock() { mLock.unlock(); }
bool isOffloadable() const
{ return (mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0; }
+ bool isImplementationSoftware() const
+ { return (mDescriptor.flags & EFFECT_FLAG_HW_ACC_MASK) == 0; }
status_t setOffloaded(bool offloaded, audio_io_handle_t io);
bool isOffloaded() const;
void addEffectToHal_l();
- void release_l();
void dump(int fd, const Vector<String16>& args);
bool enabled() const { return mEnabled; }
// Getters
- wp<EffectModule> effect() const { return mEffect; }
- int id() const {
- sp<EffectModule> effect = mEffect.promote();
- if (effect == 0) {
- return 0;
- }
- return effect->id();
- }
+ int id() const { return mEffect->id(); }
int priority() const { return mPriority; }
bool hasControl() const { return mHasControl; }
- bool disconnected() const { return mDisconnected; }
+ sp<EffectModule> effect() const { return mEffect; }
+ // destroyed_l() must be called with the associated EffectModule mLock held
+ bool destroyed_l() const { return mDestroyed; }
void dumpToBuffer(char* buffer, size_t size);
EffectHandle(const EffectHandle&);
EffectHandle& operator =(const EffectHandle&);
- Mutex mLock; // protects IEffect method calls
- wp<EffectModule> mEffect; // pointer to controlled EffectModule
+ sp<EffectModule> mEffect; // pointer to controlled EffectModule
sp<IEffectClient> mEffectClient; // callback interface for client notifications
/*const*/ sp<Client> mClient; // client for shared memory allocation, see disconnect()
sp<IMemory> mCblkMemory; // shared memory for control block
bool mHasControl; // true if this handle is controlling the effect
bool mEnabled; // cached enable state: needed when the effect is
// restored after being suspended
- bool mDisconnected; // Set to true by disconnect()
+ bool mDestroyed; // Set to true by destructor. Access with EffectModule
+ // mLock held
};
// the EffectChain class represents a group of effects associated to one audio session.
mLock.unlock();
}
- status_t createEffect_l(sp<EffectModule>& effect,
- ThreadBase *thread,
- effect_descriptor_t *desc,
- int id,
- audio_session_t sessionId,
- bool pinned);
status_t addEffect_l(const sp<EffectModule>& handle);
- status_t addEffect_ll(const sp<EffectModule>& handle);
- size_t removeEffect_l(const sp<EffectModule>& handle, bool release = false);
+ size_t removeEffect_l(const sp<EffectModule>& handle);
audio_session_t sessionId() const { return mSessionId; }
void setSessionId(audio_session_t sessionId) { mSessionId = sessionId; }
sp<EffectModule> getEffectFromId_l(int id);
sp<EffectModule> getEffectFromType_l(const effect_uuid_t *type);
// FIXME use float to improve the dynamic range
- bool setVolume_l(uint32_t *left, uint32_t *right);
+ bool setVolume_l(uint32_t *left, uint32_t *right, bool force = false);
+ void resetVolume_l();
void setDevice_l(audio_devices_t device);
void setMode_l(audio_mode_t mode);
void setAudioSource_l(audio_source_t source);
// At least one non offloadable effect in the chain is enabled
bool isNonOffloadableEnabled();
- // use release_cas because we don't care about the observed value, just want to make sure the
- // new value is observable.
- void forceVolume() { android_atomic_release_cas(false, true, &mForceVolume); }
- // use acquire_cas because we are interested in the observed value and
- // we are the only observers.
- bool isVolumeForced() { return (android_atomic_acquire_cas(true, false, &mForceVolume) == 0); }
-
void syncHalEffectsState();
+ bool hasSoftwareEffect() const;
+
+ // isCompatibleWithThread_l() must be called with thread->mLock held
+ bool isCompatibleWithThread_l(const sp<ThreadBase>& thread) const;
+
void dump(int fd, const Vector<String16>& args);
protected:
void setThread(const sp<ThreadBase>& thread);
- wp<ThreadBase> mThread; // parent mixer thread
- Mutex mLock; // mutex protecting effect list
- Vector< sp<EffectModule> > mEffects; // list of effect modules
- audio_session_t mSessionId; // audio session ID
- int16_t *mInBuffer; // chain input buffer
- int16_t *mOutBuffer; // chain output buffer
+ wp<ThreadBase> mThread; // parent mixer thread
+ mutable Mutex mLock; // mutex protecting effect list
+ Vector< sp<EffectModule> > mEffects; // list of effect modules
+ audio_session_t mSessionId; // audio session ID
+ int16_t *mInBuffer; // chain input buffer
+ int16_t *mOutBuffer; // chain output buffer
// 'volatile' here means these are accessed with atomic operations instead of mutex
volatile int32_t mActiveTrackCnt; // number of active tracks connected
volatile int32_t mTrackCnt; // number of tracks connected
- int32_t mTailBufferCount; // current effect tail buffer count
- int32_t mMaxTailBuffers; // maximum effect tail buffers
- bool mOwnInBuffer; // true if the chain owns its input buffer
- int mVolumeCtrlIdx; // index of insert effect having control over volume
- uint32_t mLeftVolume; // previous volume on left channel
- uint32_t mRightVolume; // previous volume on right channel
- uint32_t mNewLeftVolume; // new volume on left channel
- uint32_t mNewRightVolume; // new volume on right channel
- uint32_t mStrategy; // strategy for this effect chain
- // mSuspendedEffects lists all effects currently suspended in the chain.
- // Use effect type UUID timelow field as key. There is no real risk of identical
- // timeLow fields among effect type UUIDs.
- // Updated by updateSuspendedSessions_l() only.
- KeyedVector< int, sp<SuspendedEffectDesc> > mSuspendedEffects;
- volatile int32_t mForceVolume; // force next volume command because a new effect was enabled
+ int32_t mTailBufferCount; // current effect tail buffer count
+ int32_t mMaxTailBuffers; // maximum effect tail buffers
+ bool mOwnInBuffer; // true if the chain owns its input buffer
+ int mVolumeCtrlIdx; // index of insert effect having control over volume
+ uint32_t mLeftVolume; // previous volume on left channel
+ uint32_t mRightVolume; // previous volume on right channel
+ uint32_t mNewLeftVolume; // new volume on left channel
+ uint32_t mNewRightVolume; // new volume on right channel
+ uint32_t mStrategy; // strategy for this effect chain
+ // mSuspendedEffects lists all effects currently suspended in the chain.
+ // Use effect type UUID timelow field as key. There is no real risk of identical
+ // timeLow fields among effect type UUIDs.
+ // Updated by updateSuspendedSessions_l() only.
+ KeyedVector< int, sp<SuspendedEffectDesc> > mSuspendedEffects;
};
/*static*/ const FastCaptureState FastCapture::sInitial;
-FastCapture::FastCapture() : FastThread(),
+FastCapture::FastCapture() : FastThread("cycleC_ms", "loadC_us"),
mInputSource(NULL), mInputSourceGen(0), mPipeSink(NULL), mPipeSinkGen(0),
mReadBuffer(NULL), mReadBufferState(-1), mFormat(Format_Invalid), mSampleRate(0),
// mDummyDumpState
/*static*/ const FastMixerState FastMixer::sInitial;
-FastMixer::FastMixer() : FastThread(),
+FastMixer::FastMixer() : FastThread("cycle_ms", "load_us"),
// mFastTrackNames
// mGenerations
mOutputSink(NULL),
namespace android {
-FastThread::FastThread() : Thread(false /*canCallJava*/),
+FastThread::FastThread(const char *cycleMs, const char *loadUs) : Thread(false /*canCallJava*/),
// re-initialized to &sInitial by subclass constructor
mPrevious(NULL), mCurrent(NULL),
/* mOldTs({0, 0}), */
frameCount(0),
#endif
mAttemptedWrite(false)
+ // mCycleMs(cycleMs)
+ // mLoadUs(loadUs)
{
mOldTs.tv_sec = 0;
mOldTs.tv_nsec = 0;
mMeasuredWarmupTs.tv_sec = 0;
mMeasuredWarmupTs.tv_nsec = 0;
+ strlcpy(mCycleMs, cycleMs, sizeof(mCycleMs));
+ strlcpy(mLoadUs, loadUs, sizeof(mLoadUs));
}
FastThread::~FastThread()
if (old <= 0) {
syscall(__NR_futex, coldFutexAddr, FUTEX_WAIT_PRIVATE, old - 1, NULL);
}
- int policy = sched_getscheduler(0);
+ int policy = sched_getscheduler(0) & ~SCHED_RESET_ON_FORK;
if (!(policy == SCHED_FIFO || policy == SCHED_RR)) {
ALOGE("did not receive expected priority boost");
}
// this store #4 is not atomic with respect to stores #1, #2, #3 above, but
// the newest open & oldest closed halves are atomic with respect to each other
mDumpState->mBounds = mBounds;
- ATRACE_INT("cycle_ms", monotonicNs / 1000000);
- ATRACE_INT("load_us", loadNs / 1000);
+ ATRACE_INT(mCycleMs, monotonicNs / 1000000);
+ ATRACE_INT(mLoadUs, loadNs / 1000);
}
#endif
} else {
class FastThread : public Thread {
public:
- FastThread();
+ FastThread(const char *cycleMs, const char *loadUs);
virtual ~FastThread();
private:
FastThreadState::Command mCommand;
bool mAttemptedWrite;
+ char mCycleMs[16]; // cycle_ms + suffix
+ char mLoadUs[16]; // load_us + suffix
+
}; // class FastThread
} // android
ALOGV("createAudioPatch() removing patch handle %d", *handle);
halHandle = mPatches[index]->mHalHandle;
Patch *removedPatch = mPatches[index];
+ // free resources owned by the removed patch if applicable
+ // 1) if a software patch is present, release the playback and capture threads and
+ // tracks created. This will also release the corresponding audio HAL patches
if ((removedPatch->mRecordPatchHandle
!= AUDIO_PATCH_HANDLE_NONE) ||
(removedPatch->mPlaybackPatchHandle !=
AUDIO_PATCH_HANDLE_NONE)) {
clearPatchConnections(removedPatch);
}
+ // 2) if the new patch and old patch source or sink are devices from different
+ // hw modules, clear the audio HAL patches now because they will not be updated
+ // by call to create_audio_patch() below which will happen on a different HW module
+ if (halHandle != AUDIO_PATCH_HANDLE_NONE) {
+ audio_module_handle_t hwModule = AUDIO_MODULE_HANDLE_NONE;
+ if ((removedPatch->mAudioPatch.sources[0].type == AUDIO_PORT_TYPE_DEVICE) &&
+ ((patch->sources[0].type != AUDIO_PORT_TYPE_DEVICE) ||
+ (removedPatch->mAudioPatch.sources[0].ext.device.hw_module !=
+ patch->sources[0].ext.device.hw_module))) {
+ hwModule = removedPatch->mAudioPatch.sources[0].ext.device.hw_module;
+ } else if ((patch->num_sinks == 0) ||
+ ((removedPatch->mAudioPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE) &&
+ ((patch->sinks[0].type != AUDIO_PORT_TYPE_DEVICE) ||
+ (removedPatch->mAudioPatch.sinks[0].ext.device.hw_module !=
+ patch->sinks[0].ext.device.hw_module)))) {
+ // Note on (patch->num_sinks == 0): this situation should not happen as
+ // these special patches are only created by the policy manager but just
+ // in case, systematically clear the HAL patch.
+ // Note that removedPatch->mAudioPatch.num_sinks cannot be 0 here because
+ // halHandle would be AUDIO_PATCH_HANDLE_NONE in this case.
+ hwModule = removedPatch->mAudioPatch.sinks[0].ext.device.hw_module;
+ }
+ if (hwModule != AUDIO_MODULE_HANDLE_NONE) {
+ ssize_t index = audioflinger->mAudioHwDevs.indexOfKey(hwModule);
+ if (index >= 0) {
+ audio_hw_device_t *hwDevice =
+ audioflinger->mAudioHwDevs.valueAt(index)->hwDevice();
+ hwDevice->release_audio_patch(hwDevice, halHandle);
+ }
+ }
+ }
mPatches.removeAt(index);
delete removedPatch;
break;
format,
frameCount,
NULL,
- IAudioFlinger::TRACK_DEFAULT);
+ AUDIO_INPUT_FLAG_NONE);
if (patch->mPatchRecord == 0) {
return NO_MEMORY;
}
format,
frameCount,
patch->mPatchRecord->buffer(),
- IAudioFlinger::TRACK_DEFAULT);
+ AUDIO_OUTPUT_FLAG_NONE);
if (patch->mPatchTrack == 0) {
return NO_MEMORY;
}
struct audio_patch mAudioPatch;
audio_patch_handle_t mHandle;
+ // handle for audio HAL patch handle present only when the audio HAL version is >= 3.0
audio_patch_handle_t mHalHandle;
+ // below members are used by a software audio patch connecting a source device from a
+ // given audio HW module to a sink device on an other audio HW module.
+ // playback thread created by createAudioPatch() and released by clearPatchConnections() if
+ // no existing playback thread can be used by the software patch
sp<PlaybackThread> mPlaybackThread;
+ // audio track created by createPatchConnections() and released by clearPatchConnections()
sp<PlaybackThread::PatchTrack> mPatchTrack;
+ // record thread created by createAudioPatch() and released by clearPatchConnections()
sp<RecordThread> mRecordThread;
+ // audio record created by createPatchConnections() and released by clearPatchConnections()
sp<RecordThread::PatchRecord> mPatchRecord;
+ // handle for audio patch connecting source device to record thread input.
+ // created by createPatchConnections() and released by clearPatchConnections()
audio_patch_handle_t mRecordPatchHandle;
+ // handle for audio patch connecting playback thread output to sink device
+ // created by createPatchConnections() and released by clearPatchConnections()
audio_patch_handle_t mPlaybackPatchHandle;
};
const sp<IMemory>& sharedBuffer,
audio_session_t sessionId,
int uid,
- IAudioFlinger::track_flags_t flags,
+ audio_output_flags_t flags,
track_type type);
virtual ~Track();
virtual status_t initCheck() const;
audio_stream_type_t streamType() const {
return mStreamType;
}
- bool isOffloaded() const { return (mFlags & IAudioFlinger::TRACK_OFFLOAD) != 0; }
- bool isDirect() const { return (mFlags & IAudioFlinger::TRACK_DIRECT) != 0; }
+ bool isOffloaded() const
+ { return (mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0; }
+ bool isDirect() const { return (mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0; }
status_t setParameters(const String8& keyValuePairs);
status_t attachAuxEffect(int EffectId);
void setAuxBuffer(int EffectId, int32_t *buffer);
virtual status_t setSyncEvent(const sp<SyncEvent>& event);
+ virtual bool isFastTrack() const { return (mFlags & AUDIO_OUTPUT_FLAG_FAST) != 0; }
+
protected:
// for numerous
friend class PlaybackThread;
AudioTrackServerProxy* mAudioTrackServerProxy;
bool mResumeToStopping; // track was paused in stopping state.
bool mFlushHwPending; // track requests for thread flush
-
+ audio_output_flags_t mFlags;
}; // end of Track
audio_format_t format,
size_t frameCount,
void *buffer,
- IAudioFlinger::track_flags_t flags);
+ audio_output_flags_t flags);
virtual ~PatchTrack();
virtual status_t start(AudioSystem::sync_event_t event =
void *buffer,
audio_session_t sessionId,
int uid,
- IAudioFlinger::track_flags_t flags,
+ audio_input_flags_t flags,
track_type type);
virtual ~RecordTrack();
virtual status_t initCheck() const;
int64_t sourceFramesRead,
uint32_t halSampleRate,
const ExtendedTimestamp ×tamp);
+
+ virtual bool isFastTrack() const { return (mFlags & AUDIO_INPUT_FLAG_FAST) != 0; }
+
private:
friend class AudioFlinger; // for mState
// used by the record thread to convert frames to proper destination format
RecordBufferConverter *mRecordBufferConverter;
+ audio_input_flags_t mFlags;
};
// playback track, used by PatchPanel
audio_format_t format,
size_t frameCount,
void *buffer,
- IAudioFlinger::track_flags_t flags);
+ audio_input_flags_t flags);
virtual ~PatchRecord();
// AudioBufferProvider interface
}
}
+// checkEffectCompatibility_l() must be called with ThreadBase::mLock held
+status_t AudioFlinger::RecordThread::checkEffectCompatibility_l(
+ const effect_descriptor_t *desc, audio_session_t sessionId)
+{
+ // No global effect sessions on record threads
+ if (sessionId == AUDIO_SESSION_OUTPUT_MIX || sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
+ ALOGW("checkEffectCompatibility_l(): global effect %s on record thread %s",
+ desc->name, mThreadName);
+ return BAD_VALUE;
+ }
+ // only pre processing effects on record thread
+ if ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_PRE_PROC) {
+ ALOGW("checkEffectCompatibility_l(): non pre processing effect %s on record thread %s",
+ desc->name, mThreadName);
+ return BAD_VALUE;
+ }
+ audio_input_flags_t flags = mInput->flags;
+ if (hasFastCapture() || (flags & AUDIO_INPUT_FLAG_FAST)) {
+ if (flags & AUDIO_INPUT_FLAG_RAW) {
+ ALOGW("checkEffectCompatibility_l(): effect %s on record thread %s in raw mode",
+ desc->name, mThreadName);
+ return BAD_VALUE;
+ }
+ if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) == 0) {
+ ALOGW("checkEffectCompatibility_l(): non HW effect %s on record thread %s in fast mode",
+ desc->name, mThreadName);
+ return BAD_VALUE;
+ }
+ }
+ return NO_ERROR;
+}
+
+// checkEffectCompatibility_l() must be called with ThreadBase::mLock held
+status_t AudioFlinger::PlaybackThread::checkEffectCompatibility_l(
+ const effect_descriptor_t *desc, audio_session_t sessionId)
+{
+ // no preprocessing on playback threads
+ if ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC) {
+ ALOGW("checkEffectCompatibility_l(): pre processing effect %s created on playback"
+ " thread %s", desc->name, mThreadName);
+ return BAD_VALUE;
+ }
+
+ switch (mType) {
+ case MIXER: {
+ // Reject any effect on mixer multichannel sinks.
+ // TODO: fix both format and multichannel issues with effects.
+ if (mChannelCount != FCC_2) {
+ ALOGW("checkEffectCompatibility_l(): effect %s for multichannel(%d) on MIXER"
+ " thread %s", desc->name, mChannelCount, mThreadName);
+ return BAD_VALUE;
+ }
+ audio_output_flags_t flags = mOutput->flags;
+ if (hasFastMixer() || (flags & AUDIO_OUTPUT_FLAG_FAST)) {
+ if (sessionId == AUDIO_SESSION_OUTPUT_MIX) {
+ // global effects are applied only to non fast tracks if they are SW
+ if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) == 0) {
+ break;
+ }
+ } else if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
+ // only post processing on output stage session
+ if ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_POST_PROC) {
+ ALOGW("checkEffectCompatibility_l(): non post processing effect %s not allowed"
+ " on output stage session", desc->name);
+ return BAD_VALUE;
+ }
+ } else {
+ // no restriction on effects applied on non fast tracks
+ if ((hasAudioSession_l(sessionId) & ThreadBase::FAST_SESSION) == 0) {
+ break;
+ }
+ }
+ if (flags & AUDIO_OUTPUT_FLAG_RAW) {
+ ALOGW("checkEffectCompatibility_l(): effect %s on playback thread in raw mode",
+ desc->name);
+ return BAD_VALUE;
+ }
+ if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) == 0) {
+ ALOGW("checkEffectCompatibility_l(): non HW effect %s on playback thread"
+ " in fast mode", desc->name);
+ return BAD_VALUE;
+ }
+ }
+ } break;
+ case OFFLOAD:
+ // nothing actionable on offload threads, if the effect:
+ // - is offloadable: the effect can be created
+ // - is NOT offloadable: the effect should still be created, but EffectHandle::enable()
+ // will take care of invalidating the tracks of the thread
+ break;
+ case DIRECT:
+ // Reject any effect on Direct output threads for now, since the format of
+ // mSinkBuffer is not guaranteed to be compatible with effect processing (PCM 16 stereo).
+ ALOGW("checkEffectCompatibility_l(): effect %s on DIRECT output thread %s",
+ desc->name, mThreadName);
+ return BAD_VALUE;
+ case DUPLICATING:
+ // Reject any effect on mixer multichannel sinks.
+ // TODO: fix both format and multichannel issues with effects.
+ if (mChannelCount != FCC_2) {
+ ALOGW("checkEffectCompatibility_l(): effect %s for multichannel(%d)"
+ " on DUPLICATING thread %s", desc->name, mChannelCount, mThreadName);
+ return BAD_VALUE;
+ }
+ if ((sessionId == AUDIO_SESSION_OUTPUT_STAGE) || (sessionId == AUDIO_SESSION_OUTPUT_MIX)) {
+ ALOGW("checkEffectCompatibility_l(): global effect %s on DUPLICATING"
+ " thread %s", desc->name, mThreadName);
+ return BAD_VALUE;
+ }
+ if ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
+ ALOGW("checkEffectCompatibility_l(): post processing effect %s on"
+ " DUPLICATING thread %s", desc->name, mThreadName);
+ return BAD_VALUE;
+ }
+ if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) != 0) {
+ ALOGW("checkEffectCompatibility_l(): HW tunneled effect %s on"
+ " DUPLICATING thread %s", desc->name, mThreadName);
+ return BAD_VALUE;
+ }
+ break;
+ default:
+ LOG_ALWAYS_FATAL("checkEffectCompatibility_l(): wrong thread type %d", mType);
+ }
+
+ return NO_ERROR;
+}
+
// ThreadBase::createEffect_l() must be called with AudioFlinger::mLock held
sp<AudioFlinger::EffectHandle> AudioFlinger::ThreadBase::createEffect_l(
const sp<AudioFlinger::Client>& client,
audio_session_t sessionId,
effect_descriptor_t *desc,
int *enabled,
- status_t *status,
- bool pinned)
+ status_t *status)
{
sp<EffectModule> effect;
sp<EffectHandle> handle;
goto Exit;
}
- // Reject any effect on Direct output threads for now, since the format of
- // mSinkBuffer is not guaranteed to be compatible with effect processing (PCM 16 stereo).
- if (mType == DIRECT) {
- ALOGW("createEffect_l() Cannot add effect %s on Direct output type thread %s",
- desc->name, mThreadName);
- lStatus = BAD_VALUE;
- goto Exit;
- }
-
- // Reject any effect on mixer or duplicating multichannel sinks.
- // TODO: fix both format and multichannel issues with effects.
- if ((mType == MIXER || mType == DUPLICATING) && mChannelCount != FCC_2) {
- ALOGW("createEffect_l() Cannot add effect %s for multichannel(%d) %s threads",
- desc->name, mChannelCount, mType == MIXER ? "MIXER" : "DUPLICATING");
- lStatus = BAD_VALUE;
- goto Exit;
- }
-
- // Allow global effects only on offloaded and mixer threads
- if (sessionId == AUDIO_SESSION_OUTPUT_MIX) {
- switch (mType) {
- case MIXER:
- case OFFLOAD:
- break;
- case DIRECT:
- case DUPLICATING:
- case RECORD:
- default:
- ALOGW("createEffect_l() Cannot add global effect %s on thread %s",
- desc->name, mThreadName);
- lStatus = BAD_VALUE;
- goto Exit;
- }
- }
-
- // Only Pre processor effects are allowed on input threads and only on input threads
- if ((mType == RECORD) != ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC)) {
- ALOGW("createEffect_l() effect %s (flags %08x) created on wrong thread type %d",
- desc->name, desc->flags, mType);
- lStatus = BAD_VALUE;
- goto Exit;
- }
-
ALOGV("createEffect_l() thread %p effect %s on session %d", this, desc->name, sessionId);
{ // scope for mLock
Mutex::Autolock _l(mLock);
+ lStatus = checkEffectCompatibility_l(desc, sessionId);
+ if (lStatus != NO_ERROR) {
+ goto Exit;
+ }
+
// check for existing effect chain with the requested audio session
chain = getEffectChain_l(sessionId);
if (chain == 0) {
}
effectRegistered = true;
// create a new effect module if none present in the chain
- lStatus = chain->createEffect_l(effect, this, desc, id, sessionId, pinned);
+ effect = new EffectModule(this, chain, desc, id, sessionId);
+ lStatus = effect->status();
+ if (lStatus != NO_ERROR) {
+ goto Exit;
+ }
+ effect->setOffloaded(mType == OFFLOAD, mId);
+
+ lStatus = chain->addEffect_l(effect);
if (lStatus != NO_ERROR) {
goto Exit;
}
return handle;
}
-void AudioFlinger::ThreadBase::disconnectEffectHandle(EffectHandle *handle,
- bool unpinIfLast)
-{
- bool remove = false;
- sp<EffectModule> effect;
- {
- Mutex::Autolock _l(mLock);
-
- effect = handle->effect().promote();
- if (effect == 0) {
- return;
- }
- // restore suspended effects if the disconnected handle was enabled and the last one.
- remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast);
- if (remove) {
- removeEffect_l(effect, true);
- }
- }
- if (remove) {
- mAudioFlinger->updateOrphanEffectChains(effect);
- AudioSystem::unregisterEffect(effect->id());
- if (handle->enabled()) {
- checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
- }
- }
-}
-
sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect(audio_session_t sessionId,
int effectId)
{
return NO_ERROR;
}
-void AudioFlinger::ThreadBase::removeEffect_l(const sp<EffectModule>& effect, bool release) {
+void AudioFlinger::ThreadBase::removeEffect_l(const sp<EffectModule>& effect) {
- ALOGV("%s %p effect %p", __FUNCTION__, this, effect.get());
+ ALOGV("removeEffect_l() %p effect %p", this, effect.get());
effect_descriptor_t desc = effect->desc();
if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
detachAuxEffect_l(effect->id());
sp<EffectChain> chain = effect->chain().promote();
if (chain != 0) {
// remove effect chain if removing last effect
- if (chain->removeEffect_l(effect, release) == 0) {
+ if (chain->removeEffect_l(effect) == 0) {
removeEffectChain_l(chain);
}
} else {
size_t *pFrameCount,
const sp<IMemory>& sharedBuffer,
audio_session_t sessionId,
- IAudioFlinger::track_flags_t *flags,
+ audio_output_flags_t *flags,
pid_t tid,
int uid,
status_t *status)
size_t frameCount = *pFrameCount;
sp<Track> track;
status_t lStatus;
+ audio_output_flags_t outputFlags = mOutput->flags;
+
+ // special case for FAST flag considered OK if fast mixer is present
+ if (hasFastMixer()) {
+ outputFlags = (audio_output_flags_t)(outputFlags | AUDIO_OUTPUT_FLAG_FAST);
+ }
+
+ // Check if requested flags are compatible with output stream flags
+ if ((*flags & outputFlags) != *flags) {
+ ALOGW("createTrack_l(): mismatch between requested flags (%08x) and output flags (%08x)",
+ *flags, outputFlags);
+ *flags = (audio_output_flags_t)(*flags & outputFlags);
+ }
// client expresses a preference for FAST, but we get the final say
- if (*flags & IAudioFlinger::TRACK_FAST) {
+ if (*flags & AUDIO_OUTPUT_FLAG_FAST) {
if (
// PCM data
audio_is_linear_pcm(format) &&
}
frameCount = max(frameCount, mFrameCount * sFastTrackMultiplier); // incl framecount 0
}
- ALOGV("AUDIO_OUTPUT_FLAG_FAST accepted: frameCount=%zu mFrameCount=%zu",
- frameCount, mFrameCount);
+
+ // check compatibility with audio effects.
+ { // scope for mLock
+ Mutex::Autolock _l(mLock);
+ // do not accept RAW flag if post processing are present. Note that post processing on
+ // a fast mixer are necessarily hardware
+ sp<EffectChain> chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_STAGE);
+ if (chain != 0) {
+ ALOGV_IF((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0,
+ "AUDIO_OUTPUT_FLAG_RAW denied: post processing effect present");
+ *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
+ }
+ // Do not accept FAST flag if software global effects are present
+ chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
+ if (chain != 0) {
+ ALOGV_IF((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0,
+ "AUDIO_OUTPUT_FLAG_RAW denied: global effect present");
+ *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
+ if (chain->hasSoftwareEffect()) {
+ ALOGV("AUDIO_OUTPUT_FLAG_FAST denied: software global effect present");
+ *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
+ }
+ }
+ // Do not accept FAST flag if the session has software effects
+ chain = getEffectChain_l(sessionId);
+ if (chain != 0) {
+ ALOGV_IF((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0,
+ "AUDIO_OUTPUT_FLAG_RAW denied: effect present on session");
+ *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
+ if (chain->hasSoftwareEffect()) {
+ ALOGV("AUDIO_OUTPUT_FLAG_FAST denied: software effect present on session");
+ *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
+ }
+ }
+ }
+ ALOGV_IF((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0,
+ "AUDIO_OUTPUT_FLAG_FAST accepted: frameCount=%zu mFrameCount=%zu",
+ frameCount, mFrameCount);
} else {
ALOGV("AUDIO_OUTPUT_FLAG_FAST denied: sharedBuffer=%p frameCount=%zu "
"mFrameCount=%zu format=%#x mFormat=%#x isLinear=%d channelMask=%#x "
sharedBuffer.get(), frameCount, mFrameCount, format, mFormat,
audio_is_linear_pcm(format),
channelMask, sampleRate, mSampleRate, hasFastMixer(), tid, mFastTrackAvailMask);
- *flags &= ~IAudioFlinger::TRACK_FAST;
+ *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
}
}
// For normal PCM streaming tracks, update minimum frame count.
// to be at least 2 x the normal mixer frame count and cover audio hardware latency.
// This is probably too conservative, but legacy application code may depend on it.
// If you change this calculation, also review the start threshold which is related.
- if (!(*flags & IAudioFlinger::TRACK_FAST)
+ if (!(*flags & AUDIO_OUTPUT_FLAG_FAST)
&& audio_has_proportional_frames(format) && sharedBuffer == 0) {
// this must match AudioTrack.cpp calculateMinFrameCount().
// TODO: Move to a common library
chain->incTrackCnt();
}
- if ((*flags & IAudioFlinger::TRACK_FAST) && (tid != -1)) {
+ if ((*flags & AUDIO_OUTPUT_FLAG_FAST) && (tid != -1)) {
pid_t callingPid = IPCThreadState::self()->getCallingPid();
// we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful,
// so ask activity manager to do this on our behalf
mCallbackThread->resetDraining();
}
+void AudioFlinger::PlaybackThread::errorCallback()
+{
+ ALOG_ASSERT(mCallbackThread != 0);
+ mCallbackThread->setAsyncError();
+}
+
void AudioFlinger::PlaybackThread::resetWriteBlocked(uint32_t sequence)
{
Mutex::Autolock _l(mLock);
case STREAM_CBK_EVENT_DRAIN_READY:
me->drainCallback();
break;
+ case STREAM_CBK_EVENT_ERROR:
+ me->errorCallback();
+ break;
default:
ALOGW("asyncCallback() unknown event %d", event);
break;
kUseFastMixer == FastMixer_Dynamic)) {
size_t minNormalFrameCount = (kMinNormalSinkBufferSizeMs * mSampleRate) / 1000;
size_t maxNormalFrameCount = (kMaxNormalSinkBufferSizeMs * mSampleRate) / 1000;
+
// round up minimum and round down maximum to nearest 16 frames to satisfy AudioMixer
minNormalFrameCount = (minNormalFrameCount + 15) & ~15;
maxNormalFrameCount = maxNormalFrameCount & ~15;
multiplier = (double) maxNormalFrameCount / (double) mFrameCount;
}
} else {
- // prefer an even multiplier, for compatibility with doubling of fast tracks due to HAL
- // SRC (it would be unusual for the normal sink buffer size to not be a multiple of fast
- // track, but we sometimes have to do this to satisfy the maximum frame count
- // constraint)
- // FIXME this rounding up should not be done if no HAL SRC
- uint32_t truncMult = (uint32_t) multiplier;
- if ((truncMult & 1)) {
- if ((truncMult + 1) * mFrameCount <= maxNormalFrameCount) {
- ++truncMult;
- }
- }
- multiplier = (double) truncMult;
+ multiplier = floor(multiplier);
}
}
mNormalFrameCount = multiplier * mFrameCount;
}
}
-uint32_t AudioFlinger::PlaybackThread::hasAudioSession(audio_session_t sessionId) const
+// hasAudioSession_l() must be called with ThreadBase::mLock held
+uint32_t AudioFlinger::PlaybackThread::hasAudioSession_l(audio_session_t sessionId) const
{
- Mutex::Autolock _l(mLock);
uint32_t result = 0;
if (getEffectChain_l(sessionId) != 0) {
result = EFFECT_SESSION;
sp<Track> track = mTracks[i];
if (sessionId == track->sessionId() && !track->isInvalid()) {
result |= TRACK_SESSION;
+ if (track->isFastTrack()) {
+ result |= FAST_SESSION;
+ }
break;
}
}
if (timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] >= 0) {
kernelLocationUpdate = true;
} else {
- ALOGV("getTimestamp error - no valid kernel position");
+ ALOGVV("getTimestamp error - no valid kernel position");
}
// copy over kernel info
// (1) mixer threads without a fast mixer (which has its own warm-up)
// (2) minimum buffer sized tracks (even if the track is full,
// the app won't fill fast enough to handle the sudden draw).
+ //
+ // Total time spent in last processing cycle equals time spent in
+ // 1. threadLoop_write, as well as time spent in
+ // 2. threadLoop_mix (significant for heavy mixing, especially
+ // on low tier processors)
// it's OK if deltaMs is an overestimate.
const int32_t deltaMs =
" ret(%zd) deltaMs(%d) requires sleep %d ms",
this, ret, deltaMs, throttleMs);
mThreadThrottleTimeMs += throttleMs;
+ // Throttle must be attributed to the previous mixer loop's write time
+ // to allow back-to-back throttling.
+ lastWriteFinished += throttleMs * 1000000;
} else {
uint32_t diff = mThreadThrottleTimeMs - mThreadThrottleEndMs;
if (diff > 0) {
status_t AudioFlinger::MixerThread::createAudioPatch_l(const struct audio_patch *patch,
audio_patch_handle_t *handle)
{
- AutoPark<FastMixer> park(mFastMixer);
-
- status_t status = PlaybackThread::createAudioPatch_l(patch, handle);
-
+ status_t status;
+ if (property_get_bool("af.patch_park", false /* default_value */)) {
+ // Park FastMixer to avoid potential DOS issues with writing to the HAL
+ // or if HAL does not properly lock against access.
+ AutoPark<FastMixer> park(mFastMixer);
+ status = PlaybackThread::createAudioPatch_l(patch, handle);
+ } else {
+ status = PlaybackThread::createAudioPatch_l(patch, handle);
+ }
return status;
}
status_t AudioFlinger::MixerThread::releaseAudioPatch_l(const audio_patch_handle_t handle)
{
- AutoPark<FastMixer> park(mFastMixer);
-
- status_t status = PlaybackThread::releaseAudioPatch_l(handle);
-
+ status_t status;
+ if (property_get_bool("af.patch_park", false /* default_value */)) {
+ // Park FastMixer to avoid potential DOS issues with writing to the HAL
+ // or if HAL does not properly lock against access.
+ AutoPark<FastMixer> park(mFastMixer);
+ status = PlaybackThread::releaseAudioPatch_l(handle);
+ } else {
+ status = PlaybackThread::releaseAudioPatch_l(handle);
+ }
return status;
}
broadcast_l();
}
+void AudioFlinger::PlaybackThread::onAsyncError()
+{
+ for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
+ invalidateTracks((audio_stream_type_t)i);
+ }
+}
+
void AudioFlinger::MixerThread::threadLoop_mix()
{
// mix buffers...
}
} else if (track->isResumePending()) {
track->resumeAck();
- if (last && mHwPaused) {
- doHwResume = true;
- mHwPaused = false;
+ if (last) {
+ mLeftVolFloat = mRightVolFloat = -1.0;
+ if (mHwPaused) {
+ doHwResume = true;
+ mHwPaused = false;
+ }
}
}
if (track->mFillingUpStatus == Track::FS_FILLED) {
track->mFillingUpStatus = Track::FS_ACTIVE;
- // make sure processVolume_l() will apply new volume even if 0
- mLeftVolFloat = mRightVolFloat = -1.0;
+ if (last) {
+ // make sure processVolume_l() will apply new volume even if 0
+ mLeftVolFloat = mRightVolFloat = -1.0;
+ }
if (!mHwSupportsPause) {
track->resumeAck();
}
: Thread(false /*canCallJava*/),
mPlaybackThread(playbackThread),
mWriteAckSequence(0),
- mDrainSequence(0)
+ mDrainSequence(0),
+ mAsyncError(false)
{
}
while (!exitPending()) {
uint32_t writeAckSequence;
uint32_t drainSequence;
+ bool asyncError;
{
Mutex::Autolock _l(mLock);
while (!((mWriteAckSequence & 1) ||
(mDrainSequence & 1) ||
+ mAsyncError ||
exitPending())) {
mWaitWorkCV.wait(mLock);
}
mWriteAckSequence &= ~1;
drainSequence = mDrainSequence;
mDrainSequence &= ~1;
+ asyncError = mAsyncError;
+ mAsyncError = false;
}
{
sp<AudioFlinger::PlaybackThread> playbackThread = mPlaybackThread.promote();
if (drainSequence & 1) {
playbackThread->resetDraining(drainSequence >> 1);
}
+ if (asyncError) {
+ playbackThread->onAsyncError();
+ }
}
}
}
}
}
+void AudioFlinger::AsyncCallbackThread::setAsyncError()
+{
+ Mutex::Autolock _l(mLock);
+ mAsyncError = true;
+ mWaitWorkCV.signal();
+}
+
// ----------------------------------------------------------------------------
AudioFlinger::OffloadThread::OffloadThread(const sp<AudioFlinger>& audioFlinger,
// enable write to audio HAL
mSleepTimeUs = 0;
+ mLeftVolFloat = mRightVolFloat = -1.0;
+
// Do not handle new data in this iteration even if track->framesReady()
mixerStatus = MIXER_TRACKS_ENABLED;
}
ALOGVV("OffloadThread: track %d s=%08x [OK]", track->name(), cblk->mServer);
if (track->mFillingUpStatus == Track::FS_FILLED) {
track->mFillingUpStatus = Track::FS_ACTIVE;
- // make sure processVolume_l() will apply new volume even if 0
- mLeftVolFloat = mRightVolFloat = -1.0;
+ if (last) {
+ // make sure processVolume_l() will apply new volume even if 0
+ mLeftVolFloat = mRightVolFloat = -1.0;
+ }
}
if (last) {
mChannelMask,
frameCount,
IPCThreadState::self()->getCallingUid());
- if (outputTrack->cblk() != NULL) {
- thread->setStreamVolume(AUDIO_STREAM_PATCH, 1.0f);
- mOutputTracks.add(outputTrack);
- ALOGV("addOutputTrack() track %p, on thread %p", outputTrack.get(), thread);
- updateWaitTime_l();
+ status_t status = outputTrack != 0 ? outputTrack->initCheck() : (status_t) NO_MEMORY;
+ if (status != NO_ERROR) {
+ ALOGE("addOutputTrack() initCheck failed %d", status);
+ return;
}
+ thread->setStreamVolume(AUDIO_STREAM_PATCH, 1.0f);
+ mOutputTracks.add(outputTrack);
+ ALOGV("addOutputTrack() track %p, on thread %p", outputTrack.get(), thread);
+ updateWaitTime_l();
}
void AudioFlinger::DuplicatingThread::removeOutputTrack(MixerThread *thread)
for (;;) {
Vector< sp<EffectChain> > effectChains;
- // sleep with mutex unlocked
- if (sleepUs > 0) {
- ATRACE_BEGIN("sleep");
- usleep(sleepUs);
- ATRACE_END();
- sleepUs = 0;
- }
-
// activeTracks accumulates a copy of a subset of mActiveTracks
Vector< sp<RecordTrack> > activeTracks;
break;
}
+ // sleep with mutex unlocked
+ if (sleepUs > 0) {
+ ATRACE_BEGIN("sleepC");
+ mWaitWorkCV.waitRelative(mLock, microseconds((nsecs_t)sleepUs));
+ ATRACE_END();
+ sleepUs = 0;
+ continue;
+ }
+
// if no active track(s), then standby and release wakelock
size_t size = mActiveTracks.size();
if (size == 0) {
}
bool doBroadcast = false;
+ bool allStopped = true;
for (size_t i = 0; i < size; ) {
activeTrack = mActiveTracks[i];
case TrackBase::STARTING_1:
sleepUs = 10000;
i++;
+ allStopped = false;
continue;
case TrackBase::STARTING_2:
doBroadcast = true;
mStandby = false;
activeTrack->mState = TrackBase::ACTIVE;
+ allStopped = false;
break;
case TrackBase::ACTIVE:
+ allStopped = false;
break;
case TrackBase::IDLE:
fastTrack = activeTrack;
}
}
+
+ if (allStopped) {
+ standbyIfNotAlreadyInStandby();
+ }
if (doBroadcast) {
mStartStopCond.broadcast();
}
mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] = systemTime();
// Update server timestamp with kernel stats
- if (mInput->stream->get_capture_position != nullptr) {
+ if (mInput->stream->get_capture_position != nullptr
+ && mPipeSource.get() == nullptr /* don't obtain for FastCapture, could block */) {
int64_t position, time;
int ret = mInput->stream->get_capture_position(mInput->stream, &position, &time);
if (ret == NO_ERROR) {
audio_session_t sessionId,
size_t *notificationFrames,
int uid,
- IAudioFlinger::track_flags_t *flags,
+ audio_input_flags_t *flags,
pid_t tid,
status_t *status)
{
size_t frameCount = *pFrameCount;
sp<RecordTrack> track;
status_t lStatus;
+ audio_input_flags_t inputFlags = mInput->flags;
+
+ // special case for FAST flag considered OK if fast capture is present
+ if (hasFastCapture()) {
+ inputFlags = (audio_input_flags_t)(inputFlags | AUDIO_INPUT_FLAG_FAST);
+ }
+
+ // Check if requested flags are compatible with output stream flags
+ if ((*flags & inputFlags) != *flags) {
+ ALOGW("createRecordTrack_l(): mismatch between requested flags (%08x) and"
+ " input flags (%08x)",
+ *flags, inputFlags);
+ *flags = (audio_input_flags_t)(*flags & inputFlags);
+ }
// client expresses a preference for FAST, but we get the final say
- if (*flags & IAudioFlinger::TRACK_FAST) {
+ if (*flags & AUDIO_INPUT_FLAG_FAST) {
if (
// we formerly checked for a callback handler (non-0 tid),
// but that is no longer required for TRANSFER_OBTAIN mode
// there are sufficient fast track slots available
mFastTrackAvail
) {
- ALOGV("AUDIO_INPUT_FLAG_FAST accepted: frameCount=%zu mFrameCount=%zu",
- frameCount, mFrameCount);
+ // check compatibility with audio effects.
+ Mutex::Autolock _l(mLock);
+ // Do not accept FAST flag if the session has software effects
+ sp<EffectChain> chain = getEffectChain_l(sessionId);
+ if (chain != 0) {
+ ALOGV_IF((*flags & AUDIO_INPUT_FLAG_RAW) != 0,
+ "AUDIO_INPUT_FLAG_RAW denied: effect present on session");
+ *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
+ if (chain->hasSoftwareEffect()) {
+ ALOGV("AUDIO_INPUT_FLAG_FAST denied: software effect present on session");
+ *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
+ }
+ }
+ ALOGV_IF((*flags & AUDIO_INPUT_FLAG_FAST) != 0,
+ "AUDIO_INPUT_FLAG_FAST accepted: frameCount=%zu mFrameCount=%zu",
+ frameCount, mFrameCount);
} else {
ALOGV("AUDIO_INPUT_FLAG_FAST denied: frameCount=%zu mFrameCount=%zu mPipeFramesP2=%zu "
"format=%#x isLinear=%d channelMask=%#x sampleRate=%u mSampleRate=%u "
frameCount, mFrameCount, mPipeFramesP2,
format, audio_is_linear_pcm(format), channelMask, sampleRate, mSampleRate,
hasFastCapture(), tid, mFastTrackAvail);
- *flags &= ~IAudioFlinger::TRACK_FAST;
+ *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
}
}
// compute track buffer size in frames, and suggest the notification frame count
- if (*flags & IAudioFlinger::TRACK_FAST) {
+ if (*flags & AUDIO_INPUT_FLAG_FAST) {
// fast track: frame count is exactly the pipe depth
frameCount = mPipeFramesP2;
// ignore requested notificationFrames, and always notify exactly once every HAL buffer
setEffectSuspended_l(FX_IID_AEC, suspend, sessionId);
setEffectSuspended_l(FX_IID_NS, suspend, sessionId);
- if ((*flags & IAudioFlinger::TRACK_FAST) && (tid != -1)) {
+ if ((*flags & AUDIO_INPUT_FLAG_FAST) && (tid != -1)) {
pid_t callingPid = IPCThreadState::self()->getCallingPid();
// we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful,
// so ask activity manager to do this on our behalf
}
// note that threadLoop may still be processing the track at this point [without lock]
recordTrack->mState = TrackBase::PAUSING;
+ // signal thread to stop
+ mWaitWorkCV.broadcast();
// do not wait for mStartStopCond if exiting
if (exitPending()) {
return true;
return mInput->stream->get_input_frames_lost(mInput->stream);
}
-uint32_t AudioFlinger::RecordThread::hasAudioSession(audio_session_t sessionId) const
+// hasAudioSession_l() must be called with ThreadBase::mLock held
+uint32_t AudioFlinger::RecordThread::hasAudioSession_l(audio_session_t sessionId) const
{
- Mutex::Autolock _l(mLock);
uint32_t result = 0;
if (getEffectChain_l(sessionId) != 0) {
result = EFFECT_SESSION;
for (size_t i = 0; i < mTracks.size(); ++i) {
if (sessionId == mTracks[i]->sessionId()) {
result |= TRACK_SESSION;
+ if (mTracks[i]->isFastTrack()) {
+ result |= FAST_SESSION;
+ }
break;
}
}
audio_session_t sessionId,
effect_descriptor_t *desc,
int *enabled,
- status_t *status /*non-NULL*/,
- bool pinned);
+ status_t *status /*non-NULL*/);
// return values for hasAudioSession (bit field)
enum effect_state {
EFFECT_SESSION = 0x1, // the audio session corresponds to at least one
// effect
- TRACK_SESSION = 0x2 // the audio session corresponds to at least one
+ TRACK_SESSION = 0x2, // the audio session corresponds to at least one
// track
+ FAST_SESSION = 0x4 // the audio session corresponds to at least one
+ // fast track
};
// get effect chain corresponding to session Id.
status_t addEffect_l(const sp< EffectModule>& effect);
// remove and effect module. Also removes the effect chain is this was the last
// effect
- void removeEffect_l(const sp< EffectModule>& effect, bool release = false);
- // disconnect an effect handle from module and destroy module if last handle
- void disconnectEffectHandle(EffectHandle *handle, bool unpinIfLast);
+ void removeEffect_l(const sp< EffectModule>& effect);
// detach all tracks connected to an auxiliary effect
virtual void detachAuxEffect_l(int effectId __unused) {}
- // returns either EFFECT_SESSION if effects on this audio session exist in one
- // chain, or TRACK_SESSION if tracks on this audio session exist, or both
- virtual uint32_t hasAudioSession(audio_session_t sessionId) const = 0;
+ // returns a combination of:
+ // - EFFECT_SESSION if effects on this audio session exist in one chain
+ // - TRACK_SESSION if tracks on this audio session exist
+ // - FAST_SESSION if fast tracks on this audio session exist
+ virtual uint32_t hasAudioSession_l(audio_session_t sessionId) const = 0;
+ uint32_t hasAudioSession(audio_session_t sessionId) const {
+ Mutex::Autolock _l(mLock);
+ return hasAudioSession_l(sessionId);
+ }
+
// the value returned by default implementation is not important as the
// strategy is only meaningful for PlaybackThread which implements this method
virtual uint32_t getStrategyForSession_l(audio_session_t sessionId __unused)
void systemReady();
+ // checkEffectCompatibility_l() must be called with ThreadBase::mLock held
+ virtual status_t checkEffectCompatibility_l(const effect_descriptor_t *desc,
+ audio_session_t sessionId) = 0;
+
mutable Mutex mLock;
protected:
// RefBase
virtual void onFirstRef();
+ virtual status_t checkEffectCompatibility_l(const effect_descriptor_t *desc,
+ audio_session_t sessionId);
+
protected:
// Code snippets that were lifted up out of threadLoop()
virtual void threadLoop_mix() = 0;
void resetWriteBlocked(uint32_t sequence);
void drainCallback();
void resetDraining(uint32_t sequence);
+ void errorCallback();
static int asyncCallback(stream_callback_event_t event, void *param, void *cookie);
virtual bool waitingAsyncCallback_l();
virtual bool shouldStandby_l();
virtual void onAddNewTrack_l();
+ void onAsyncError(); // error reported by AsyncCallbackThread
// ThreadBase virtuals
virtual void preExit();
size_t *pFrameCount,
const sp<IMemory>& sharedBuffer,
audio_session_t sessionId,
- IAudioFlinger::track_flags_t *flags,
+ audio_output_flags_t *flags,
pid_t tid,
int uid,
status_t *status /*non-NULL*/);
virtual status_t addEffectChain_l(const sp<EffectChain>& chain);
virtual size_t removeEffectChain_l(const sp<EffectChain>& chain);
- virtual uint32_t hasAudioSession(audio_session_t sessionId) const;
+ virtual uint32_t hasAudioSession_l(audio_session_t sessionId) const;
virtual uint32_t getStrategyForSession_l(audio_session_t sessionId);
virtual bool waitingAsyncCallback_l();
virtual void invalidateTracks(audio_stream_type_t streamType);
- virtual bool keepWakeLock() const { return mKeepWakeLock; }
+ virtual bool keepWakeLock() const { return (mKeepWakeLock || (mDrainSequence & 1)); }
private:
size_t mPausedWriteLength; // length in bytes of write interrupted by pause
void resetWriteBlocked();
void setDraining(uint32_t sequence);
void resetDraining();
+ void setAsyncError();
private:
const wp<PlaybackThread> mPlaybackThread;
uint32_t mDrainSequence;
Condition mWaitWorkCV;
Mutex mLock;
+ bool mAsyncError;
};
class DuplicatingThread : public MixerThread {
audio_session_t sessionId,
size_t *notificationFrames,
int uid,
- IAudioFlinger::track_flags_t *flags,
+ audio_input_flags_t *flags,
pid_t tid,
status_t *status /*non-NULL*/);
virtual status_t addEffectChain_l(const sp<EffectChain>& chain);
virtual size_t removeEffectChain_l(const sp<EffectChain>& chain);
- virtual uint32_t hasAudioSession(audio_session_t sessionId) const;
+ virtual uint32_t hasAudioSession_l(audio_session_t sessionId) const;
// Return the set of unique session IDs across all tracks.
// The keys are the session IDs, and the associated values are meaningless.
bool hasFastCapture() const { return mFastCapture != 0; }
virtual void getAudioPortConfig(struct audio_port_config *config);
+ virtual status_t checkEffectCompatibility_l(const effect_descriptor_t *desc,
+ audio_session_t sessionId);
+
private:
// Enter standby if not already in standby, and set mStandby flag
void standbyIfNotAlreadyInStandby();
void *buffer,
audio_session_t sessionId,
int uid,
- IAudioFlinger::track_flags_t flags,
bool isOut,
alloc_type alloc = ALLOC_CBLK,
track_type type = TYPE_DEFAULT);
sp<IMemory> getBuffers() const { return mBufferMemory; }
void* buffer() const { return mBuffer; }
- bool isFastTrack() const { return (mFlags & IAudioFlinger::TRACK_FAST) != 0; }
+ virtual bool isFastTrack() const = 0;
bool isOutputTrack() const { return (mType == TYPE_OUTPUT); }
bool isPatchTrack() const { return (mType == TYPE_PATCH); }
bool isExternalTrack() const { return !isOutputTrack() && !isPatchTrack(); }
const audio_session_t mSessionId;
int mUid;
Vector < sp<SyncEvent> >mSyncEvents;
- const IAudioFlinger::track_flags_t mFlags;
const bool mIsOut;
ServerProxy* mServerProxy;
const int mId;
void *buffer,
audio_session_t sessionId,
int clientUid,
- IAudioFlinger::track_flags_t flags,
bool isOut,
alloc_type alloc,
track_type type)
mChannelCount * audio_bytes_per_sample(format) : sizeof(int8_t)),
mFrameCount(frameCount),
mSessionId(sessionId),
- mFlags(flags),
mIsOut(isOut),
mServerProxy(NULL),
mId(android_atomic_inc(&nextTrackId)),
const sp<IMemory>& sharedBuffer,
audio_session_t sessionId,
int uid,
- IAudioFlinger::track_flags_t flags,
+ audio_output_flags_t flags,
track_type type)
: TrackBase(thread, client, sampleRate, format, channelMask, frameCount,
(sharedBuffer != 0) ? sharedBuffer->pointer() : buffer,
- sessionId, uid, flags, true /*isOut*/,
+ sessionId, uid, true /*isOut*/,
(type == TYPE_PATCH) ? ( buffer == NULL ? ALLOC_LOCAL : ALLOC_NONE) : ALLOC_CBLK,
type),
mFillingUpStatus(FS_INVALID),
mIsInvalid(false),
mAudioTrackServerProxy(NULL),
mResumeToStopping(false),
- mFlushHwPending(false)
+ mFlushHwPending(false),
+ mFlags(flags)
{
// client == 0 implies sharedBuffer == 0
ALOG_ASSERT(!(client == 0 && sharedBuffer != 0));
return;
}
// only allocate a fast track index if we were able to allocate a normal track name
- if (flags & IAudioFlinger::TRACK_FAST) {
+ if (flags & AUDIO_OUTPUT_FLAG_FAST) {
// FIXME: Not calling framesReadyIsCalledByMultipleThreads() exposes a potential
// race with setSyncEvent(). However, if we call it, we cannot properly start
// static fast tracks (SoundPool) immediately after stopping.
int uid)
: Track(playbackThread, NULL, AUDIO_STREAM_PATCH,
sampleRate, format, channelMask, frameCount,
- NULL, 0, AUDIO_SESSION_NONE, uid, IAudioFlinger::TRACK_DEFAULT,
+ NULL, 0, AUDIO_SESSION_NONE, uid, AUDIO_OUTPUT_FLAG_NONE,
TYPE_OUTPUT),
mActive(false), mSourceThread(sourceThread), mClientProxy(NULL)
{
audio_format_t format,
size_t frameCount,
void *buffer,
- IAudioFlinger::track_flags_t flags)
+ audio_output_flags_t flags)
: Track(playbackThread, NULL, streamType,
sampleRate, format, channelMask, frameCount,
buffer, 0, AUDIO_SESSION_NONE, getuid(), flags, TYPE_PATCH),
void *buffer,
audio_session_t sessionId,
int uid,
- IAudioFlinger::track_flags_t flags,
+ audio_input_flags_t flags,
track_type type)
: TrackBase(thread, client, sampleRate, format,
- channelMask, frameCount, buffer, sessionId, uid,
- flags, false /*isOut*/,
+ channelMask, frameCount, buffer, sessionId, uid, false /*isOut*/,
(type == TYPE_DEFAULT) ?
- ((flags & IAudioFlinger::TRACK_FAST) ? ALLOC_PIPE : ALLOC_CBLK) :
+ ((flags & AUDIO_INPUT_FLAG_FAST) ? ALLOC_PIPE : ALLOC_CBLK) :
((buffer == NULL) ? ALLOC_LOCAL : ALLOC_NONE),
type),
mOverflow(false),
mFramesToDrop(0),
mResamplerBufferProvider(NULL), // initialize in case of early constructor exit
- mRecordBufferConverter(NULL)
+ mRecordBufferConverter(NULL),
+ mFlags(flags)
{
if (mCblk == NULL) {
return;
mResamplerBufferProvider = new ResamplerBufferProvider(this);
- if (flags & IAudioFlinger::TRACK_FAST) {
+ if (flags & AUDIO_INPUT_FLAG_FAST) {
ALOG_ASSERT(thread->mFastTrackAvail);
thread->mFastTrackAvail = false;
}
audio_format_t format,
size_t frameCount,
void *buffer,
- IAudioFlinger::track_flags_t flags)
+ audio_input_flags_t flags)
: RecordTrack(recordThread, NULL, sampleRate, format, channelMask, frameCount,
buffer, AUDIO_SESSION_NONE, getuid(), flags, TYPE_PATCH),
mProxy(new ClientProxy(mCblk, mBuffer, frameCount, mFrameSize, false, true))
LOCAL_MODULE:= libaudiopolicyservice
LOCAL_CFLAGS += -fvisibility=hidden
+LOCAL_CFLAGS += -Wall -Werror
include $(BUILD_SHARED_LIBRARY)
LOCAL_CFLAGS += -DUSE_XML_AUDIO_POLICY_CONF
endif #ifeq ($(USE_XML_AUDIO_POLICY_CONF), 1)
+LOCAL_CFLAGS += -Wall -Werror
+
LOCAL_MULTILIB := $(AUDIOSERVER_MULTILIB)
LOCAL_MODULE:= libaudiopolicymanagerdefault
$(TOPDIR)frameworks/av/services/audiopolicy/common/include \
$(TOPDIR)frameworks/av/services/audiopolicy/engine/interface
+LOCAL_CFLAGS := -Wall -Werror
+
LOCAL_MULTILIB := $(AUDIOSERVER_MULTILIB)
LOCAL_MODULE:= libaudiopolicymanager
LOCAL_MULTILIB := $(AUDIOSERVER_MULTILIB)
+LOCAL_CFLAGS := -Wall -Werror
+
LOCAL_MODULE := libaudiopolicycomponents
include $(BUILD_STATIC_LIBRARY)
typedef TypeConverter<StreamTraits> StreamTypeConverter;
typedef TypeConverter<DeviceCategoryTraits> DeviceCategoryConverter;
+inline
static SampleRateTraits::Collection samplingRatesFromString(const std::string &samplingRates,
const char *del = "|")
{
return samplingRateCollection;
}
+inline
static FormatTraits::Collection formatsFromString(const std::string &formats, const char *del = "|")
{
FormatTraits::Collection formatCollection;
return formatCollection;
}
+inline
static audio_format_t formatFromString(const std::string &literalFormat)
{
audio_format_t format;
return format;
}
+inline
static audio_channel_mask_t channelMaskFromString(const std::string &literalChannels)
{
audio_channel_mask_t channels;
return channels;
}
+inline
static ChannelTraits::Collection channelMasksFromString(const std::string &channels,
const char *del = "|")
{
return channelMaskCollection;
}
+inline
static InputChannelTraits::Collection inputChannelMasksFromString(const std::string &inChannels,
const char *del = "|")
{
return inputChannelMaskCollection;
}
+inline
static OutputChannelTraits::Collection outputChannelMasksFromString(const std::string &outChannels,
const char *del = "|")
{
ssize_t DeviceVector::remove(const sp<DeviceDescriptor>& item)
{
- size_t i;
ssize_t ret = indexOf(item);
if (ret < 0) {
MAKE_STRING_FROM_ENUM(AUDIO_FORMAT_DTS),
MAKE_STRING_FROM_ENUM(AUDIO_FORMAT_DTS_HD),
MAKE_STRING_FROM_ENUM(AUDIO_FORMAT_IEC61937),
+ MAKE_STRING_FROM_ENUM(AUDIO_FORMAT_DOLBY_TRUEHD),
};
template<>
const size_t FormatConverter::mSize = sizeof(FormatConverter::mTable) /
// Where would this volume index been inserted in the curve point
size_t indexInUiPosition = mCurvePoints.orderOf(CurvePoint(volIdx, 0));
if (indexInUiPosition >= nbCurvePoints) {
- return 0.0f; // out of bounds
+ //use last point of table
+ return mCurvePoints[nbCurvePoints - 1].mAttenuationInMb / 100.0f;
}
if (indexInUiPosition == 0) {
if (indexInUiPosition != mCurvePoints[0].mIndex) {
namespace android {
+//FIXME: workaround for truncated touch sounds
+// to be removed when the problem is handled by system UI
+#define TOUCH_SOUND_FIXED_DELAY_MS 100
// ----------------------------------------------------------------------------
// AudioPolicyInterface implementation
// ----------------------------------------------------------------------------
}
closeAllInputs();
+ // As the input device list can impact the output device selection, update
+ // getDeviceForStrategy() cache
+ updateDevicesAndOutputs();
if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
AUDIO_POLICY_DEVICE_STATE_AVAILABLE : AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
}
-void AudioPolicyManager::updateCallRouting(audio_devices_t rxDevice, int delayMs)
+uint32_t AudioPolicyManager::updateCallRouting(audio_devices_t rxDevice, uint32_t delayMs)
{
bool createTxPatch = false;
status_t status;
audio_patch_handle_t afPatchHandle;
DeviceVector deviceList;
+ uint32_t muteWaitMs = 0;
if(!hasPrimaryOutput()) {
- return;
+ return muteWaitMs;
}
audio_devices_t txDevice = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
ALOGV("updateCallRouting device rxDevice %08x txDevice %08x", rxDevice, txDevice);
// via setOutputDevice() on primary output.
// Otherwise, create two audio patches for TX and RX path.
if (availablePrimaryOutputDevices() & rxDevice) {
- setOutputDevice(mPrimaryOutput, rxDevice, true, delayMs);
+ muteWaitMs = setOutputDevice(mPrimaryOutput, rxDevice, true, delayMs);
// If the TX device is also on the primary HW module, setOutputDevice() will take care
// of it due to legacy implementation. If not, create a patch.
if ((availablePrimaryInputDevices() & txDevice & ~AUDIO_DEVICE_BIT_IN)
}
afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
- status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0);
+ status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, delayMs);
ALOGW_IF(status != NO_ERROR, "updateCallRouting() error %d creating RX audio patch",
status);
if (status == NO_ERROR) {
}
afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
- status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0);
+ status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, delayMs);
ALOGW_IF(status != NO_ERROR, "setPhoneState() error %d creating TX audio patch",
status);
if (status == NO_ERROR) {
mCallTxPatch->mUid = mUidCached;
}
}
+
+ return muteWaitMs;
}
void AudioPolicyManager::setPhoneState(audio_mode_t state)
checkOutputForAllStrategies();
updateDevicesAndOutputs();
+ //FIXME: workaround for truncated touch sounds
+ // to be removed when the problem is handled by system UI
+ uint32_t delayMs = 0;
+ uint32_t waitMs = 0;
+ if (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) {
+ delayMs = TOUCH_SOUND_FIXED_DELAY_MS;
+ }
if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
- updateCallRouting(newDevice);
+ waitMs = updateCallRouting(newDevice, delayMs);
}
for (size_t i = 0; i < mOutputs.size(); i++) {
sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/);
if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) {
- setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE));
+ waitMs = setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE),
+ delayMs);
}
if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
- applyStreamVolumes(outputDesc, newDevice, 0, true);
+ applyStreamVolumes(outputDesc, newDevice, waitMs, true);
}
}
const audio_offload_info_t *offloadInfo)
{
audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
- uint32_t latency = 0;
status_t status;
#ifdef AUDIO_POLICY_TEST
beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
}
+ // force device change if the output is inactive and no audio patch is already present.
// check active before incrementing usage count
- bool force = !outputDesc->isActive();
+ bool force = !outputDesc->isActive() &&
+ (outputDesc->getPatchHandle() == AUDIO_PATCH_HANDLE_NONE);
// increment usage count for this stream on the requested output:
// NOTE that the usage count is the same for duplicated output and hardware output which is
for (size_t i = 0; i < mOutputs.size(); i++) {
sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
if (desc != outputDesc) {
- // force a device change if any other output is managed by the same hw
- // module and has a current device selection that differs from selected device.
+ // force a device change if any other output is:
+ // - managed by the same hw module
+ // - has a current device selection that differs from selected device.
+ // - supports currently selected device
+ // - has an active audio patch
// In this case, the audio HAL must receive the new device selection so that it can
// change the device currently selected by the other active output.
if (outputDesc->sharesHwModuleWith(desc) &&
- desc->device() != device) {
+ desc->device() != device &&
+ desc->supportedDevices() & device &&
+ desc->getPatchHandle() != AUDIO_PATCH_HANDLE_NONE) {
force = true;
}
// wait for audio on other active outputs to be presented when starting
if (strategy == STRATEGY_SONIFICATION) {
mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
}
+
+ if (waitMs > muteWaitMs) {
+ *delayMs = waitMs - muteWaitMs;
+ }
}
+
return NO_ERROR;
}
// force restoring the device selection on other active outputs if it differs from the
// one being selected for this output
for (size_t i = 0; i < mOutputs.size(); i++) {
- audio_io_handle_t curOutput = mOutputs.keyAt(i);
sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
if (desc != outputDesc &&
desc->isActive() &&
profileFlags);
if (profile != 0) {
break; // success
+ } else if (profileFlags & AUDIO_INPUT_FLAG_RAW) {
+ profileFlags = (audio_input_flags_t) (profileFlags & ~AUDIO_INPUT_FLAG_RAW); // retry
} else if (profileFlags != AUDIO_INPUT_FLAG_NONE) {
profileFlags = AUDIO_INPUT_FLAG_NONE; // retry
} else { // fail
ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
if (patch_index >= 0) {
sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
- status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
+ (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
mAudioPatches.removeItemsAt(patch_index);
patchRemoved = true;
}
}
if (applyDefault || ((curDevice & curStreamDevice) != 0)) {
+ //FIXME: workaround for truncated touch sounds
+ // delayed volume change for system stream to be removed when the problem is
+ // handled by system UI
status_t volStatus =
- checkAndSetVolume((audio_stream_type_t)curStream, index, desc, curDevice);
+ checkAndSetVolume((audio_stream_type_t)curStream, index, desc, curDevice,
+ (stream == AUDIO_STREAM_SYSTEM) ? TOUCH_SOUND_FIXED_DELAY_MS : 0);
if (volStatus != NO_ERROR) {
status = volStatus;
}
true,
NULL);
} else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
- audio_patch_handle_t afPatchHandle = patchDesc->mAfPatchHandle;
status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d",
status, patchDesc->mAfPatchHandle);
}
mEngine->setObserver(this);
status_t status = mEngine->initCheck();
+ (void) status;
ALOG_ASSERT(status == NO_ERROR, "Policy engine not initialized(err=%d)", status);
// mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices
ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle());
if (index >= 0) {
sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
- status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
+ (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
mAudioPatches.removeItemsAt(index);
mpClientInterface->onAudioPatchListUpdate();
}
ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle());
if (index >= 0) {
sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
- status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
+ (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
mAudioPatches.removeItemsAt(index);
mpClientInterface->onAudioPatchListUpdate();
}
}
routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream);
audio_devices_t curDevices =
- getDeviceForStrategy((routing_strategy)curStrategy, true /*fromCache*/);
+ getDeviceForStrategy((routing_strategy)curStrategy, false /*fromCache*/);
SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(curDevices, mOutputs);
for (size_t i = 0; i < outputs.size(); i++) {
sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
// temporary mute output if device selection changes to avoid volume bursts due to
// different per device volumes
if (outputDesc->isActive() && (device != prevDevice)) {
- if (muteWaitMs < outputDesc->latency() * 2) {
- muteWaitMs = outputDesc->latency() * 2;
+ uint32_t tempMuteWaitMs = outputDesc->latency() * 2;
+ // temporary mute duration is conservatively set to 4 times the reported latency
+ uint32_t tempMuteDurationMs = outputDesc->latency() * 4;
+ if (muteWaitMs < tempMuteWaitMs) {
+ muteWaitMs = tempMuteWaitMs;
}
+
for (size_t i = 0; i < NUM_STRATEGIES; i++) {
if (isStrategyActive(outputDesc, (routing_strategy)i)) {
- setStrategyMute((routing_strategy)i, true, outputDesc);
- // do tempMute unmute after twice the mute wait time
+ // make sure that we do not start the temporary mute period too early in case of
+ // delayed device change
+ setStrategyMute((routing_strategy)i, true, outputDesc, delayMs);
setStrategyMute((routing_strategy)i, false, outputDesc,
- muteWaitMs *2, device);
+ delayMs + tempMuteDurationMs, device);
}
}
}
AudioProfileVector &profiles)
{
String8 reply;
- char *value;
// Format MUST be checked first to update the list of AudioProfile
if (profiles.hasDynamicFormat()) {
return mAvailableInputDevices.getDevicesFromHwModule(mPrimaryOutput->getModuleHandle());
}
- void updateCallRouting(audio_devices_t rxDevice, int delayMs = 0);
+ uint32_t updateCallRouting(audio_devices_t rxDevice, uint32_t delayMs = 0);
// if argument "device" is different from AUDIO_DEVICE_NONE, startSource() will force
// the re-evaluation of the output device.
sp<AudioPolicyEffects>audioPolicyEffects;
{
Mutex::Autolock _l(mLock);
+ mAudioPolicyManager->releaseInput(input, session);
audioPolicyEffects = mAudioPolicyEffects;
}
if (audioPolicyEffects != 0) {
ALOGW("Failed to release effects on input %d", input);
}
}
- {
- Mutex::Autolock _l(mLock);
- mAudioPolicyManager->releaseInput(input, session);
- }
}
status_t AudioPolicyService::initStreamVolume(audio_stream_type_t stream,
static const nsecs_t kAudioCommandTimeoutNs = seconds(3); // 3 seconds
+#ifdef USE_LEGACY_AUDIO_POLICY
namespace {
extern struct audio_policy_service_ops aps_ops;
};
+#endif
// ----------------------------------------------------------------------------
void AudioPolicyService::onFirstRef()
{
- char value[PROPERTY_VALUE_MAX];
- const struct hw_module_t *module;
- int forced_val;
- int rc;
-
{
Mutex::Autolock _l(mLock);
ALOGI("AudioPolicyService CSTOR in legacy mode");
/* instantiate the audio policy manager */
- rc = hw_get_module(AUDIO_POLICY_HARDWARE_MODULE_ID, &module);
+ const struct hw_module_t *module;
+ int rc = hw_get_module(AUDIO_POLICY_HARDWARE_MODULE_ID, &module);
if (rc) {
return;
}
delayMs = 1;
} break;
+ case SET_VOICE_VOLUME: {
+ VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
+ VoiceVolumeData *data2 = (VoiceVolumeData *)command2->mParam.get();
+ ALOGV("Filtering out voice volume command value %f replaced by %f",
+ data2->mVolume, data->mVolume);
+ removedCommands.add(command2);
+ command->mTime = command2->mTime;
+ // force delayMs to non 0 so that code below does not request to wait for
+ // command status as the command is now delayed
+ delayMs = 1;
+ } break;
+
case CREATE_AUDIO_PATCH:
case RELEASE_AUDIO_PATCH: {
audio_patch_handle_t handle;
int aps_set_voice_volume(void *service, float volume, int delay_ms);
};
+#ifdef USE_LEGACY_AUDIO_POLICY
namespace {
struct audio_policy_service_ops aps_ops = {
.open_output = aps_open_output,
.open_input_on_module = aps_open_input_on_module,
};
}; // namespace <unnamed>
+#endif
}; // namespace android
device3/Camera3BufferManager.cpp \
gui/RingBufferConsumer.cpp \
utils/CameraTraces.cpp \
- utils/AutoConditionLock.cpp
+ utils/AutoConditionLock.cpp \
+ utils/TagMonitor.cpp
LOCAL_SHARED_LIBRARIES:= \
libui \
// Can camera service trust the caller based on the calling UID?
static bool isTrustedCallingUid(uid_t uid) {
switch (uid) {
- case AID_MEDIA: // mediaserver
+ case AID_MEDIA: // mediaserver
case AID_CAMERASERVER: // cameraserver
+ case AID_RADIO: // telephony
return true;
default:
return false;
}
finishCameraOps();
+ // Notify flashlight that a camera device is closed.
+ mCameraService->mFlashlight->deviceClosed(String8::format("%d", mCameraId));
ALOGI("%s: Disconnected client for camera %d for PID %d", __FUNCTION__, mCameraId, mClientPid);
// client shouldn't be able to call into us anymore
// Transition device state to CLOSED
mCameraService->updateProxyDeviceState(ICameraServiceProxy::CAMERA_STATE_CLOSED,
String8::format("%d", mCameraId));
-
- // Notify flashlight that a camera device is closed.
- mCameraService->mFlashlight->deviceClosed(
- String8::format("%d", mCameraId));
}
// Always stop watching, even if no camera op is active
if (mOpsCallback != NULL) {
p.fastInfo.bestStillCaptureFpsRange[0],
p.fastInfo.bestStillCaptureFpsRange[1]);
+ result.appendFormat(" Use zero shutter lag: %s\n",
+ p.useZeroShutterLag() ? "yes" : "no");
+
result.append(" Current streams:\n");
result.appendFormat(" Preview stream ID: %d\n",
getPreviewStreamId());
}
}
- if (params.zslMode && !params.recordingHint &&
+ if (params.useZeroShutterLag() &&
getRecordingStreamId() == NO_STREAM) {
res = updateProcessorStream(mZslProcessor, params);
if (res != OK) {
return OK;
}
- if (l.mParameters.zslMode) {
+ if (l.mParameters.allowZslMode) {
mZslProcessor->clearZslQueue();
}
}
// Clear ZSL buffer queue when Jpeg size is changed.
bool jpegStreamChanged = mJpegProcessor->getStreamId() != lastJpegStreamId;
- if (l.mParameters.zslMode && jpegStreamChanged) {
+ if (l.mParameters.allowZslMode && jpegStreamChanged) {
ALOGV("%s: Camera %d: Clear ZSL buffer queue when Jpeg size is changed",
__FUNCTION__, mCameraId);
mZslProcessor->clearZslQueue();
if (res != OK) return res;
Parameters::focusMode_t focusModeAfter = l.mParameters.focusMode;
- if (l.mParameters.zslMode && focusModeAfter != focusModeBefore) {
+ if (l.mParameters.allowZslMode && focusModeAfter != focusModeBefore) {
mZslProcessor->clearZslQueue();
}
return DONE;
}
- else if (l.mParameters.zslMode &&
+ else if (l.mParameters.useZeroShutterLag() &&
l.mParameters.state == Parameters::STILL_CAPTURE &&
l.mParameters.flashMode != Parameters::FLASH_MODE_ON) {
nextState = ZSL_START;
}
void JpegProcessor::onBufferReleased(const BufferInfo& bufferInfo) {
- Mutex::Autolock l(mInputMutex);
ALOGV("%s", __FUNCTION__);
-
if (bufferInfo.mError) {
+ // Only lock in case of error, since we get one of these for each
+ // onFrameAvailable as well, and scheduling may delay this call late
+ // enough to run into later preview restart operations, for non-error
+ // cases.
+ // b/29524651
+ ALOGV("%s: JPEG buffer lost", __FUNCTION__);
+ Mutex::Autolock l(mInputMutex);
mCaptureDone = true;
mCaptureSuccess = false;
mCaptureDoneSignal.signal();
property_get("camera.disable_zsl_mode", value, "0");
if (!strcmp(value,"1") || slowJpegMode) {
ALOGI("Camera %d: Disabling ZSL mode", cameraId);
- zslMode = false;
+ allowZslMode = false;
} else {
- zslMode = true;
+ allowZslMode = true;
}
- ALOGI("%s: zslMode: %d slowJpegMode %d", __FUNCTION__, zslMode, slowJpegMode);
+ ALOGI("%s: allowZslMode: %d slowJpegMode %d", __FUNCTION__, allowZslMode, slowJpegMode);
state = STOPPED;
ALOGV("Camera %d: Flexible YUV %s supported",
cameraId, fastInfo.useFlexibleYuv ? "is" : "is not");
+ fastInfo.maxJpegSize = getMaxSize(getAvailableJpegSizes());
+
return OK;
}
return pictureSizeOverriden;
}
+bool Parameters::useZeroShutterLag() const {
+ // If ZSL mode is disabled, don't use it
+ if (!allowZslMode) return false;
+ // If recording hint is enabled, don't do ZSL
+ if (recordingHint) return false;
+ // If still capture size is no bigger than preview or video size,
+ // don't do ZSL
+ if (pictureWidth <= previewWidth || pictureHeight <= previewHeight ||
+ pictureWidth <= videoWidth || pictureHeight <= videoHeight) {
+ return false;
+ }
+ // If still capture size is less than quarter of max, don't do ZSL
+ if ((pictureWidth * pictureHeight) <
+ (fastInfo.maxJpegSize.width * fastInfo.maxJpegSize.height / 4) ) {
+ return false;
+ }
+ return true;
+}
+
const char* Parameters::getStateName(State state) {
#define CASE_ENUM_TO_CHAR(x) case x: return(#x); break;
switch(state) {
bool previewCallbackOneShot;
bool previewCallbackSurface;
- bool zslMode;
+ bool allowZslMode;
// Whether the jpeg stream is slower than 30FPS and can slow down preview.
- // When slowJpegMode is true, zslMode must be false to avoid slowing down preview.
+ // When slowJpegMode is true, allowZslMode must be false to avoid slowing down preview.
bool slowJpegMode;
// Overall camera state
DefaultKeyedVector<uint8_t, OverrideModes> sceneModeOverrides;
float minFocalLength;
bool useFlexibleYuv;
+ Size maxJpegSize;
} fastInfo;
// Quirks information; these are short-lived flags to enable workarounds for
status_t recoverOverriddenJpegSize();
// if video snapshot size is currently overridden
bool isJpegSizeOverridden();
+ // whether zero shutter lag should be used for non-recording operation
+ bool useZeroShutterLag() const;
// Calculate the crop region rectangle, either tightly about the preview
// resolution, or a region just based on the active array; both take
// Use CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG for ZSL streaming case.
if (client->getCameraDeviceVersion() >= CAMERA_DEVICE_API_VERSION_3_0) {
- if (params.zslMode && !params.recordingHint) {
+ if (params.useZeroShutterLag() && !params.recordingHint) {
res = device->createDefaultRequest(CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG,
&mPreviewRequest);
} else {
status_t err = mDevice->configureStreams(isConstrainedHighSpeed);
if (err == BAD_VALUE) {
- res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
- "Camera %d: Unsupported set of inputs/outputs provided",
+ String8 msg = String8::format("Camera %d: Unsupported set of inputs/outputs provided",
mCameraId);
+ ALOGE("%s: %s", __FUNCTION__, msg.string());
+ res = STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
} else if (err != OK) {
- res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
- "Camera %d: Error configuring streams: %s (%d)",
+ String8 msg = String8::format("Camera %d: Error configuring streams: %s (%d)",
mCameraId, strerror(-err), err);
+ ALOGE("%s: %s", __FUNCTION__, msg.string());
+ res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
}
return res;
bool isInput = false;
ssize_t index = NAME_NOT_FOUND;
+ ssize_t dIndex = NAME_NOT_FOUND;
if (mInputStream.configured && mInputStream.id == streamId) {
isInput = true;
}
if (index == NAME_NOT_FOUND) {
- String8 msg = String8::format("Camera %d: Invalid stream ID (%d) specified, no such "
- "stream created yet", mCameraId, streamId);
- ALOGW("%s: %s", __FUNCTION__, msg.string());
- return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
+ // See if this stream is one of the deferred streams.
+ for (size_t i = 0; i < mDeferredStreams.size(); ++i) {
+ if (streamId == mDeferredStreams[i]) {
+ dIndex = i;
+ break;
+ }
+ }
+ if (dIndex == NAME_NOT_FOUND) {
+ String8 msg = String8::format("Camera %d: Invalid stream ID (%d) specified, no such"
+ " stream created yet", mCameraId, streamId);
+ ALOGW("%s: %s", __FUNCTION__, msg.string());
+ return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
+ }
}
}
} else {
if (isInput) {
mInputStream.configured = false;
- } else {
+ } else if (index != NAME_NOT_FOUND) {
mStreamMap.removeItemsAt(index);
+ } else {
+ mDeferredStreams.removeItemsAt(dIndex);
}
}
Mutex::Autolock icl(mBinderSerializationLock);
sp<IGraphicBufferProducer> bufferProducer = outputConfiguration.getGraphicBufferProducer();
- if (bufferProducer == NULL) {
- ALOGE("%s: bufferProducer must not be null", __FUNCTION__);
+ bool deferredConsumer = bufferProducer == NULL;
+ int surfaceType = outputConfiguration.getSurfaceType();
+ bool validSurfaceType = ((surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_VIEW) ||
+ (surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_TEXTURE));
+ if (deferredConsumer && !validSurfaceType) {
+ ALOGE("%s: Target surface is invalid: bufferProducer = %p, surfaceType = %d.",
+ __FUNCTION__, bufferProducer.get(), surfaceType);
return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Target Surface is invalid");
}
+
if (!mDevice.get()) {
return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
}
+ int width, height, format;
+ int32_t consumerUsage;
+ android_dataspace dataSpace;
+ status_t err;
+
+ // Create stream for deferred surface case.
+ if (deferredConsumer) {
+ return createDeferredSurfaceStreamLocked(outputConfiguration, newStreamId);
+ }
+
// Don't create multiple streams for the same target surface
{
ssize_t index = mStreamMap.indexOfKey(IInterface::asBinder(bufferProducer));
}
}
- status_t err;
-
// HACK b/10949105
// Query consumer usage bits to set async operation mode for
// GLConsumer using controlledByApp parameter.
bool useAsync = false;
- int32_t consumerUsage;
if ((err = bufferProducer->query(NATIVE_WINDOW_CONSUMER_USAGE_BITS,
&consumerUsage)) != OK) {
String8 msg = String8::format("Camera %d: Failed to query Surface consumer usage: %s (%d)",
return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
}
if (consumerUsage & GraphicBuffer::USAGE_HW_TEXTURE) {
- ALOGW("%s: Camera %d: Forcing asynchronous mode for stream",
- __FUNCTION__, mCameraId);
+ ALOGW("%s: Camera %d with consumer usage flag: 0x%x: Forcing asynchronous mode for stream",
+ __FUNCTION__, mCameraId, consumerUsage);
useAsync = true;
}
sp<Surface> surface = new Surface(bufferProducer, useAsync);
ANativeWindow *anw = surface.get();
- int width, height, format;
- android_dataspace dataSpace;
-
if ((err = anw->query(anw, NATIVE_WINDOW_WIDTH, &width)) != OK) {
String8 msg = String8::format("Camera %d: Failed to query Surface width: %s (%d)",
mCameraId, strerror(-err), err);
} else {
mStreamMap.add(binder, streamId);
- ALOGV("%s: Camera %d: Successfully created a new stream ID %d",
- __FUNCTION__, mCameraId, streamId);
+ ALOGV("%s: Camera %d: Successfully created a new stream ID %d for output surface"
+ " (%d x %d) with format 0x%x.",
+ __FUNCTION__, mCameraId, streamId, width, height, format);
- /**
- * Set the stream transform flags to automatically
- * rotate the camera stream for preview use cases.
- */
- int32_t transform = 0;
- err = getRotationTransformLocked(&transform);
+ // Set transform flags to ensure preview to be rotated correctly.
+ res = setStreamTransformLocked(streamId);
- if (err != OK) {
- // Error logged by getRotationTransformLocked.
- return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
- "Unable to calculate rotation transform for new stream");
- }
+ *newStreamId = streamId;
+ }
- err = mDevice->setStreamTransform(streamId, transform);
- if (err != OK) {
- String8 msg = String8::format("Failed to set stream transform (stream id %d)",
- streamId);
- ALOGE("%s: %s", __FUNCTION__, msg.string());
- return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
- }
+ return res;
+}
- *newStreamId = streamId;
+binder::Status CameraDeviceClient::createDeferredSurfaceStreamLocked(
+ const hardware::camera2::params::OutputConfiguration &outputConfiguration,
+ /*out*/
+ int* newStreamId) {
+ int width, height, format, surfaceType;
+ int32_t consumerUsage;
+ android_dataspace dataSpace;
+ status_t err;
+ binder::Status res;
+
+ if (!mDevice.get()) {
+ return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
}
+ // Infer the surface info for deferred surface stream creation.
+ width = outputConfiguration.getWidth();
+ height = outputConfiguration.getHeight();
+ surfaceType = outputConfiguration.getSurfaceType();
+ format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
+ dataSpace = android_dataspace_t::HAL_DATASPACE_UNKNOWN;
+ // Hardcode consumer usage flags: SurfaceView--0x900, SurfaceTexture--0x100.
+ consumerUsage = GraphicBuffer::USAGE_HW_TEXTURE;
+ if (surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_VIEW) {
+ consumerUsage |= GraphicBuffer::USAGE_HW_COMPOSER;
+ }
+ int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
+ err = mDevice->createStream(/*surface*/nullptr, width, height, format, dataSpace,
+ static_cast<camera3_stream_rotation_t>(outputConfiguration.getRotation()),
+ &streamId, outputConfiguration.getSurfaceSetID(), consumerUsage);
+
+ if (err != OK) {
+ res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
+ "Camera %d: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
+ mCameraId, width, height, format, dataSpace, strerror(-err), err);
+ } else {
+ // Can not add streamId to mStreamMap here, as the surface is deferred. Add it to
+ // a separate list to track. Once the deferred surface is set, this id will be
+ // relocated to mStreamMap.
+ mDeferredStreams.push_back(streamId);
+
+ ALOGV("%s: Camera %d: Successfully created a new stream ID %d for a deferred surface"
+ " (%d x %d) stream with format 0x%x.",
+ __FUNCTION__, mCameraId, streamId, width, height, format);
+
+ // Set transform flags to ensure preview to be rotated correctly.
+ res = setStreamTransformLocked(streamId);
+
+ *newStreamId = streamId;
+ }
return res;
}
+binder::Status CameraDeviceClient::setStreamTransformLocked(int streamId) {
+ int32_t transform = 0;
+ status_t err;
+ binder::Status res;
+
+ if (!mDevice.get()) {
+ return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
+ }
+
+ err = getRotationTransformLocked(&transform);
+ if (err != OK) {
+ // Error logged by getRotationTransformLocked.
+ return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
+ "Unable to calculate rotation transform for new stream");
+ }
+
+ err = mDevice->setStreamTransform(streamId, transform);
+ if (err != OK) {
+ String8 msg = String8::format("Failed to set stream transform (stream id %d)",
+ streamId);
+ ALOGE("%s: %s", __FUNCTION__, msg.string());
+ return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
+ }
+
+ return res;
+}
binder::Status CameraDeviceClient::createInputStream(
int width, int height, int format,
return res;
}
+binder::Status CameraDeviceClient::setDeferredConfiguration(int32_t streamId,
+ const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
+ ATRACE_CALL();
+
+ binder::Status res;
+ if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
+
+ Mutex::Autolock icl(mBinderSerializationLock);
+
+ sp<IGraphicBufferProducer> bufferProducer = outputConfiguration.getGraphicBufferProducer();
+
+ // Client code should guarantee that the surface is from SurfaceView or SurfaceTexture.
+ if (bufferProducer == NULL) {
+ ALOGE("%s: bufferProducer must not be null", __FUNCTION__);
+ return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Target Surface is invalid");
+ }
+ // Check if this stram id is one of the deferred streams
+ ssize_t index = NAME_NOT_FOUND;
+ for (size_t i = 0; i < mDeferredStreams.size(); i++) {
+ if (streamId == mDeferredStreams[i]) {
+ index = i;
+ break;
+ }
+ }
+ if (index == NAME_NOT_FOUND) {
+ String8 msg = String8::format("Camera %d: deferred surface is set to a unknown stream"
+ "(ID %d)", mCameraId, streamId);
+ ALOGW("%s: %s", __FUNCTION__, msg.string());
+ return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
+ }
+
+ if (!mDevice.get()) {
+ return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
+ }
+
+ // Don't create multiple streams for the same target surface
+ {
+ ssize_t index = mStreamMap.indexOfKey(IInterface::asBinder(bufferProducer));
+ if (index != NAME_NOT_FOUND) {
+ String8 msg = String8::format("Camera %d: Surface already has a stream created "
+ " for it (ID %zd)", mCameraId, index);
+ ALOGW("%s: %s", __FUNCTION__, msg.string());
+ return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
+ }
+ }
+
+ status_t err;
+
+ // Always set to async, as we know the deferred surface is for preview streaming.
+ sp<Surface> consumerSurface = new Surface(bufferProducer, /*useAsync*/true);
+
+ // Finish the deferred stream configuration with the surface.
+ err = mDevice->setConsumerSurface(streamId, consumerSurface);
+ if (err == OK) {
+ sp<IBinder> binder = IInterface::asBinder(bufferProducer);
+ mStreamMap.add(binder, streamId);
+ mDeferredStreams.removeItemsAt(index);
+ } else if (err == NO_INIT) {
+ res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
+ "Camera %d: Deferred surface is invalid: %s (%d)",
+ mCameraId, strerror(-err), err);
+ } else {
+ res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
+ "Camera %d: Error setting output stream deferred surface: %s (%d)",
+ mCameraId, strerror(-err), err);
+ }
+
+ return res;
+}
+
status_t CameraDeviceClient::dump(int fd, const Vector<String16>& args) {
return BasicClient::dump(fd, args);
}
for (size_t i = 0; i < mStreamMap.size(); i++) {
result.appendFormat(" Stream %d\n", mStreamMap.valueAt(i));
}
+ } else if (!mDeferredStreams.isEmpty()) {
+ result.append(" Current deferred surface output stream IDs:\n");
+ for (auto& streamId : mDeferredStreams) {
+ result.appendFormat(" Stream %d\n", streamId);
+ }
} else {
result.append(" No output streams configured.\n");
}
// Prepare stream by preallocating up to maxCount of its buffers
virtual binder::Status prepare2(int32_t maxCount, int32_t streamId);
+ // Set the deferred surface for a stream.
+ virtual binder::Status setDeferredConfiguration(int32_t streamId,
+ const hardware::camera2::params::OutputConfiguration &outputConfiguration);
+
/**
* Interface used by CameraService
*/
// Find the square of the euclidean distance between two points
static int64_t euclidDistSquare(int32_t x0, int32_t y0, int32_t x1, int32_t y1);
+ // Create an output stream with surface deferred for future.
+ binder::Status createDeferredSurfaceStreamLocked(
+ const hardware::camera2::params::OutputConfiguration &outputConfiguration,
+ int* newStreamId = NULL);
+
+ // Set the stream transform flags to automatically rotate the camera stream for preview use
+ // cases.
+ binder::Status setStreamTransformLocked(int streamId);
+
// Find the closest dimensions for a given format in available stream configurations with
// a width <= ROUNDING_WIDTH_CAP
static const int32_t ROUNDING_WIDTH_CAP = 1920;
int32_t mRequestIdCounter;
+ // The list of output streams whose surfaces are deferred. We have to track them separately
+ // as there are no surfaces available and can not be put into mStreamMap. Once the deferred
+ // Surface is configured, the stream id will be moved to mStreamMap.
+ Vector<int32_t> mDeferredStreams;
};
}; // namespace android
return res;
}
- res = mDevice->setNotifyCallback(this);
+ wp<CameraDeviceBase::NotificationListener> weakThis(this);
+ res = mDevice->setNotifyCallback(weakThis);
return OK;
}
virtual status_t createStream(sp<Surface> consumer,
uint32_t width, uint32_t height, int format,
android_dataspace dataSpace, camera3_stream_rotation_t rotation, int *id,
- int streamSetId = camera3::CAMERA3_STREAM_SET_ID_INVALID) = 0;
+ int streamSetId = camera3::CAMERA3_STREAM_SET_ID_INVALID,
+ uint32_t consumerUsage = 0) = 0;
/**
* Create an input stream of width, height, and format.
/**
* Abstract class for HAL notification listeners
*/
- class NotificationListener {
+ class NotificationListener : public virtual RefBase {
public:
// The set of notifications is a merge of the notifications required for
// API1 and API2.
* Connect HAL notifications to a listener. Overwrites previous
* listener. Set to NULL to stop receiving notifications.
*/
- virtual status_t setNotifyCallback(NotificationListener *listener) = 0;
+ virtual status_t setNotifyCallback(wp<NotificationListener> listener) = 0;
/**
* Whether the device supports calling notifyAutofocus, notifyAutoExposure,
* Get the HAL device version.
*/
virtual uint32_t getDeviceVersion() = 0;
+
+ /**
+ * Set the deferred consumer surface and finish the rest of the stream configuration.
+ */
+ virtual status_t setConsumerSurface(int streamId, sp<Surface> consumer) = 0;
+
};
}; // namespace android
#include <utils/Log.h>
#include <utils/Trace.h>
#include <utils/Timers.h>
+#include <cutils/properties.h>
#include <android/hardware/camera2/ICameraDeviceUser.h>
ATRACE_CALL();
Mutex::Autolock il(mInterfaceLock);
- ALOGV("%s: E", __FUNCTION__);
+ ALOGI("%s: E", __FUNCTION__);
status_t res = OK;
internalUpdateStatusLocked(STATUS_UNINITIALIZED);
}
- ALOGV("%s: X", __FUNCTION__);
+ ALOGI("%s: X", __FUNCTION__);
return res;
}
mId, __FUNCTION__);
bool dumpTemplates = false;
+
String16 templatesOption("-t");
+ String16 monitorOption("-m");
int n = args.size();
for (int i = 0; i < n; i++) {
if (args[i] == templatesOption) {
dumpTemplates = true;
}
+ if (args[i] == monitorOption) {
+ if (i + 1 < n) {
+ String8 monitorTags = String8(args[i + 1]);
+ if (monitorTags == "off") {
+ mTagMonitor.disableMonitoring();
+ } else {
+ mTagMonitor.parseTagsToMonitor(monitorTags);
+ }
+ } else {
+ mTagMonitor.disableMonitoring();
+ }
+ }
}
String8 lines;
}
}
+ mTagMonitor.dumpMonitoredMetadata(fd);
+
if (mHal3Device != NULL) {
lines = String8(" HAL device dump:\n");
write(fd, lines.string(), lines.size());
status_t Camera3Device::createStream(sp<Surface> consumer,
uint32_t width, uint32_t height, int format, android_dataspace dataSpace,
- camera3_stream_rotation_t rotation, int *id, int streamSetId) {
+ camera3_stream_rotation_t rotation, int *id, int streamSetId, uint32_t consumerUsage) {
ATRACE_CALL();
Mutex::Autolock il(mInterfaceLock);
Mutex::Autolock l(mLock);
- ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d",
- mId, mNextStreamId, width, height, format, dataSpace, rotation);
+ ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d"
+ " consumer usage 0x%x", mId, mNextStreamId, width, height, format, dataSpace, rotation,
+ consumerUsage);
status_t res;
bool wasActive = false;
if (mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2) {
streamSetId = CAMERA3_STREAM_SET_ID_INVALID;
}
+
+ // HAL3.1 doesn't support deferred consumer stream creation as it requires buffer registration
+ // which requires a consumer surface to be available.
+ if (consumer == nullptr && mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
+ ALOGE("HAL3.1 doesn't support deferred consumer stream creation");
+ return BAD_VALUE;
+ }
+
+ if (consumer == nullptr && format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
+ ALOGE("Deferred consumer stream creation only support IMPLEMENTATION_DEFINED format");
+ return BAD_VALUE;
+ }
+
// Use legacy dataspace values for older HALs
if (mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_3) {
dataSpace = mapToLegacyDataspace(dataSpace);
newStream = new Camera3OutputStream(mNextStreamId, consumer,
width, height, rawOpaqueBufferSize, format, dataSpace, rotation,
mTimestampOffset, streamSetId);
+ } else if (consumer == nullptr) {
+ newStream = new Camera3OutputStream(mNextStreamId,
+ width, height, format, consumerUsage, dataSpace, rotation,
+ mTimestampOffset, streamSetId);
} else {
newStream = new Camera3OutputStream(mNextStreamId, consumer,
width, height, format, dataSpace, rotation,
}
-status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
+status_t Camera3Device::setNotifyCallback(wp<NotificationListener> listener) {
ATRACE_CALL();
Mutex::Autolock l(mOutputLock);
ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
Mutex::Autolock il(mInterfaceLock);
- NotificationListener* listener;
- {
- Mutex::Autolock l(mOutputLock);
- listener = mListener;
- }
-
{
Mutex::Autolock l(mLock);
- mRequestThread->clear(listener, /*out*/frameNumber);
+ mRequestThread->clear(/*out*/frameNumber);
}
status_t res;
// state changes
if (mPauseStateNotify) return;
}
- NotificationListener *listener;
+
+ sp<NotificationListener> listener;
{
Mutex::Autolock l(mOutputLock);
- listener = mListener;
+ listener = mListener.promote();
}
if (idle && listener != NULL) {
listener->notifyIdle();
}
}
+status_t Camera3Device::setConsumerSurface(int streamId, sp<Surface> consumer) {
+ ATRACE_CALL();
+ ALOGV("%s: Camera %d: set consumer surface for stream %d", __FUNCTION__, mId, streamId);
+ Mutex::Autolock il(mInterfaceLock);
+ Mutex::Autolock l(mLock);
+
+ if (consumer == nullptr) {
+ CLOGE("Null consumer is passed!");
+ return BAD_VALUE;
+ }
+
+ ssize_t idx = mOutputStreams.indexOfKey(streamId);
+ if (idx == NAME_NOT_FOUND) {
+ CLOGE("Stream %d is unknown", streamId);
+ return idx;
+ }
+ sp<Camera3OutputStreamInterface> stream = mOutputStreams[idx];
+ status_t res = stream->setConsumer(consumer);
+ if (res != OK) {
+ CLOGE("Stream %d set consumer failed (error %d %s) ", streamId, res, strerror(-res));
+ return res;
+ }
+
+ if (!stream->isConfiguring()) {
+ CLOGE("Stream %d was already fully configured.", streamId);
+ return INVALID_OPERATION;
+ }
+
+ res = stream->finishConfiguration(mHal3Device);
+ if (res != OK) {
+ SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
+ stream->getId(), strerror(-res), res);
+ return res;
+ }
+
+ return OK;
+}
+
/**
* Camera3Device private methods
*/
sp<Camera3OutputStreamInterface> stream =
mOutputStreams.editValueAt(idx);
+ // It is illegal to include a deferred consumer output stream into a request
+ if (stream->isConsumerConfigurationDeferred()) {
+ CLOGE("Stream %d hasn't finished configuration yet due to deferred consumer",
+ stream->getId());
+ return NULL;
+ }
+
// Lazy completion of stream configuration (allocation/registration)
// on first use
if (stream->isConfiguring()) {
for (size_t i = 0; i < mOutputStreams.size(); i++) {
sp<Camera3OutputStreamInterface> outputStream =
mOutputStreams.editValueAt(i);
- if (outputStream->isConfiguring()) {
+ if (outputStream->isConfiguring() && !outputStream->isConsumerConfigurationDeferred()) {
res = outputStream->finishConfiguration(mHal3Device);
if (res != OK) {
CLOGE("Can't finish configuring output stream %d: %s (%d)",
// across configure_streams() calls
mRequestThread->configurationComplete(mIsConstrainedHighSpeedConfiguration);
- // Boost priority of request thread for high speed recording to SCHED_FIFO
- if (mIsConstrainedHighSpeedConfiguration) {
+ char value[PROPERTY_VALUE_MAX];
+ property_get("camera.fifo.disable", value, "0");
+ int32_t disableFifo = atoi(value);
+ if (disableFifo != 1) {
+ // Boost priority of request thread to SCHED_FIFO.
pid_t requestThreadTid = mRequestThread->getTid();
res = requestPriority(getpid(), requestThreadTid,
- kConstrainedHighSpeedThreadPriority, /*asynchronous*/ false);
+ kRequestThreadPriority, /*asynchronous*/ false);
if (res != OK) {
ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
strerror(-res), res);
} else {
ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
}
- } else {
- // TODO: Set/restore normal priority for normal use cases
}
// Update device state
internalUpdateStatusLocked(STATUS_ERROR);
// Notify upstream about a device error
- if (mListener != NULL) {
- mListener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
+ sp<NotificationListener> listener = mListener.promote();
+ if (listener != NULL) {
+ listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
CaptureResultExtras());
}
captureResult.mMetadata.sort();
// Check that there's a timestamp in the result metadata
- camera_metadata_entry entry = captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
- if (entry.count == 0) {
+ camera_metadata_entry timestamp = captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
+ if (timestamp.count == 0) {
SET_ERR("No timestamp provided by HAL for frame %d!",
frameNumber);
return;
}
+ mTagMonitor.monitorMetadata(TagMonitor::RESULT,
+ frameNumber, timestamp.data.i64[0], captureResult.mMetadata);
+
insertResultLocked(&captureResult, frameNumber, aeTriggerCancelOverride);
}
void Camera3Device::notify(const camera3_notify_msg *msg) {
ATRACE_CALL();
- NotificationListener *listener;
+ sp<NotificationListener> listener;
{
Mutex::Autolock l(mOutputLock);
- listener = mListener;
+ listener = mListener.promote();
}
if (msg == NULL) {
}
void Camera3Device::notifyError(const camera3_error_msg_t &msg,
- NotificationListener *listener) {
+ sp<NotificationListener> listener) {
// Map camera HAL error codes to ICameraDeviceCallback error codes
// Index into this with the HAL error code
}
void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
- NotificationListener *listener) {
+ sp<NotificationListener> listener) {
ssize_t idx;
// Set timestamp for the request in the in-flight tracking
}
+void Camera3Device::monitorMetadata(TagMonitor::eventSource source,
+ int64_t frameNumber, nsecs_t timestamp, const CameraMetadata& metadata) {
+ mTagMonitor.monitorMetadata(source, frameNumber, timestamp, metadata);
+}
+
/**
* RequestThread inner class methods
*/
}
void Camera3Device::RequestThread::setNotificationListener(
- NotificationListener *listener) {
+ wp<NotificationListener> listener) {
Mutex::Autolock l(mRequestLock);
mListener = listener;
}
}
status_t Camera3Device::RequestThread::clear(
- NotificationListener *listener,
/*out*/int64_t *lastFrameNumber) {
Mutex::Autolock l(mRequestLock);
ALOGV("RequestThread::%s:", __FUNCTION__);
// Send errors for all requests pending in the request queue, including
// pending repeating requests
+ sp<NotificationListener> listener = mListener.promote();
if (listener != NULL) {
for (RequestList::iterator it = mRequestQueue.begin();
it != mRequestQueue.end(); ++it) {
void Camera3Device::RequestThread::checkAndStopRepeatingRequest() {
bool surfaceAbandoned = false;
int64_t lastFrameNumber = 0;
+ sp<NotificationListener> listener;
{
Mutex::Autolock l(mRequestLock);
// Check all streams needed by repeating requests are still valid. Otherwise, stop
break;
}
}
+ listener = mListener.promote();
}
- if (surfaceAbandoned) {
- mListener->notifyRepeatingRequestError(lastFrameNumber);
+
+ if (listener != NULL && surfaceAbandoned) {
+ listener->notifyRepeatingRequestError(lastFrameNumber);
}
}
mFlushLock.lock();
}
- ALOGVV("%s: %d: submitting %d requests in a batch.", __FUNCTION__, __LINE__,
+ ALOGVV("%s: %d: submitting %zu requests in a batch.", __FUNCTION__, __LINE__,
mNextRequests.size());
for (auto& nextRequest : mNextRequests) {
// Submit request and block until ready for next one
camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
mLatestRequest.acquire(cloned);
+
+ sp<Camera3Device> parent = mParent.promote();
+ if (parent != NULL) {
+ parent->monitorMetadata(TagMonitor::REQUEST, nextRequest.halRequest.frame_number,
+ 0, mLatestRequest);
+ }
}
if (nextRequest.halRequest.settings != NULL) {
if (sendRequestError) {
Mutex::Autolock l(mRequestLock);
- if (mListener != NULL) {
- mListener->notifyError(
+ sp<NotificationListener> listener = mListener.promote();
+ if (listener != NULL) {
+ listener->notifyError(
hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
captureRequest->mResultExtras);
}
// error
ALOGE("%s: Can't get input buffer, skipping request:"
" %s (%d)", __FUNCTION__, strerror(-res), res);
- if (mListener != NULL) {
- mListener->notifyError(
+
+ sp<NotificationListener> listener = mListener.promote();
+ if (listener != NULL) {
+ listener->notifyError(
hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
nextRequest->mResultExtras);
}
status_t res;
Mutex::Autolock l(mLock);
+ sp<NotificationListener> listener = mListener.promote();
res = stream->startPrepare(maxCount);
if (res == OK) {
// No preparation needed, fire listener right off
ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
- if (mListener) {
- mListener->notifyPrepared(stream->getId());
+ if (listener != NULL) {
+ listener->notifyPrepared(stream->getId());
}
return OK;
} else if (res != NOT_ENOUGH_DATA) {
res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
if (res != OK) {
ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
- if (mListener) {
- mListener->notifyPrepared(stream->getId());
+ if (listener != NULL) {
+ listener->notifyPrepared(stream->getId());
}
return res;
}
return OK;
}
-void Camera3Device::PreparerThread::setNotificationListener(NotificationListener *listener) {
+void Camera3Device::PreparerThread::setNotificationListener(wp<NotificationListener> listener) {
Mutex::Autolock l(mLock);
mListener = listener;
}
// This stream has finished, notify listener
Mutex::Autolock l(mLock);
- if (mListener) {
+ sp<NotificationListener> listener = mListener.promote();
+ if (listener != NULL) {
ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
mCurrentStream->getId());
- mListener->notifyPrepared(mCurrentStream->getId());
+ listener->notifyPrepared(mCurrentStream->getId());
}
ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
#include "common/CameraDeviceBase.h"
#include "device3/StatusTracker.h"
#include "device3/Camera3BufferManager.h"
+#include "utils/TagMonitor.h"
/**
* Function pointer types with C calling convention to
// Actual stream creation/deletion is delayed until first request is submitted
// If adding streams while actively capturing, will pause device before adding
- // stream, reconfiguring device, and unpausing.
+ // stream, reconfiguring device, and unpausing. If the client create a stream
+ // with nullptr consumer surface, the client must then call setConsumer()
+ // and finish the stream configuration before starting output streaming.
virtual status_t createStream(sp<Surface> consumer,
uint32_t width, uint32_t height, int format,
android_dataspace dataSpace, camera3_stream_rotation_t rotation, int *id,
- int streamSetId = camera3::CAMERA3_STREAM_SET_ID_INVALID);
+ int streamSetId = camera3::CAMERA3_STREAM_SET_ID_INVALID,
+ uint32_t consumerUsage = 0);
virtual status_t createInputStream(
uint32_t width, uint32_t height, int format,
int *id);
// Transitions to the idle state on success
virtual status_t waitUntilDrained();
- virtual status_t setNotifyCallback(NotificationListener *listener);
+ virtual status_t setNotifyCallback(wp<NotificationListener> listener);
virtual bool willNotify3A();
virtual status_t waitForNextFrame(nsecs_t timeout);
virtual status_t getNextResult(CaptureResult *frame);
// Methods called by subclasses
void notifyStatus(bool idle); // updates from StatusTracker
+ /**
+ * Set the deferred consumer surface to the output stream and finish the deferred
+ * consumer configuration.
+ */
+ virtual status_t setConsumerSurface(int streamId, sp<Surface> consumer);
+
private:
static const size_t kDumpLockAttempts = 10;
static const size_t kDumpSleepDuration = 100000; // 0.10 sec
static const size_t kInFlightWarnLimit = 20;
static const size_t kInFlightWarnLimitHighSpeed = 256; // batch size 32 * pipe depth 8
// SCHED_FIFO priority for request submission thread in HFR mode
- static const int kConstrainedHighSpeedThreadPriority = 1;
+ static const int kRequestThreadPriority = 1;
struct RequestTrigger;
// minimal jpeg buffer size: 256KB + blob header
camera3_device_t *hal3Device,
bool aeLockAvailable);
- void setNotificationListener(NotificationListener *listener);
+ void setNotificationListener(wp<NotificationListener> listener);
/**
* Call after stream (re)-configuration is completed.
/**
* Remove all queued and repeating requests, and pending triggers
*/
- status_t clear(NotificationListener *listener,
- /*out*/
- int64_t *lastFrameNumber = NULL);
+ status_t clear(/*out*/int64_t *lastFrameNumber = NULL);
/**
* Flush all pending requests in HAL.
wp<camera3::StatusTracker> mStatusTracker;
camera3_device_t *mHal3Device;
- NotificationListener *mListener;
+ wp<NotificationListener> mListener;
const int mId; // The camera ID
int mStatusId; // The RequestThread's component ID for
PreparerThread();
~PreparerThread();
- void setNotificationListener(NotificationListener *listener);
+ void setNotificationListener(wp<NotificationListener> listener);
/**
* Queue up a stream to be prepared. Streams are processed by a background thread in FIFO
// Guarded by mLock
- NotificationListener *mListener;
+ wp<NotificationListener> mListener;
List<sp<camera3::Camera3StreamInterface> > mPendingStreams;
bool mActive;
bool mCancelNow;
uint32_t mNextReprocessShutterFrameNumber;
List<CaptureResult> mResultQueue;
Condition mResultSignal;
- NotificationListener *mListener;
+ wp<NotificationListener> mListener;
/**** End scope for mOutputLock ****/
// Specific notify handlers
void notifyError(const camera3_error_msg_t &msg,
- NotificationListener *listener);
+ sp<NotificationListener> listener);
void notifyShutter(const camera3_shutter_msg_t &msg,
- NotificationListener *listener);
+ sp<NotificationListener> listener);
// helper function to return the output buffers to the streams.
void returnOutputBuffers(const camera3_stream_buffer_t *outputBuffers,
/**** End scope for mInFlightLock ****/
+ // Debug tracker for metadata tag value changes
+ // - Enabled with the -m <taglist> option to dumpsys, such as
+ // dumpsys -m android.control.aeState,android.control.aeMode
+ // - Disabled with -m off
+ // - dumpsys -m 3a is a shortcut for ae/af/awbMode, State, and Triggers
+ TagMonitor mTagMonitor;
+
+ void monitorMetadata(TagMonitor::eventSource source, int64_t frameNumber,
+ nsecs_t timestamp, const CameraMetadata& metadata);
+
/**
* Static callback forwarding methods from HAL to instance
*/
return false;
}
+bool Camera3DummyStream::isConsumerConfigurationDeferred() const {
+ return false;
+}
+
+status_t Camera3DummyStream::setConsumer(sp<Surface> consumer) {
+ ALOGE("%s: Stream %d: Dummy stream doesn't support set consumer surface %p!",
+ __FUNCTION__, mId, consumer.get());
+ return INVALID_OPERATION;
+}
}; // namespace camera3
}; // namespace android
*/
bool isVideoStream() const;
+ /**
+ * Return if the consumer configuration of this stream is deferred.
+ */
+ virtual bool isConsumerConfigurationDeferred() const;
+
+ /**
+ * Set the consumer surface to the output stream.
+ */
+ virtual status_t setConsumer(sp<Surface> consumer);
+
protected:
/**
mTransform(0),
mTraceFirstBuffer(true),
mUseBufferManager(false),
- mTimestampOffset(timestampOffset) {
+ mTimestampOffset(timestampOffset),
+ mConsumerUsage(0) {
if (mConsumer == NULL) {
ALOGE("%s: Consumer is NULL!", __FUNCTION__);
mTraceFirstBuffer(true),
mUseMonoTimestamp(false),
mUseBufferManager(false),
- mTimestampOffset(timestampOffset) {
+ mTimestampOffset(timestampOffset),
+ mConsumerUsage(0) {
if (format != HAL_PIXEL_FORMAT_BLOB && format != HAL_PIXEL_FORMAT_RAW_OPAQUE) {
ALOGE("%s: Bad format for size-only stream: %d", __FUNCTION__,
}
}
+Camera3OutputStream::Camera3OutputStream(int id,
+ uint32_t width, uint32_t height, int format,
+ uint32_t consumerUsage, android_dataspace dataSpace,
+ camera3_stream_rotation_t rotation, nsecs_t timestampOffset, int setId) :
+ Camera3IOStreamBase(id, CAMERA3_STREAM_OUTPUT, width, height,
+ /*maxSize*/0, format, dataSpace, rotation, setId),
+ mConsumer(nullptr),
+ mTransform(0),
+ mTraceFirstBuffer(true),
+ mUseBufferManager(false),
+ mTimestampOffset(timestampOffset),
+ mConsumerUsage(consumerUsage) {
+ // Deferred consumer only support preview surface format now.
+ if (format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
+ ALOGE("%s: Deferred consumer only supports IMPLEMENTATION_DEFINED format now!",
+ __FUNCTION__);
+ mState = STATE_ERROR;
+ }
+
+ // Sanity check for the consumer usage flag.
+ if ((consumerUsage & GraphicBuffer::USAGE_HW_TEXTURE) == 0 &&
+ (consumerUsage & GraphicBuffer::USAGE_HW_COMPOSER) == 0) {
+ ALOGE("%s: Deferred consumer usage flag is illegal (0x%x)!", __FUNCTION__, consumerUsage);
+ mState = STATE_ERROR;
+ }
+
+ mConsumerName = String8("Deferred");
+ if (setId > CAMERA3_STREAM_SET_ID_INVALID) {
+ mBufferReleasedListener = new BufferReleasedListener(this);
+ }
+
+}
+
Camera3OutputStream::Camera3OutputStream(int id, camera3_stream_type_t type,
uint32_t width, uint32_t height,
int format,
mTransform(0),
mTraceFirstBuffer(true),
mUseMonoTimestamp(false),
- mUseBufferManager(false) {
+ mUseBufferManager(false),
+ mConsumerUsage(0) {
if (setId > CAMERA3_STREAM_SET_ID_INVALID) {
mBufferReleasedListener = new BufferReleasedListener(this);
*/
if (buffer.status == CAMERA3_BUFFER_STATUS_ERROR) {
// Cancel buffer
+ ALOGW("A frame is dropped for stream %d", mId);
res = currentConsumer->cancelBuffer(currentConsumer.get(),
container_of(buffer.buffer, ANativeWindowBuffer, handle),
anwReleaseFence);
__FUNCTION__, mTransform, strerror(-res), res);
}
+ // Set dequeueBuffer/attachBuffer timeout if the consumer is not hw composer or hw texture.
+ // We need skip these cases as timeout will disable the non-blocking (async) mode.
+ if (!(isConsumedByHWComposer() || isConsumedByHWTexture())) {
+ mConsumer->setDequeueTimeout(kDequeueBufferTimeout);
+ }
+
/**
* Camera3 Buffer manager is only supported by HAL3.3 onwards, as the older HALs requires
* buffers to be statically allocated for internal static buffer registration, while the
return res;
}
+ // Stream configuration was not finished (can only be in STATE_IN_CONFIG or STATE_CONSTRUCTED
+ // state), don't need change the stream state, return OK.
+ if (mConsumer == nullptr) {
+ return OK;
+ }
+
ALOGV("%s: disconnecting stream %d from native window", __FUNCTION__, getId());
res = native_window_api_disconnect(mConsumer.get(),
NATIVE_WINDOW_API_CAMERA);
-
/**
* This is not an error. if client calling process dies, the window will
* also die and all calls to it will return DEAD_OBJECT, thus it's already
status_t res;
int32_t u = 0;
+ if (mConsumer == nullptr) {
+ // mConsumerUsage was sanitized before the Camera3OutputStream was constructed.
+ *usage = mConsumerUsage;
+ return OK;
+ }
+
res = static_cast<ANativeWindow*>(mConsumer.get())->query(mConsumer.get(),
NATIVE_WINDOW_CONSUMER_USAGE_BITS, &u);
status_t Camera3OutputStream::setBufferManager(sp<Camera3BufferManager> bufferManager) {
Mutex::Autolock l(mLock);
if (mState != STATE_CONSTRUCTED) {
- ALOGE("%s: this method can only be called when stream in in CONSTRUCTED state.",
+ ALOGE("%s: this method can only be called when stream in CONSTRUCTED state.",
__FUNCTION__);
return INVALID_OPERATION;
}
return OK;
}
+bool Camera3OutputStream::isConsumerConfigurationDeferred() const {
+ Mutex::Autolock l(mLock);
+ return mConsumer == nullptr;
+}
+
+status_t Camera3OutputStream::setConsumer(sp<Surface> consumer) {
+ if (consumer == nullptr) {
+ ALOGE("%s: it's illegal to set a null consumer surface!", __FUNCTION__);
+ return INVALID_OPERATION;
+ }
+
+ if (mConsumer != nullptr) {
+ ALOGE("%s: consumer surface was already set!", __FUNCTION__);
+ return INVALID_OPERATION;
+ }
+
+ mConsumer = consumer;
+ return OK;
+}
+
bool Camera3OutputStream::isConsumedByHWComposer() const {
uint32_t usage = 0;
status_t res = getEndpointUsage(&usage);
return (usage & GRALLOC_USAGE_HW_COMPOSER) != 0;
}
+bool Camera3OutputStream::isConsumedByHWTexture() const {
+ uint32_t usage = 0;
+ status_t res = getEndpointUsage(&usage);
+ if (res != OK) {
+ ALOGE("%s: getting end point usage failed: %s (%d).", __FUNCTION__, strerror(-res), res);
+ return false;
+ }
+
+ return (usage & GRALLOC_USAGE_HW_TEXTURE) != 0;
+}
+
}; // namespace camera3
}; // namespace android
android_dataspace dataSpace, camera3_stream_rotation_t rotation,
nsecs_t timestampOffset, int setId = CAMERA3_STREAM_SET_ID_INVALID);
+ /**
+ * Set up a stream with deferred consumer for formats that have 2 dimensions, such as
+ * RAW and YUV. The consumer must be set before using this stream for output. A valid
+ * stream set id needs to be set to support buffer sharing between multiple streams.
+ */
+ Camera3OutputStream(int id, uint32_t width, uint32_t height, int format,
+ uint32_t consumerUsage, android_dataspace dataSpace,
+ camera3_stream_rotation_t rotation, nsecs_t timestampOffset,
+ int setId = CAMERA3_STREAM_SET_ID_INVALID);
+
virtual ~Camera3OutputStream();
/**
*/
bool isConsumedByHWComposer() const;
+ /**
+ * Return if this output stream is consumed by hardware texture.
+ */
+ bool isConsumedByHWTexture() const;
+
+ /**
+ * Return if the consumer configuration of this stream is deferred.
+ */
+ virtual bool isConsumerConfigurationDeferred() const;
+
+ /**
+ * Set the consumer surface to the output stream.
+ */
+ virtual status_t setConsumer(sp<Surface> consumer);
+
class BufferReleasedListener : public BnProducerListener {
public:
BufferReleasedListener(wp<Camera3OutputStream> parent) : mParent(parent) {}
virtual status_t disconnectLocked();
sp<Surface> mConsumer;
+
private:
+
+ static const nsecs_t kDequeueBufferTimeout = 1000000000; // 1 sec
+
int mTransform;
virtual status_t setTransformLocked(int transform);
nsecs_t mTimestampOffset;
/**
+ * Consumer end point usage flag set by the constructor for the deferred
+ * consumer case.
+ */
+ uint32_t mConsumerUsage;
+
+ /**
* Internal Camera3Stream interface
*/
virtual status_t getBufferLocked(camera3_stream_buffer *buffer);
virtual bool isVideoStream() const = 0;
/**
+ * Return if the consumer configuration of this stream is deferred.
+ */
+ virtual bool isConsumerConfigurationDeferred() const = 0;
+
+ /**
+ * Set the consumer surface to the output stream.
+ */
+ virtual status_t setConsumer(sp<Surface> consumer) = 0;
+
+ /**
* Detach an unused buffer from the stream.
*
* buffer must be non-null; fenceFd may null, and if it is non-null, but
*
*/
virtual status_t detachBuffer(sp<GraphicBuffer>* buffer, int* fenceFd) = 0;
-
};
} // namespace camera3
mMaxSize(maxSize),
mState(STATE_CONSTRUCTED),
mStatusId(StatusTracker::NO_STATUS_ID),
- mStreamUnpreparable(false),
+ mStreamUnpreparable(true),
mOldUsage(0),
mOldMaxBuffers(0),
mPrepared(false),
* OK on a successful configuration
* NO_INIT in case of a serious error from the HAL device
* NO_MEMORY in case of an error registering buffers
- * INVALID_OPERATION in case connecting to the consumer failed
+ * INVALID_OPERATION in case connecting to the consumer failed or consumer
+ * doesn't exist yet.
*/
status_t finishConfiguration(camera3_device *hal3Device);
--- /dev/null
+/*
+ * Copyright (C) 2016 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_TAG "Camera3-TagMonitor"
+#define ATRACE_TAG ATRACE_TAG_CAMERA
+//#define LOG_NDEBUG 0
+
+#include "TagMonitor.h"
+
+#include <inttypes.h>
+#include <utils/Log.h>
+#include <camera/VendorTagDescriptor.h>
+
+namespace android {
+
+TagMonitor::TagMonitor():
+ mMonitoringEnabled(false),
+ mMonitoringEvents(kMaxMonitorEvents)
+{}
+
+const char* TagMonitor::k3aTags =
+ "android.control.aeMode, android.control.afMode, android.control.awbMode,"
+ "android.control.aeState, android.control.afState, android.control.awbState,"
+ "android.control.aePrecaptureTrigger, android.control.afTrigger,"
+ "android.control.aeRegions, android.control.awbRegions, android.control.afRegions,"
+ "android.control.aeExposureCompensation, android.control.aeLock, android.control.awbLock,"
+ "android.control.aeAntibandingMode, android.control.aeTargetFpsRange,"
+ "android.control.effectMode, android.control.mode, android.control.sceneMode,"
+ "android.control.videoStabilizationMode";
+
+void TagMonitor::parseTagsToMonitor(String8 tagNames) {
+ std::lock_guard<std::mutex> lock(mMonitorMutex);
+
+ // Expand shorthands
+ if (ssize_t idx = tagNames.find("3a") != -1) {
+ ssize_t end = tagNames.find(",", idx);
+ char* start = tagNames.lockBuffer(tagNames.size());
+ start[idx] = '\0';
+ char* rest = (end != -1) ? (start + end) : (start + tagNames.size());
+ tagNames = String8::format("%s%s%s", start, k3aTags, rest);
+ }
+
+ sp<VendorTagDescriptor> vTags =
+ VendorTagDescriptor::getGlobalVendorTagDescriptor();
+
+ bool gotTag = false;
+
+ char *tokenized = tagNames.lockBuffer(tagNames.size());
+ char *savePtr;
+ char *nextTagName = strtok_r(tokenized, ", ", &savePtr);
+ while (nextTagName != nullptr) {
+ uint32_t tag;
+ status_t res = CameraMetadata::getTagFromName(nextTagName, vTags.get(), &tag);
+ if (res != OK) {
+ ALOGW("%s: Unknown tag %s, ignoring", __FUNCTION__, nextTagName);
+ } else {
+ if (!gotTag) {
+ mMonitoredTagList.clear();
+ gotTag = true;
+ }
+ mMonitoredTagList.push_back(tag);
+ }
+ nextTagName = strtok_r(nullptr, ", ", &savePtr);
+ }
+
+ tagNames.unlockBuffer();
+
+ if (gotTag) {
+ // Got at least one new tag
+ mMonitoringEnabled = true;
+ }
+}
+
+void TagMonitor::disableMonitoring() {
+ mMonitoringEnabled = false;
+ mLastMonitoredRequestValues.clear();
+ mLastMonitoredResultValues.clear();
+}
+
+void TagMonitor::monitorMetadata(eventSource source, int64_t frameNumber, nsecs_t timestamp,
+ const CameraMetadata& metadata) {
+ if (!mMonitoringEnabled) return;
+
+ std::lock_guard<std::mutex> lock(mMonitorMutex);
+
+ if (timestamp == 0) {
+ timestamp = systemTime(SYSTEM_TIME_BOOTTIME);
+ }
+
+ for (auto tag : mMonitoredTagList) {
+ camera_metadata_ro_entry entry = metadata.find(tag);
+ CameraMetadata &lastValues = (source == REQUEST) ?
+ mLastMonitoredRequestValues : mLastMonitoredResultValues;
+ camera_metadata_entry lastEntry = lastValues.find(tag);
+
+ if (entry.count > 0) {
+ bool isDifferent = false;
+ if (lastEntry.count > 0) {
+ // Have a last value, compare to see if changed
+ if (lastEntry.type == entry.type &&
+ lastEntry.count == entry.count) {
+ // Same type and count, compare values
+ size_t bytesPerValue = camera_metadata_type_size[lastEntry.type];
+ size_t entryBytes = bytesPerValue * lastEntry.count;
+ int cmp = memcmp(entry.data.u8, lastEntry.data.u8, entryBytes);
+ if (cmp != 0) {
+ isDifferent = true;
+ }
+ } else {
+ // Count or type has changed
+ isDifferent = true;
+ }
+ } else {
+ // No last entry, so always consider to be different
+ isDifferent = true;
+ }
+
+ if (isDifferent) {
+ ALOGV("%s: Tag %s changed", __FUNCTION__, get_camera_metadata_tag_name(tag));
+ lastValues.update(entry);
+ mMonitoringEvents.emplace(source, frameNumber, timestamp, entry);
+ }
+ } else if (lastEntry.count > 0) {
+ // Value has been removed
+ ALOGV("%s: Tag %s removed", __FUNCTION__, get_camera_metadata_tag_name(tag));
+ lastValues.erase(tag);
+ entry.tag = tag;
+ entry.type = get_camera_metadata_tag_type(tag);
+ entry.count = 0;
+ mMonitoringEvents.emplace(source, frameNumber, timestamp, entry);
+ }
+ }
+}
+
+void TagMonitor::dumpMonitoredMetadata(int fd) {
+ std::lock_guard<std::mutex> lock(mMonitorMutex);
+
+ if (mMonitoringEnabled) {
+ dprintf(fd, " Tag monitoring enabled for tags:\n");
+ for (uint32_t tag : mMonitoredTagList) {
+ dprintf(fd, " %s.%s\n",
+ get_camera_metadata_section_name(tag),
+ get_camera_metadata_tag_name(tag));
+ }
+ } else {
+ dprintf(fd, " Tag monitoring disabled (enable with -m <name1,..,nameN>)\n");
+ }
+ if (mMonitoringEvents.size() > 0) {
+ dprintf(fd, " Monitored tag event log:\n");
+ for (const auto& event : mMonitoringEvents) {
+ int indentation = (event.source == REQUEST) ? 15 : 30;
+ dprintf(fd, " f%d:%" PRId64 "ns: %*s%s.%s: ",
+ event.frameNumber, event.timestamp,
+ indentation,
+ event.source == REQUEST ? "REQ:" : "RES:",
+ get_camera_metadata_section_name(event.tag),
+ get_camera_metadata_tag_name(event.tag));
+ if (event.newData.size() == 0) {
+ dprintf(fd, " (Removed)\n");
+ } else {
+ printData(fd, event.newData.data(), event.tag,
+ event.type, event.newData.size() / camera_metadata_type_size[event.type],
+ indentation + 18);
+ }
+ }
+ }
+
+}
+
+// TODO: Consolidate with printData from camera_metadata.h
+
+#define CAMERA_METADATA_ENUM_STRING_MAX_SIZE 29
+
+void TagMonitor::printData(int fd, const uint8_t *data_ptr, uint32_t tag,
+ int type, int count, int indentation) {
+ static int values_per_line[NUM_TYPES] = {
+ [TYPE_BYTE] = 16,
+ [TYPE_INT32] = 8,
+ [TYPE_FLOAT] = 8,
+ [TYPE_INT64] = 4,
+ [TYPE_DOUBLE] = 4,
+ [TYPE_RATIONAL] = 4,
+ };
+ size_t type_size = camera_metadata_type_size[type];
+ char value_string_tmp[CAMERA_METADATA_ENUM_STRING_MAX_SIZE];
+ uint32_t value;
+
+ int lines = count / values_per_line[type];
+ if (count % values_per_line[type] != 0) lines++;
+
+ int index = 0;
+ int j, k;
+ for (j = 0; j < lines; j++) {
+ dprintf(fd, "%*s[", (j != 0) ? indentation + 4 : 0, "");
+ for (k = 0;
+ k < values_per_line[type] && count > 0;
+ k++, count--, index += type_size) {
+
+ switch (type) {
+ case TYPE_BYTE:
+ value = *(data_ptr + index);
+ if (camera_metadata_enum_snprint(tag,
+ value,
+ value_string_tmp,
+ sizeof(value_string_tmp))
+ == OK) {
+ dprintf(fd, "%s ", value_string_tmp);
+ } else {
+ dprintf(fd, "%hhu ",
+ *(data_ptr + index));
+ }
+ break;
+ case TYPE_INT32:
+ value =
+ *(int32_t*)(data_ptr + index);
+ if (camera_metadata_enum_snprint(tag,
+ value,
+ value_string_tmp,
+ sizeof(value_string_tmp))
+ == OK) {
+ dprintf(fd, "%s ", value_string_tmp);
+ } else {
+ dprintf(fd, "%" PRId32 " ",
+ *(int32_t*)(data_ptr + index));
+ }
+ break;
+ case TYPE_FLOAT:
+ dprintf(fd, "%0.8f ",
+ *(float*)(data_ptr + index));
+ break;
+ case TYPE_INT64:
+ dprintf(fd, "%" PRId64 " ",
+ *(int64_t*)(data_ptr + index));
+ break;
+ case TYPE_DOUBLE:
+ dprintf(fd, "%0.8f ",
+ *(double*)(data_ptr + index));
+ break;
+ case TYPE_RATIONAL: {
+ int32_t numerator = *(int32_t*)(data_ptr + index);
+ int32_t denominator = *(int32_t*)(data_ptr + index + 4);
+ dprintf(fd, "(%d / %d) ",
+ numerator, denominator);
+ break;
+ }
+ default:
+ dprintf(fd, "??? ");
+ }
+ }
+ dprintf(fd, "]\n");
+ }
+}
+
+template<typename T>
+TagMonitor::MonitorEvent::MonitorEvent(eventSource src, uint32_t frameNumber, nsecs_t timestamp,
+ const T &value) :
+ source(src),
+ frameNumber(frameNumber),
+ timestamp(timestamp),
+ tag(value.tag),
+ type(value.type),
+ newData(value.data.u8, value.data.u8 + camera_metadata_type_size[value.type] * value.count) {
+}
+
+TagMonitor::MonitorEvent::~MonitorEvent() {
+}
+
+} // namespace android
--- /dev/null
+/*
+ * Copyright (C) 2016 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 ANDROID_SERVERS_CAMERA_TAGMONITOR_H
+#define ANDROID_SERVERS_CAMERA_TAGMONITOR_H
+
+#include <vector>
+#include <atomic>
+#include <mutex>
+
+#include <utils/RefBase.h>
+#include <utils/String8.h>
+#include <utils/Timers.h>
+
+#include <media/RingBuffer.h>
+#include <system/camera_metadata.h>
+#include <camera/CameraMetadata.h>
+
+namespace android {
+
+/**
+ * A monitor for camera metadata values.
+ * Tracks changes to specified metadata values over time, keeping a circular
+ * buffer log that can be dumped at will. */
+class TagMonitor {
+ public:
+ enum eventSource {
+ REQUEST,
+ RESULT
+ };
+
+ TagMonitor();
+
+ // Parse tag name list (comma-separated) and if valid, enable monitoring
+ // If invalid, do nothing.
+ // Recognizes "3a" as a shortcut for enabling tracking 3A state, mode, and
+ // triggers
+ void parseTagsToMonitor(String8 tagNames);
+
+ // Disable monitoring; does not clear the event log
+ void disableMonitoring();
+
+ // Scan through the metadata and update the monitoring information
+ void monitorMetadata(eventSource source, int64_t frameNumber,
+ nsecs_t timestamp, const CameraMetadata& metadata);
+
+ // Dump current event log to the provided fd
+ void dumpMonitoredMetadata(int fd);
+
+ private:
+
+ static void printData(int fd, const uint8_t *data_ptr, uint32_t tag,
+ int type, int count, int indentation);
+
+ std::atomic<bool> mMonitoringEnabled;
+ std::mutex mMonitorMutex;
+
+ // Current tags to monitor and record changes to
+ std::vector<uint32_t> mMonitoredTagList;
+
+ // Latest-seen values of tracked tags
+ CameraMetadata mLastMonitoredRequestValues;
+ CameraMetadata mLastMonitoredResultValues;
+
+ /**
+ * A monitoring event
+ * Stores a new metadata field value and the timestamp at which it changed.
+ * Copies the source metadata value array and frees it on destruct.
+ */
+ struct MonitorEvent {
+ template<typename T>
+ MonitorEvent(eventSource src, uint32_t frameNumber, nsecs_t timestamp,
+ const T &newValue);
+ ~MonitorEvent();
+
+ eventSource source;
+ uint32_t frameNumber;
+ nsecs_t timestamp;
+ uint32_t tag;
+ uint8_t type;
+ std::vector<uint8_t> newData;
+ };
+
+ // A ring buffer for tracking the last kMaxMonitorEvents metadata changes
+ static const int kMaxMonitorEvents = 100;
+ RingBuffer<MonitorEvent> mMonitoringEvents;
+
+ // 3A fields to use with the "3a" option
+ static const char *k3aTags;
+};
+
+} // namespace android
+
+#endif
LOCAL_MULTILIB := $(AUDIOSERVER_MULTILIB)
+LOCAL_CFLAGS := -Wall -Werror
+
LOCAL_MODULE:= libsoundtriggerservice
include $(BUILD_SHARED_LIBRARY)