modules/media/src/main/native/jfxmedia/platform/osx/OSXMediaPlayer.mm

Print this page
rev 7591 : RT-38074: [macosx] Separate QTKit platform code from core media code so it can be removed for MAS
Reviewed-by:
   1 /*
   2  * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #import "OSXMediaPlayer.h"

  27 #import "com_sun_media_jfxmediaimpl_platform_osx_OSXMediaPlayer.h"
  28 #import <Utils/JObjectPeers.h>
  29 #import <Utils/JavaUtils.h>
  30 #import <CoreAudio/CoreAudio.h>
  31 #import <jni/Logger.h>
  32 
  33 #import "QTKMediaPlayer.h"
  34 
  35 #import <objc/runtime.h>
  36 
  37 #define USE_WEAK_REFS 0
  38 
  39 // Don't access directly, use the OSXMediaPlayer static methods to ensure thread safe access
  40 static JObjectPeers *gMediaPlayerPeers = nil;

  41 
  42 @implementation OSXMediaPlayer
  43 
  44 + (void) initialize
  45 {
  46     gMediaPlayerPeers = [[JObjectPeers alloc] init];
  47 }
  48 
  49 + (OSXMediaPlayer*) peerForPlayer:(jobject)javaPlayer andEnv:(JNIEnv*)javaEnv
  50 {
  51     return [gMediaPlayerPeers peerForJObject:javaPlayer javaEnv:javaEnv];
  52 }
  53 
  54 + (void) setPeer:(OSXMediaPlayer*)player forJavaPlayer:(jobject)javaPlayer andEnv:(JNIEnv*)javaEnv
  55 {
  56     [gMediaPlayerPeers setPeer:player forJObject:javaPlayer javaEnv:javaEnv];
  57 }
  58 
  59 + (void) removePlayerPeers:(OSXMediaPlayer*)player
  60 {
  61     [gMediaPlayerPeers removePeer:player];
  62 }
  63 













  64 - (id) init
  65 {
  66     if ((self = [super init]) != nil) {
  67     }
  68     return self;
  69 }
  70 
  71 - (id) initWithURL:(NSURL *)source javaPlayer:(jobject)jp andEnv:(JNIEnv*)env eventHandler:(CJavaPlayerEventDispatcher*)hdlr
  72 {





  73     if ((self = [super init]) != nil) {
  74         movieURL = [source retain];
  75         
  76         env->GetJavaVM(&javaPlayerVM);
  77 #if USE_WEAK_REFS
  78         javaPlayer = env->NewWeakGlobalRef(jp);
  79 #else
  80         javaPlayer = env->NewGlobalRef(jp);
  81 #endif
  82         // set up the peer association
  83         [OSXMediaPlayer setPeer:self forJavaPlayer:javaPlayer andEnv:env];
  84         
  85         eventHandler = hdlr;
  86         
  87         // create the movie object
  88         player = [[QTKMediaPlayer alloc] initWithURL:movieURL eventHandler:eventHandler];
  89     }
  90     return self;
  91 }
  92 







  93 - (void) dealloc
  94 {
  95     [self dispose]; // just in case
  96     [movieURL release];
  97     [super dealloc];
  98 }
  99 
 100 - (void) dispose
 101 {
 102     @synchronized(self) {
 103         [player dispose];
 104         [player release];
 105         player = nil;
 106     
 107         if (eventHandler) {
 108             eventHandler->Dispose();
 109             delete eventHandler;
 110         }
 111         eventHandler = NULL;
 112         


   1 /*
   2  * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #import "OSXMediaPlayer.h"
  27 #import "OSXPlayerProtocol.h"
  28 #import "com_sun_media_jfxmediaimpl_platform_osx_OSXMediaPlayer.h"
  29 #import <Utils/JObjectPeers.h>
  30 #import <Utils/JavaUtils.h>
  31 #import <CoreAudio/CoreAudio.h>
  32 #import <jni/Logger.h>
  33 


  34 #import <objc/runtime.h>
  35 
  36 #define USE_WEAK_REFS 0
  37 
  38 // Don't access directly, use the OSXMediaPlayer static methods to ensure thread safe access
  39 static JObjectPeers *gMediaPlayerPeers = nil;
  40 static Class gMediaPlayerClass = nil;
  41 
  42 @implementation OSXMediaPlayer
  43 
  44 + (void) initialize
  45 {
  46     gMediaPlayerPeers = [[JObjectPeers alloc] init];
  47 }
  48 
  49 + (OSXMediaPlayer*) peerForPlayer:(jobject)javaPlayer andEnv:(JNIEnv*)javaEnv
  50 {
  51     return [gMediaPlayerPeers peerForJObject:javaPlayer javaEnv:javaEnv];
  52 }
  53 
  54 + (void) setPeer:(OSXMediaPlayer*)player forJavaPlayer:(jobject)javaPlayer andEnv:(JNIEnv*)javaEnv
  55 {
  56     [gMediaPlayerPeers setPeer:player forJObject:javaPlayer javaEnv:javaEnv];
  57 }
  58 
  59 + (void) removePlayerPeers:(OSXMediaPlayer*)player
  60 {
  61     [gMediaPlayerPeers removePeer:player];
  62 }
  63 
  64 + (void) initPlayerPlatform
  65 {
  66     // Determine if we can use QTKMediaPlayer or not, without directly linking and pulling
  67     // in unwanted dependencies
  68     Class klass = objc_getClass("QTKMediaPlayer");
  69     if (klass) {
  70         // And make sure it conforms to the OSXPlayerProtocol
  71         if ([klass conformsToProtocol:@protocol(OSXPlayerProtocol)]) {
  72             gMediaPlayerClass = klass;
  73         }
  74     } // else we can't log yet, so fail silently
  75 }
  76 
  77 - (id) init
  78 {
  79     if ((self = [super init]) != nil) {
  80     }
  81     return self;
  82 }
  83 
  84 - (id) initWithURL:(NSURL *)source javaPlayer:(jobject)jp andEnv:(JNIEnv*)env eventHandler:(CJavaPlayerEventDispatcher*)hdlr
  85 {
  86     if (!gMediaPlayerClass) {
  87         // No player class available, abort
  88         return nil;
  89     }
  90 
  91     if ((self = [super init]) != nil) {
  92         movieURL = [source retain];
  93         
  94         env->GetJavaVM(&javaPlayerVM);
  95 #if USE_WEAK_REFS
  96         javaPlayer = env->NewWeakGlobalRef(jp);
  97 #else
  98         javaPlayer = env->NewGlobalRef(jp);
  99 #endif
 100         // set up the peer association
 101         [OSXMediaPlayer setPeer:self forJavaPlayer:javaPlayer andEnv:env];
 102         
 103         eventHandler = hdlr;
 104         
 105         // create the player object
 106         player = [[gMediaPlayerClass alloc] initWithURL:movieURL eventHandler:eventHandler];
 107     }
 108     return self;
 109 }
 110 
 111 - (id) initWithURL:(NSURL *)source eventHandler:(CJavaPlayerEventDispatcher*)hdlr
 112 {
 113     // stub initWithURL message to satisfy the protocol requirements, this should
 114     // never be called
 115     return nil;
 116 }
 117 
 118 - (void) dealloc
 119 {
 120     [self dispose]; // just in case
 121     [movieURL release];
 122     [super dealloc];
 123 }
 124 
 125 - (void) dispose
 126 {
 127     @synchronized(self) {
 128         [player dispose];
 129         [player release];
 130         player = nil;
 131     
 132         if (eventHandler) {
 133             eventHandler->Dispose();
 134             delete eventHandler;
 135         }
 136         eventHandler = NULL;
 137