I created my own version of Android's MediaMetadataRetriever using the source code for MediaMetadataRetriever.java as the basis. My version uses FFmpeg to retrieve the metadata. This approach works however it relies on static variables in the C code to retain state in between JNI calls. This means I can only use one instance of this class at a time or the state can get corrupted. The two Java functions are defined as follows:
public class MediaMetadataRetriever
{
static {
System.loadLibrary("metadata_retriever_jni");
}
public MediaMetadataRetriever() {
}
public native void setDataSource(String path) throws IllegalArgumentException;
public native String extractMetadata(String key);
}
The corresponding C (JNI) code code is:
const char *TAG = "Java_com_example_metadataexample_MediaMetadataRetriever";
static AVFormatContext *pFormatCtx = NULL;
JNIEXPORT void JNICALL
Java_com_example_metadataexample_MediaMetadataRetriever_setDataSource(JNIEnv *env, jclass obj, jstring jpath) {
if (pFormatCtx) {
avformat_close_input(&pFormatCtx);
}
char duration[30] = "0";
const char *uri;
uri = (*env)->GetStringUTFChars(env, jpath, NULL);
if (avformat_open_input(&pFormatCtx, uri, NULL, NULL) != 0) {
__android_log_write(ANDROID_LOG_INFO, TAG, "Metadata could not be retrieved");
(*env)->ReleaseStringUTFChars(env, jpath, uri);
jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
return;
}
if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {
__android_log_write(ANDROID_LOG_INFO, TAG, "Metadata could not be retrieved");
avformat_close_input(&pFormatCtx);
(*env)->ReleaseStringUTFChars(env, jpath, uri);
jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
return;
}
(*env)->ReleaseStringUTFChars(env, jpath, uri);
}
JNIEXPORT jstring JNICALL
Java_com_example_metadataexample_MediaMetadataRetriever_extractMetadata(JNIEnv *env, jclass obj, jstring jkey) {
const char *key;
jstring value = NULL;
key = (*env)->GetStringUTFChars(env, jkey, NULL) ;
if (!pFormatCtx) {
goto fail;
}
if (key) {
if (av_dict_get(pFormatCtx->metadata, key, NULL, AV_DICT_IGNORE_SUFFIX)) {
value = (*env)->NewStringUTF(env, av_dict_get(pFormatCtx->metadata, key, NULL, AV_DICT_IGNORE_SUFFIX)->value);
}
}
fail:
(*env)->ReleaseStringUTFChars(env, jkey, key);
return value;
}
Sample usage that outlines my issue would be:
MediaMetadataRetriever mmr = new MediaMetadataRetriever();
mmr.setDataSource("one.mp3");
MediaMetadataRetriever mmr2 = new MediaMetadataRetriever();
// This line resets the data source to two.mp3
mmr2.setDataSource("two.mp3");
// should retrieve the artist from one.mp3 but retrieves it from two.mp3 due to the static
// variable being reset in the previous statement
String artist = mmr.extractMetadata(MediaMetadataRetriever.ARTIST);
Can someone explain how I would structure this code so I could use multiple instances of MediaMetadataRetriever without them interfering with one another? I don't want to switch the code to C++ and I'm fairly certain I don't need to modify MediaMetadataRetriever.java since this code is taken line-for-line from the Android framework (which allows multiple instances, see example below). It appears I need to re-structure the C code but I'm unsure how to retain state across JNI calls without using a static variable. Thanks in advance.
File file1 = new File(Environment.getExternalStorageDirectory(), "Music/one.mp3");
File file2 = new File(Environment.getExternalStorageDirectory(), "Music/two.mp3");
android.media.MediaMetadataRetriever mmr = new android.media.MediaMetadataRetriever();
mmr.setDataSource(file1.toString());
android.media.MediaMetadataRetriever mmr2 = new android.media.MediaMetadataRetriever();
mmr2.setDataSource(file2.toString());
// Returns the artist of one.mp3, not two.mp3, as expected. This is the expected behavior
// and confirms that multiple instances of MediaMetadataRetriever can be used simultaneously
mmr.extractMetadata(android.media.MediaMetadataRetriever.METADATA_KEY_ARTIST));