native-media-jni.c revision 97bdbe13fc48640babe6c1ce270660476f04c3df
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <assert.h>
18#include <jni.h>
19#include <pthread.h>
20#include <string.h>
21#define LOG_TAG "NativeMedia"
22#include <utils/Log.h>
23#include "OMXAL/OpenMAXAL.h"
24
25// engine interfaces
26static XAObjectItf engineObject = NULL;
27static XAEngineItf engineEngine;
28
29// output mix interfaces
30static XAObjectItf outputMixObject = NULL;
31
32// streaming media player interfaces
33static XAObjectItf streamingPlayerObject = NULL;
34static XAPlayItf streamingPlayerPlay;
35
36// cached surface
37static jobject theSurface;
38static JNIEnv *theEnv;
39
40// create the engine and output mix objects
41void Java_com_example_nativemedia_NativeMedia_createEngine(JNIEnv* env, jclass clazz)
42{
43    XAresult result;
44
45    // create engine
46    result = xaCreateEngine(&engineObject, 0, NULL, 0, NULL, NULL);
47    assert(XA_RESULT_SUCCESS == result);
48
49    // realize the engine
50    result = (*engineObject)->Realize(engineObject, XA_BOOLEAN_FALSE);
51    assert(XA_RESULT_SUCCESS == result);
52
53    // get the engine interface, which is needed in order to create other objects
54    result = (*engineObject)->GetInterface(engineObject, XA_IID_ENGINE, &engineEngine);
55    assert(XA_RESULT_SUCCESS == result);
56
57    // create output mix
58    result = (*engineEngine)->CreateOutputMix(engineEngine, &outputMixObject, 0, NULL, NULL);
59    assert(XA_RESULT_SUCCESS == result);
60
61    // realize the output mix
62    result = (*outputMixObject)->Realize(outputMixObject, XA_BOOLEAN_FALSE);
63    assert(XA_RESULT_SUCCESS == result);
64
65}
66
67
68// create streaming media player
69jboolean Java_com_example_nativemedia_NativeMedia_createStreamingMediaPlayer(JNIEnv* env,
70        jclass clazz, jstring filename)
71{
72    XAresult result;
73
74    // convert Java string to UTF-8
75    const char *utf8 = (*env)->GetStringUTFChars(env, filename, NULL);
76    assert(NULL != utf8);
77
78    // configure audio source
79    XADataLocator_URI loc_uri = {XA_DATALOCATOR_URI, (XAchar *) utf8};
80    XADataFormat_MIME format_mime = {XA_DATAFORMAT_MIME, NULL, XA_CONTAINERTYPE_UNSPECIFIED};
81    XADataSource dataSrc = {&loc_uri, &format_mime};
82
83    // configure audio sink
84    XADataLocator_OutputMix loc_outmix = {XA_DATALOCATOR_OUTPUTMIX, outputMixObject};
85    XADataSink audioSnk = {&loc_outmix, NULL};
86
87    // configure image video sink
88    XADataLocator_NativeDisplay loc_nd = {XA_DATALOCATOR_NATIVEDISPLAY, theSurface, theEnv};
89    XADataSink imageVideoSink = {&loc_nd, NULL};
90
91    // create media player
92    result = (*engineEngine)->CreateMediaPlayer(engineEngine, &streamingPlayerObject, &dataSrc,
93            NULL, &audioSnk, &imageVideoSink, NULL, NULL, 0, NULL, NULL);
94    assert(XA_RESULT_SUCCESS == result);
95
96    // release the Java string and UTF-8
97    (*env)->ReleaseStringUTFChars(env, filename, utf8);
98
99    // realize the player
100    result = (*streamingPlayerObject)->Realize(streamingPlayerObject, XA_BOOLEAN_FALSE);
101    assert(XA_RESULT_SUCCESS == result);
102
103    // get the play interface
104    result = (*streamingPlayerObject)->GetInterface(streamingPlayerObject, XA_IID_PLAY,
105            &streamingPlayerPlay);
106    assert(XA_RESULT_SUCCESS == result);
107
108    return JNI_TRUE;
109}
110
111
112// set the playing state for the streaming media player
113void Java_com_example_nativemedia_NativeMedia_setPlayingStreamingMediaPlayer(JNIEnv* env,
114        jclass clazz, jboolean isPlaying)
115{
116    XAresult result;
117
118    // make sure the streaming media player was created
119    if (NULL != streamingPlayerPlay) {
120
121        // set the player's state
122        result = (*streamingPlayerPlay)->SetPlayState(streamingPlayerPlay, isPlaying ?
123            XA_PLAYSTATE_PLAYING : XA_PLAYSTATE_PAUSED);
124        assert(XA_RESULT_SUCCESS == result);
125
126    }
127
128}
129
130
131// shut down the native media system
132void Java_com_example_nativemedia_NativeMedia_shutdown(JNIEnv* env, jclass clazz)
133{
134
135    // destroy streaming media player object, and invalidate all associated interfaces
136    if (streamingPlayerObject != NULL) {
137        (*streamingPlayerObject)->Destroy(streamingPlayerObject);
138        streamingPlayerObject = NULL;
139        streamingPlayerPlay = NULL;
140    }
141
142    // destroy output mix object, and invalidate all associated interfaces
143    if (outputMixObject != NULL) {
144        (*outputMixObject)->Destroy(outputMixObject);
145        outputMixObject = NULL;
146    }
147
148    // destroy engine object, and invalidate all associated interfaces
149    if (engineObject != NULL) {
150        (*engineObject)->Destroy(engineObject);
151        engineObject = NULL;
152        engineEngine = NULL;
153    }
154
155}
156
157
158// set the surface
159void Java_com_example_nativemedia_NativeMedia_setSurface(JNIEnv *env, jclass clazz, jobject surface)
160{
161    theEnv = env;
162    theSurface = surface;
163}
164