CMediaPlayer.c revision 39310fca2e30101fa6e5168da443581cc60c20bf
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#include <gui/SurfaceTextureClient.h>
32
33#include <fcntl.h>
34#endif
35
36#include "sles_allinclusive.h"
37
38#include <jni.h>
39
40#ifdef ANDROID
41using namespace android;
42
43#endif
44
45
46XAresult CMediaPlayer_Realize(void *self, XAboolean async)
47{
48    CMediaPlayer *thiz = (CMediaPlayer *) self;
49
50    XAresult result = XA_RESULT_SUCCESS;
51
52#ifdef ANDROID
53    // realize player
54    result = android_Player_realize(thiz, async);
55    if (XA_RESULT_SUCCESS == result) {
56
57        // if there is a video sink
58        if (XA_DATALOCATOR_NATIVEDISPLAY ==
59                thiz->mImageVideoSink.mLocator.mLocatorType) {
60            ANativeWindow *nativeWindow = (ANativeWindow *)
61                    thiz->mImageVideoSink.mLocator.mNativeDisplay.hWindow;
62            // we already verified earlier that hWindow is non-NULL
63            assert(nativeWindow != NULL);
64            int err;
65            int value;
66            // this could crash if app passes in a bad parameter, but that's OK
67            err = (*nativeWindow->query)(nativeWindow, NATIVE_WINDOW_CONCRETE_TYPE, &value);
68            if (0 != err) {
69                SL_LOGE("Query NATIVE_WINDOW_CONCRETE_TYPE on ANativeWindow * %p failed; "
70                        "errno %d", nativeWindow, err);
71            } else {
72                android::GenericMediaPlayer* avp =
73                        static_cast<android::GenericMediaPlayer *>(thiz->mAVPlayer.get());
74                switch (value) {
75                case NATIVE_WINDOW_SURFACE: {                // Surface
76                    sp<Surface> nativeSurface(static_cast<Surface *>(nativeWindow));
77                    result = android_Player_setVideoSurface(avp, nativeSurface);
78                    } break;
79                case NATIVE_WINDOW_SURFACE_TEXTURE_CLIENT: { // SurfaceTextureClient
80                    sp<SurfaceTextureClient> surfaceTextureClient(
81                            static_cast<SurfaceTextureClient *>(nativeWindow));
82                    sp<ISurfaceTexture> nativeSurfaceTexture(
83                            surfaceTextureClient->getISurfaceTexture());
84                    result = android_Player_setVideoSurfaceTexture(avp, nativeSurfaceTexture);
85                    } break;
86                case NATIVE_WINDOW_FRAMEBUFFER:              // FramebufferNativeWindow
87                    // fall through
88                default:
89                    SL_LOGE("ANativeWindow * %p has unknown or unsupported concrete type %d",
90                            nativeWindow, value);
91                    break;
92                }
93            }
94        }
95    }
96#endif
97
98    return result;
99}
100
101
102XAresult CMediaPlayer_Resume(void *self, XAboolean async)
103{
104    return XA_RESULT_SUCCESS;
105}
106
107
108/** \brief Hook called by Object::Destroy when a media player is destroyed */
109
110void CMediaPlayer_Destroy(void *self)
111{
112    CMediaPlayer *thiz = (CMediaPlayer *) self;
113    freeDataLocatorFormat(&thiz->mDataSource);
114    freeDataLocatorFormat(&thiz->mBankSource);
115    freeDataLocatorFormat(&thiz->mAudioSink);
116    freeDataLocatorFormat(&thiz->mImageVideoSink);
117    freeDataLocatorFormat(&thiz->mVibraSink);
118    freeDataLocatorFormat(&thiz->mLEDArraySink);
119#ifdef ANDROID
120    android_Player_destroy(thiz);
121#endif
122}
123
124
125predestroy_t CMediaPlayer_PreDestroy(void *self)
126{
127    return predestroy_ok;
128}
129