Added native metadata support.

Metadata.java:
Fixed typo 8k != 8092. The comment was correct though.

In Metadata.h, the new Metadata class is declared in the ns android::media
to limit the chances of conflict with other packages.

The MetadataType in MediaPlayerInterface is gone and moved to Metadata as
an inner typedef.

Similarly the SortedVector<MetadataType> instance have been replace by a
new type Metadata::Filter.

All the keys declared in the java counterpart are also in Metadata.h.

Metadata.cpp:
Contains the implementation of the native metadata packing.

There an associated change in the opencore package that should go in
at the same time as this one.
This commit is contained in:
niko
2009-07-20 15:07:26 -07:00
parent c7bebd1537
commit bc72692780
14 changed files with 359 additions and 87 deletions

View File

@@ -26,11 +26,10 @@
#include <media/mediaplayer.h>
#include <media/AudioSystem.h>
#include <media/Metadata.h>
namespace android {
typedef int32_t MetadataType;
class Parcel;
template<typename T> class SortedVector;
@@ -129,8 +128,10 @@ public:
// the known metadata should be returned.
// @param[inout] records Parcel where the player appends its metadata.
// @return OK if the call was successful.
virtual status_t getMetadata(const SortedVector<MetadataType>& ids,
Parcel *records) = 0;
virtual status_t getMetadata(const media::Metadata::Filter& ids,
Parcel *records) {
return INVALID_OPERATION;
};
protected:
virtual void sendEvent(int msg, int ext1=0, int ext2=0) { if (mNotify) mNotify(mCookie, msg, ext1, ext2); }

133
include/media/Metadata.h Normal file
View File

@@ -0,0 +1,133 @@
/*
* Copyright (C) 2009 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_MEDIA_METADATA_H__
#define ANDROID_MEDIA_METADATA_H__
#include <sys/types.h>
#include <utils/Errors.h>
#include <utils/RefBase.h>
#include <utils/SortedVector.h>
namespace android {
class Parcel;
namespace media {
// Metadata is a class to build/serialize a set of metadata in a Parcel.
//
// This class should be kept in sync with android/media/Metadata.java.
// It provides all the metadata ids available and methods to build the
// header, add records and adjust the set size header field.
//
// Typical Usage:
// ==============
// Parcel p;
// media::Metadata data(&p);
//
// data.appendHeader();
// data.appendBool(Metadata::kPauseAvailable, true);
// ... more append ...
// data.updateLength();
//
class Metadata {
public:
typedef int32_t Type;
typedef SortedVector<Type> Filter;
static const Type kAny = 0;
// Keep in sync with android/media/Metadata.java
static const Type kTitle = 1; // String
static const Type kComment = 2; // String
static const Type kCopyright = 3; // String
static const Type kAlbum = 4; // String
static const Type kArtist = 5; // String
static const Type kAuthor = 6; // String
static const Type kComposer = 7; // String
static const Type kGenre = 8; // String
static const Type kDate = 9; // Date
static const Type kDuration = 10; // Integer(millisec)
static const Type kCdTrackNum = 11; // Integer 1-based
static const Type kCdTrackMax = 12; // Integer
static const Type kRating = 13; // String
static const Type kAlbumArt = 14; // byte[]
static const Type kVideoFrame = 15; // Bitmap
static const Type kCaption = 16; // TimedText
static const Type kBitRate = 17; // Integer, Aggregate rate of
// all the streams in bps.
static const Type kAudioBitRate = 18; // Integer, bps
static const Type kVideoBitRate = 19; // Integer, bps
static const Type kAudioSampleRate = 20; // Integer, Hz
static const Type kVideoframeRate = 21; // Integer, Hz
// See RFC2046 and RFC4281.
static const Type kMimeType = 22; // String
static const Type kAudioCodec = 23; // String
static const Type kVideoCodec = 24; // String
static const Type kVideoHeight = 25; // Integer
static const Type kVideoWidth = 26; // Integer
static const Type kNumTracks = 27; // Integer
static const Type kDrmCrippled = 28; // Boolean
// Playback capabilities.
static const Type kPauseAvailable = 29; // Boolean
static const Type kSeekBackwardAvailable = 30; // Boolean
static const Type kSeekForwardAvailable = 31; // Boolean
// @param p[inout] The parcel to append the metadata records
// to. The global metadata header should have been set already.
explicit Metadata(Parcel *p);
~Metadata();
// Rewind the underlying parcel, undoing all the changes.
void resetParcel();
// Append the size and 'META' marker.
bool appendHeader();
// Once all the records have been added, call this to update the
// lenght field in the header.
void updateLength();
// append* are methods to append metadata.
// @param key Is the metadata Id.
// @param val Is the value of the metadata.
// @return true if successful, false otherwise.
// TODO: add more as needed to handle other types.
bool appendBool(Type key, bool val);
bool appendInt32(Type key, int32_t val);
private:
Metadata(const Metadata&);
Metadata& operator=(const Metadata&);
// Checks the key is valid and not already present.
bool checkKey(Type key);
Parcel *mData;
size_t mBegin;
};
} // namespace android::media
} // namespace android
#endif // ANDROID_MEDIA_METADATA_H__

View File

@@ -19,6 +19,7 @@
#include <utils/Errors.h>
#include <media/MediaPlayerInterface.h>
#include <media/Metadata.h>
#define MAX_OPENCORE_INSTANCES 25
@@ -53,8 +54,9 @@ public:
virtual status_t setLooping(int loop);
virtual player_type playerType() { return PV_PLAYER; }
virtual status_t invoke(const Parcel& request, Parcel *reply);
virtual status_t getMetadata(const SortedVector<MetadataType>& ids,
Parcel *records);
virtual status_t getMetadata(
const SortedVector<media::Metadata::Type>& ids,
Parcel *records);
// make available to PlayerDriver
void sendEvent(int msg, int ext1=0, int ext2=0) { MediaPlayerBase::sendEvent(msg, ext1, ext2); }

View File

@@ -104,7 +104,7 @@ public class Metadata
public static final int SEEK_FORWARD_AVAILABLE = 31; // Boolean
private static final int LAST_SYSTEM = 31;
private static final int FIRST_CUSTOM = 8092;
private static final int FIRST_CUSTOM = 8192;
// Shorthands to set the MediaPlayer's metadata filter.
public static final Set<Integer> MATCH_NONE = Collections.EMPTY_SET;

View File

@@ -2,24 +2,25 @@ LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= \
AudioTrack.cpp \
IAudioFlinger.cpp \
IAudioFlingerClient.cpp \
IAudioTrack.cpp \
IAudioRecord.cpp \
AudioRecord.cpp \
AudioSystem.cpp \
mediaplayer.cpp \
IMediaPlayerService.cpp \
IMediaPlayerClient.cpp \
IMediaPlayer.cpp \
IMediaRecorder.cpp \
mediarecorder.cpp \
IMediaMetadataRetriever.cpp \
mediametadataretriever.cpp \
ToneGenerator.cpp \
JetPlayer.cpp \
IOMX.cpp
AudioTrack.cpp \
IAudioFlinger.cpp \
IAudioFlingerClient.cpp \
IAudioTrack.cpp \
IAudioRecord.cpp \
AudioRecord.cpp \
AudioSystem.cpp \
mediaplayer.cpp \
IMediaPlayerService.cpp \
IMediaPlayerClient.cpp \
IMediaPlayer.cpp \
IMediaRecorder.cpp \
Metadata.cpp \
mediarecorder.cpp \
IMediaMetadataRetriever.cpp \
mediametadataretriever.cpp \
ToneGenerator.cpp \
JetPlayer.cpp \
IOMX.cpp
LOCAL_SHARED_LIBRARIES := \
libui libcutils libutils libbinder libsonivox

168
media/libmedia/Metadata.cpp Normal file
View File

@@ -0,0 +1,168 @@
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//#define LOG_NDEBUG 0
#define LOG_TAG "Metadata"
#include <utils/Log.h>
#include <sys/types.h>
#include <media/Metadata.h>
#include <binder/Parcel.h>
#include <utils/Errors.h>
#include <utils/RefBase.h>
// This file contains code to serialize Metadata triples (key, type,
// value) into a parcel. The Parcel is destinated to be decoded by the
// Metadata.java class.
namespace {
// All these constants below must be kept in sync with Metadata.java.
enum MetadataId {
FIRST_SYSTEM_ID = 1,
LAST_SYSTEM_ID = 31,
FIRST_CUSTOM_ID = 8192
};
// Types
enum Types {
STRING_VAL = 1,
INTEGER_VAL,
BOOLEAN_VAL,
LONG_VAL,
DOUBLE_VAL,
TIMED_TEXT_VAL,
DATE_VAL,
BYTE_ARRAY_VAL,
};
const size_t kRecordHeaderSize = 3 * sizeof(int32_t);
const int32_t kMetaMarker = 0x4d455441; // 'M' 'E' 'T' 'A'
} // anonymous namespace
namespace android {
namespace media {
Metadata::Metadata(Parcel *p)
:mData(p),
mBegin(p->dataPosition()) { }
Metadata::~Metadata() { }
void Metadata::resetParcel()
{
mData->setDataPosition(mBegin);
}
// Update the 4 bytes int at the beginning of the parcel which holds
// the number of bytes written so far.
void Metadata::updateLength()
{
const size_t end = mData->dataPosition();
mData->setDataPosition(mBegin);
mData->writeInt32(end - mBegin);
mData->setDataPosition(end);
}
// Write the header. The java layer will look for the marker.
bool Metadata::appendHeader()
{
bool ok = true;
// Placeholder for the length of the metadata
ok = ok && mData->writeInt32(-1) == OK;
ok = ok && mData->writeInt32(kMetaMarker) == OK;
return ok;
}
bool Metadata::appendBool(int key, bool val)
{
if (!checkKey(key)) {
return false;
}
const size_t begin = mData->dataPosition();
bool ok = true;
// 4 int32s: size, key, type, value.
ok = ok && mData->writeInt32(4 * sizeof(int32_t)) == OK;
ok = ok && mData->writeInt32(key) == OK;
ok = ok && mData->writeInt32(BOOLEAN_VAL) == OK;
ok = ok && mData->writeInt32(val ? 1 : 0) == OK;
if (!ok) {
mData->setDataPosition(begin);
}
return ok;
}
bool Metadata::appendInt32(int key, int32_t val)
{
if (!checkKey(key)) {
return false;
}
const size_t begin = mData->dataPosition();
bool ok = true;
// 4 int32s: size, key, type, value.
ok = ok && mData->writeInt32(4 * sizeof(int32_t)) == OK;
ok = ok && mData->writeInt32(key) == OK;
ok = ok && mData->writeInt32(INTEGER_VAL) == OK;
ok = ok && mData->writeInt32(val) == OK;
if (!ok) {
mData->setDataPosition(begin);
}
return ok;
}
// Check the key (i.e metadata id) is valid if it is a system one.
// Loop over all the exiting ones in the Parcel to check for duplicate
// (not allowed).
bool Metadata::checkKey(int key)
{
if (key < FIRST_SYSTEM_ID ||
(LAST_SYSTEM_ID < key && key < FIRST_CUSTOM_ID)) {
LOGE("Bad key %d", key);
return false;
}
size_t curr = mData->dataPosition();
// Loop over the keys to check if it has been used already.
mData->setDataPosition(mBegin);
bool error = false;
size_t left = curr - mBegin;
while (left > 0) {
size_t pos = mData->dataPosition();
size_t size = mData->readInt32();
if (size < kRecordHeaderSize || size > left) {
error = true;
break;
}
if (mData->readInt32() == key) {
LOGE("Key exists already %d", key);
error = true;
break;
}
mData->setDataPosition(pos + size);
left -= size;
}
mData->setDataPosition(curr);
return !error;
}
} // namespace android::media
} // namespace android

View File

@@ -47,10 +47,9 @@
#include <media/MediaPlayerInterface.h>
#include <media/mediarecorder.h>
#include <media/MediaMetadataRetrieverInterface.h>
#include <media/Metadata.h>
#include <media/AudioTrack.h>
#include <utils/SortedVector.h>
#include "MediaRecorderClient.h"
#include "MediaPlayerService.h"
#include "MetadataRetrieverClient.h"
@@ -85,21 +84,17 @@ pid_t gettid() { return syscall(__NR_gettid);}
#endif
namespace {
using android::media::Metadata;
using android::status_t;
using android::OK;
using android::BAD_VALUE;
using android::NOT_ENOUGH_DATA;
using android::MetadataType;
using android::Parcel;
using android::SortedVector;
// Max number of entries in the filter.
const int kMaxFilterSize = 64; // I pulled that out of thin air.
// Keep in sync with ANY in Metadata.java
const int32_t kAny = 0;
const int32_t kMetaMarker = 0x4d455441; // 'M' 'E' 'T' 'A'
// FIXME: Move all the metadata related function in the Metadata.cpp
// Unmarshall a filter from a Parcel.
@@ -124,7 +119,7 @@ const int32_t kMetaMarker = 0x4d455441; // 'M' 'E' 'T' 'A'
// @param[out] status On exit contains the status code to be returned.
// @return true if the parcel starts with a valid filter.
bool unmarshallFilter(const Parcel& p,
SortedVector<MetadataType> *filter,
Metadata::Filter *filter,
status_t *status)
{
int32_t val;
@@ -147,7 +142,7 @@ bool unmarshallFilter(const Parcel& p,
filter->clear();
filter->setCapacity(num);
size_t size = num * sizeof(MetadataType);
size_t size = num * sizeof(Metadata::Type);
if (p.dataAvail() < size)
@@ -157,7 +152,8 @@ bool unmarshallFilter(const Parcel& p,
return false;
}
const MetadataType *data = static_cast<const MetadataType*>(p.readInplace(size));
const Metadata::Type *data =
static_cast<const Metadata::Type*>(p.readInplace(size));
if (NULL == data)
{
@@ -181,11 +177,11 @@ bool unmarshallFilter(const Parcel& p,
// @param filter Of metadata type.
// @param val To be searched.
// @return true if a match was found.
bool findMetadata(const SortedVector<MetadataType>& filter, const int32_t val)
bool findMetadata(const Metadata::Filter& filter, const int32_t val)
{
// Deal with empty and ANY right away
if (filter.isEmpty()) return false;
if (filter[0] == kAny) return true;
if (filter[0] == Metadata::kAny) return true;
return filter.indexOf(val) >= 0;
}
@@ -857,7 +853,7 @@ status_t MediaPlayerService::Client::invoke(const Parcel& request,
status_t MediaPlayerService::Client::setMetadataFilter(const Parcel& filter)
{
status_t status;
SortedVector<MetadataType> allow, drop;
media::Metadata::Filter allow, drop;
if (unmarshallFilter(filter, &allow, &status) &&
unmarshallFilter(filter, &drop, &status)) {
@@ -872,14 +868,14 @@ status_t MediaPlayerService::Client::setMetadataFilter(const Parcel& filter)
status_t MediaPlayerService::Client::getMetadata(
bool update_only, bool apply_filter, Parcel *reply)
{
sp<MediaPlayerBase> p = getPlayer();
if (p == 0) return UNKNOWN_ERROR;
sp<MediaPlayerBase> player = getPlayer();
if (player == 0) return UNKNOWN_ERROR;
status_t status;
// Placeholder for the return code, updated by the caller.
reply->writeInt32(-1);
SortedVector<MetadataType> ids;
media::Metadata::Filter ids;
// We don't block notifications while we fetch the data. We clear
// mMetadataUpdated first so we don't lose notifications happening
@@ -892,14 +888,13 @@ status_t MediaPlayerService::Client::getMetadata(
mMetadataUpdated.clear();
}
const size_t begin = reply->dataPosition();
reply->writeInt32(-1); // Placeholder for the length of the metadata
reply->writeInt32(kMetaMarker);
media::Metadata metadata(reply);
status = p->getMetadata(ids, reply);
metadata.appendHeader();
status = player->getMetadata(ids, reply);
if (status != OK) {
reply->setDataPosition(begin);
metadata.resetParcel();
LOGE("getMetadata failed %d", status);
return status;
}
@@ -908,12 +903,8 @@ status_t MediaPlayerService::Client::getMetadata(
// filtering takes place on the update notifications already. This
// would be when all the metadata are fetch and a filter is set.
const size_t end = reply->dataPosition();
// Everything is fine, update the metadata length.
reply->setDataPosition(begin);
reply->writeInt32(end - begin);
reply->setDataPosition(end);
metadata.updateLength();
return OK;
}
@@ -1043,7 +1034,7 @@ void MediaPlayerService::Client::notify(void* cookie, int msg, int ext1, int ext
if (MEDIA_INFO == msg &&
MEDIA_INFO_METADATA_UPDATE == ext1) {
const MetadataType metadata_type = ext2;
const media::Metadata::Type metadata_type = ext2;
if(client->shouldDropMetadata(metadata_type)) {
return;
@@ -1058,7 +1049,7 @@ void MediaPlayerService::Client::notify(void* cookie, int msg, int ext1, int ext
}
bool MediaPlayerService::Client::shouldDropMetadata(MetadataType code) const
bool MediaPlayerService::Client::shouldDropMetadata(media::Metadata::Type code) const
{
Mutex::Autolock lock(mLock);
@@ -1074,7 +1065,7 @@ bool MediaPlayerService::Client::shouldDropMetadata(MetadataType code) const
}
void MediaPlayerService::Client::addNewMetadataUpdate(MetadataType metadata_type) {
void MediaPlayerService::Client::addNewMetadataUpdate(media::Metadata::Type metadata_type) {
Mutex::Autolock lock(mLock);
if (mMetadataUpdated.indexOf(metadata_type) < 0) {
mMetadataUpdated.add(metadata_type);
@@ -1499,4 +1490,4 @@ void MediaPlayerService::AudioCache::notify(void* cookie, int msg, int ext1, int
p->mSignal.signal();
}
}; // namespace android
} // namespace android

View File

@@ -23,15 +23,14 @@
#include <utils/List.h>
#include <utils/Errors.h>
#include <utils/KeyedVector.h>
#include <utils/SortedVector.h>
#include <utils/Vector.h>
#include <ui/SurfaceComposerClient.h>
#include <media/IMediaPlayerService.h>
#include <media/MediaPlayerInterface.h>
#include <media/Metadata.h>
namespace android {
typedef int32_t MetadataType;
class IMediaRecorder;
class IMediaMetadataRetriever;
@@ -239,12 +238,12 @@ private:
// @param type Of the metadata to be tested.
// @return true if the metadata should be dropped according to
// the filters.
bool shouldDropMetadata(MetadataType type) const;
bool shouldDropMetadata(media::Metadata::Type type) const;
// Add a new element to the set of metadata updated. Noop if
// the element exists already.
// @param type Of the metadata to be recorded.
void addNewMetadataUpdate(MetadataType type);
void addNewMetadataUpdate(media::Metadata::Type type);
mutable Mutex mLock;
sp<MediaPlayerBase> mPlayer;
@@ -257,14 +256,14 @@ private:
int32_t mConnId;
// Metadata filters.
SortedVector<int32_t> mMetadataAllow; // protected by mLock
SortedVector<int32_t> mMetadataDrop; // protected by mLock
media::Metadata::Filter mMetadataAllow; // protected by mLock
media::Metadata::Filter mMetadataDrop; // protected by mLock
// Metadata updated. For each MEDIA_INFO_METADATA_UPDATE
// notification we try to update mMetadataUpdated which is a
// set: no duplicate.
// getMetadata clears this set.
SortedVector<int32_t> mMetadataUpdated; // protected by mLock
media::Metadata::Filter mMetadataUpdated; // protected by mLock
#if CALLBACK_ANTAGONIZER
Antagonizer* mAntagonizer;

View File

@@ -49,10 +49,6 @@ public:
virtual status_t invoke(const Parcel& request, Parcel *reply) {
return INVALID_OPERATION;
}
virtual status_t getMetadata(const SortedVector<MetadataType>& ids,
Parcel *records) {
return INVALID_OPERATION;
}
private:
status_t createOutputTrack();

View File

@@ -144,7 +144,7 @@ status_t StagefrightPlayer::seekTo(int msec) {
if (mPlayer == NULL) {
return NO_INIT;
}
status_t err = mPlayer->seekTo((int64_t)msec * 1000);
sendEvent(MEDIA_SEEK_COMPLETE);
@@ -205,9 +205,4 @@ void StagefrightPlayer::setAudioSink(const sp<AudioSink> &audioSink) {
}
}
status_t StagefrightPlayer::getMetadata(
const SortedVector<MetadataType> &ids, Parcel *records) {
return INVALID_OPERATION;
}
} // namespace android

View File

@@ -48,9 +48,6 @@ public:
virtual status_t invoke(const Parcel &request, Parcel *reply);
virtual void setAudioSink(const sp<AudioSink> &audioSink);
virtual status_t getMetadata(
const SortedVector<MetadataType> &ids, Parcel *records);
private:
MediaPlayerImpl *mPlayer;

View File

@@ -94,10 +94,6 @@ class TestPlayerStub : public MediaPlayerInterface {
virtual status_t invoke(const android::Parcel& in, android::Parcel *out) {
return mPlayer->invoke(in, out);
}
virtual status_t getMetadata(const SortedVector<MetadataType>& ids,
Parcel *records) {
return INVALID_OPERATION;
}
// @return true if the current build is 'eng' or 'test' and the

View File

@@ -54,10 +54,6 @@ public:
virtual status_t setLooping(int loop);
virtual player_type playerType() { return VORBIS_PLAYER; }
virtual status_t invoke(const Parcel& request, Parcel *reply) {return INVALID_OPERATION;}
virtual status_t getMetadata(const SortedVector<MetadataType>& ids,
Parcel *records) {
return INVALID_OPERATION;
}
private:
status_t setdatasource(const char *path, int fd, int64_t offset, int64_t length);

View File

@@ -27,7 +27,6 @@
using android::INVALID_OPERATION;
using android::ISurface;
using android::MediaPlayerBase;
using android::MetadataType;
using android::OK;
using android::Parcel;
using android::SortedVector;
@@ -78,8 +77,6 @@ class Player: public MediaPlayerBase
virtual status_t setLooping(int loop) {return OK;}
virtual player_type playerType() {return TEST_PLAYER;}
virtual status_t invoke(const Parcel& request, Parcel *reply);
virtual status_t getMetadata(const SortedVector<MetadataType>& ids,
Parcel *records) {return INVALID_OPERATION;}
private:
// Take a request, copy it to the reply.