CMediaPlayer.c revision ad1ab1d13a9b043202b9d5cdc1d8c4ef66cbbca8
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/** \file CMediaPlayer.c MediaPlayer class */
18
19#ifdef ANDROID
20
21#include <binder/ProcessState.h>
22
23#include <media/IStreamSource.h>
24#include <media/mediaplayer.h>
25#include <media/stagefright/foundation/ADebug.h>
26
27#include <binder/IServiceManager.h>
28#include <media/IMediaPlayerService.h>
29#include <surfaceflinger/ISurfaceComposer.h>
30#include <surfaceflinger/SurfaceComposerClient.h>
31
32#include <fcntl.h>
33#endif
34
35#include "sles_allinclusive.h"
36
37#include <jni.h>
38
39#ifdef ANDROID
40using namespace android;
41
42#endif
43
44
45XAresult CMediaPlayer_Realize(void *self, XAboolean async)
46{
47    CMediaPlayer *thiz = (CMediaPlayer *) self;
48
49    XAresult result = XA_RESULT_SUCCESS;
50
51#ifdef ANDROID
52    // realize player
53    result = android_Player_realize(thiz, async);
54    if (XA_RESULT_SUCCESS == result) {
55
56        // if there is a video sink
57        if (XA_DATALOCATOR_NATIVEDISPLAY ==
58                thiz->mImageVideoSink.mLocator.mLocatorType) {
59            JNIEnv *env = (JNIEnv *) thiz->mImageVideoSink.mLocator.mNativeDisplay.hDisplay;
60            if (env != NULL) {
61                // FIXME this is a temporary hack because ANativeWindow is not Binderable yet
62                jobject object = (jobject) thiz->mImageVideoSink.mLocator.mNativeDisplay.hWindow;
63                assert(object != NULL);
64                jclass surfaceClass = env->FindClass("android/view/Surface");
65                jclass surfaceTextureClass = env->FindClass("android/graphics/SurfaceTexture");
66                jclass objectClass = env->GetObjectClass(object);
67                if (thiz->mAVPlayer != 0) {
68                    // initialize display surface
69                    android::GenericMediaPlayer* avp =
70                            (android::GenericMediaPlayer*)(thiz->mAVPlayer.get());
71                    if (objectClass == surfaceClass) {
72                        sp<Surface> nativeSurface((Surface *) env->GetIntField(object,
73                                env->GetFieldID(surfaceClass, "mNativeSurface", "I")));
74                        result = android_Player_setVideoSurface(avp, nativeSurface);
75                    } else if (objectClass == surfaceTextureClass) {
76                        sp<ISurfaceTexture> nativeSurfaceTexture((ISurfaceTexture *)
77                                env->GetIntField(object, env->GetFieldID(surfaceTextureClass,
78                                "mSurfaceTexture", "I")));
79                        result = android_Player_setVideoSurfaceTexture(avp, nativeSurfaceTexture);
80                    }
81                }
82            } else {
83                ANativeWindow *nativeWindow = (ANativeWindow *)
84                        thiz->mImageVideoSink.mLocator.mNativeDisplay.hWindow;
85                assert(nativeWindow != NULL);
86                // FIXME here is where to implement ANativeWindow support
87            }
88        }
89    }
90#endif
91
92    return result;
93}
94
95
96XAresult CMediaPlayer_Resume(void *self, XAboolean async)
97{
98    return XA_RESULT_SUCCESS;
99}
100
101
102/** \brief Hook called by Object::Destroy when a media player is destroyed */
103
104void CMediaPlayer_Destroy(void *self)
105{
106    CMediaPlayer *thiz = (CMediaPlayer *) self;
107    freeDataLocatorFormat(&thiz->mDataSource);
108    freeDataLocatorFormat(&thiz->mBankSource);
109    freeDataLocatorFormat(&thiz->mAudioSink);
110    freeDataLocatorFormat(&thiz->mImageVideoSink);
111    freeDataLocatorFormat(&thiz->mVibraSink);
112    freeDataLocatorFormat(&thiz->mLEDArraySink);
113#ifdef ANDROID
114    android_Player_destroy(thiz);
115#endif
116}
117
118
119predestroy_t CMediaPlayer_PreDestroy(void *self)
120{
121    return predestroy_ok;
122}
123