ijksdl_vout_android_nativewindow.c 3.5 KB
Newer Older
Z
Zhang Rui 已提交
1
/*****************************************************************************
Z
Zhang Rui 已提交
2
 * ijksdl_vout_android_nativewindow.c
Z
Zhang Rui 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *****************************************************************************
 *
 * copyright (c) 2013 Zhang Rui <bbcallen@gmail.com>
 *
 * This file is part of ijkPlayer.
 *
 * ijkPlayer is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * ijkPlayer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with ijkPlayer; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

Z
Zhang Rui 已提交
24
#include "ijksdl_vout_android_nativewindow.h"
Z
Zhang Rui 已提交
25

Z
Zhang Rui 已提交
26 27
#include <assert.h>
#include <android/native_window.h>
28
#include "ijkutil/ijkutil.h"
Z
Zhang Rui 已提交
29 30
#include "../ijksdl_vout.h"
#include "../ijksdl_vout_internal.h"
31
#include "../ffmpeg/ijksdl_inc_ffmpeg.h"
32
#include "android_nativewindow.h"
Z
Zhang Rui 已提交
33 34

typedef struct SDL_VoutSurface_Opaque {
35
    SDL_Vout *vout;
Z
Zhang Rui 已提交
36 37
} SDL_VoutSurface_Opaque;

38 39 40
typedef struct SDL_Vout_Opaque {
    ANativeWindow *native_window;
} SDL_Vout_Opaque;
Z
Zhang Rui 已提交
41

Z
Zhang Rui 已提交
42
static void vout_free_l(SDL_Vout *vout)
Z
Zhang Rui 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56
{
    if (!vout)
        return;

    SDL_Vout_Opaque *opaque = vout->opaque;
    if (opaque) {
        if (opaque->native_window) {
            ANativeWindow_release(opaque->native_window);
        }
    }

    SDL_Vout_FreeInternal(vout);
}

57 58 59 60 61
static int voud_display_overlay_l(SDL_Vout *vout, SDL_VoutOverlay *overlay)
{
    SDL_Vout_Opaque *opaque = vout->opaque;
    ANativeWindow *native_window = opaque->native_window;

Z
Zhang Rui 已提交
62 63
    if (!native_window) {
        ALOGE("voud_display_overlay_l: NULL native_window");
64
        return -1;
Z
Zhang Rui 已提交
65
    }
66

Z
Zhang Rui 已提交
67 68
    if (!overlay) {
        ALOGE("voud_display_overlay_l: NULL overlay");
69
        return -1;
Z
Zhang Rui 已提交
70 71 72 73 74 75
    }

    if (overlay->w <= 0 || overlay->h <= 0) {
        ALOGE("voud_display_overlay_l: invalid overlay dimensions(%d, %d)", overlay->w, overlay->h);
        return -1;
    }
76

77
    return sdl_native_window_display_l(native_window, overlay);
78 79
}

80 81
static int voud_display_overlay(SDL_Vout *vout, SDL_VoutOverlay *overlay)
{
82 83 84 85
    SDL_LockMutex(vout->mutex);
    int retval = voud_display_overlay_l(vout, overlay);
    SDL_UnlockMutex(vout->mutex);
    return retval;
86 87
}

Z
Zhang Rui 已提交
88
SDL_Vout *SDL_VoutAndroid_CreateForANativeWindow()
Z
Zhang Rui 已提交
89
{
90
    SDL_Vout *vout = SDL_Vout_CreateInternal(sizeof(SDL_Vout_Opaque));
Z
Zhang Rui 已提交
91 92 93
    if (!vout)
        return NULL;

94
    SDL_Vout_Opaque *opaque = vout->opaque;
Z
Zhang Rui 已提交
95
    opaque->native_window = NULL;
96

Z
Zhang Rui 已提交
97
    vout->free_l = vout_free_l;
98
    vout->display_overlay = voud_display_overlay;
Z
Zhang Rui 已提交
99 100 101 102

    return vout;
}

Z
Zhang Rui 已提交
103
static void SDL_VoutAndroid_SetNativeWindow_l(SDL_Vout *vout, ANativeWindow *native_window)
Z
Zhang Rui 已提交
104 105 106 107 108 109 110 111 112
{
    SDL_Vout_Opaque *opaque = vout->opaque;

    if (opaque->native_window == native_window)
        return;

    if (opaque->native_window)
        ANativeWindow_release(opaque->native_window);

113 114 115
    if (native_window)
        ANativeWindow_acquire(native_window);

Z
Zhang Rui 已提交
116 117 118
    opaque->native_window = native_window;
}

Z
Zhang Rui 已提交
119
void SDL_VoutAndroid_SetNativeWindow(SDL_Vout *vout, ANativeWindow *native_window)
Z
Zhang Rui 已提交
120 121 122 123 124
{
    SDL_LockMutex(vout->mutex);
    SDL_VoutAndroid_SetNativeWindow_l(vout, native_window);
    SDL_UnlockMutex(vout->mutex);
}