Spec-Zone .ru
спецификации, руководства, описания, API
001/*
002 * Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.  Oracle designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Oracle in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
022 * or visit www.oracle.com if you need additional information or have any
023 * questions.
024 */
025
026package javafx.scene.media;
027
028import com.sun.media.jfxmedia.MediaError;
029
030/**
031 * A <code>MediaException</code> describes a runtime error condition in a {@link Media},
032 * {@link MediaPlayer} or {@link MediaView}.
033 *
034 * @see Media#onErrorProperty
035 * @see MediaView#onErrorProperty
036 */
037public final class MediaException extends RuntimeException {
038    /**
039     * Enumeration describing categories of errors. A number of different
040     * actual {@link Exception}s may belong to the same category.
041     */
042    public enum Type {
043        // FIXME: generate more descriptive messages
044        /**
045         * Indicates an error has occurred: the media appears to be
046         * invalid or corrupted.
047         */
048        MEDIA_CORRUPTED,
049        /**
050         * Indicates an error has occurred: although the media
051         * may exist, it is not accessible.
052         */
053        MEDIA_INACCESSIBLE,
054        /**
055         * Indicates an error has occurred: the media
056         * does not exist or is otherwise unavailable. This error may
057         * be the result of security settings preventing access when
058         * running in a browser.
059         */
060        MEDIA_UNAVAILABLE,
061        /**
062         * Indicates that the media has not been specified.
063         */
064        MEDIA_UNSPECIFIED,
065        /**
066         * Indicates that this media type is not supported by this platform.
067         */
068        MEDIA_UNSUPPORTED,
069        /**
070         * Indicates that an operation performed on the media is not
071         * supported by this platform.
072         */
073        OPERATION_UNSUPPORTED,
074        /**
075         * Indicates a playback error which does not fall into any of the other
076         * pre-defined categories.
077         */
078        PLAYBACK_ERROR,
079        /**
080         * Indicates an unrecoverable error which has resulted in halting playback.
081         */
082        PLAYBACK_HALTED,
083        /**
084         * Indicates an error has occurred for an unknown reason.
085         */
086        UNKNOWN
087    };
088
089    /**
090     * Map {@link MediaError} codes to {@link Type}s.
091     * @param errorCode Error code from implementation layer.
092     * @return API level error type.
093     */
094    static Type errorCodeToType(int errorCode) {
095        Type errorType;
096        
097        if(errorCode == MediaError.ERROR_LOCATOR_CONNECTION_LOST.code()) {
098            errorType = Type.MEDIA_INACCESSIBLE;
099        } else if(errorCode == MediaError.ERROR_GSTREAMER_SOURCEFILE_NONEXISTENT.code() ||
100                errorCode == MediaError.ERROR_GSTREAMER_SOURCEFILE_NONREGULAR.code()) {
101            errorType = Type.MEDIA_UNAVAILABLE;
102        } else if(errorCode == MediaError.ERROR_MEDIA_AUDIO_FORMAT_UNSUPPORTED.code() ||
103                errorCode == MediaError.ERROR_MEDIA_UNKNOWN_PIXEL_FORMAT.code() ||
104                errorCode == MediaError.ERROR_MEDIA_VIDEO_FORMAT_UNSUPPORTED.code() ||
105                errorCode == MediaError.ERROR_LOCATOR_CONTENT_TYPE_NULL.code() ||
106                errorCode == MediaError.ERROR_LOCATOR_UNSUPPORTED_MEDIA_FORMAT.code() ||
107                errorCode == MediaError.ERROR_LOCATOR_UNSUPPORTED_TYPE.code() ||
108                errorCode == MediaError.ERROR_GSTREAMER_UNSUPPORTED_PROTOCOL.code() ||
109                errorCode == MediaError.ERROR_MEDIA_MP3_FORMAT_UNSUPPORTED.code() ||
110                errorCode == MediaError.ERROR_MEDIA_AAC_FORMAT_UNSUPPORTED.code() ||
111                errorCode == MediaError.ERROR_MEDIA_H264_FORMAT_UNSUPPORTED.code() ||
112                errorCode == MediaError.ERROR_MEDIA_HLS_FORMAT_UNSUPPORTED.code()) {            
113            errorType = Type.MEDIA_UNSUPPORTED;
114        } else if(errorCode == MediaError.ERROR_MEDIA_CORRUPTED.code()) {
115            errorType = Type.MEDIA_CORRUPTED;
116        } else if((errorCode & MediaError.ERROR_BASE_GSTREAMER.code()) == MediaError.ERROR_BASE_GSTREAMER.code() ||
117                (errorCode & MediaError.ERROR_BASE_JNI.code()) == MediaError.ERROR_BASE_JNI.code()) {
118            errorType = Type.PLAYBACK_ERROR;
119        } else {
120            errorType = Type.UNKNOWN;
121        }
122
123        return errorType;
124    }
125
126    /**
127     * converts Java exceptions into mediaErrors
128     */
129    static MediaException exceptionToMediaException(Exception e) {
130        Type errType = Type.UNKNOWN;
131        // Set appropriate error code based on exception cause.
132        if (e.getCause() instanceof java.net.UnknownHostException) {
133            errType = Type.MEDIA_UNAVAILABLE;
134        } else if (e.getCause() instanceof java.lang.IllegalArgumentException) {
135            errType = Type.MEDIA_UNSUPPORTED;
136        } else if (e instanceof com.sun.media.jfxmedia.MediaException) {
137            com.sun.media.jfxmedia.MediaException me = (com.sun.media.jfxmedia.MediaException)e;
138            MediaError error = me.getMediaError();
139            if (error != null ) {
140                errType = errorCodeToType(error.code());
141            }
142        }
143
144        return new MediaException(errType, e);
145    }
146
147    static MediaException haltException(String message) {
148        return new MediaException(Type.PLAYBACK_HALTED, message);
149    }
150
151    static MediaException getMediaException(Object source, int errorCode, String message) {
152        // Get the error code description.
153        String errorDescription = MediaError.getFromCode(errorCode).description();
154        String exceptionMessage = "[" + source + "] " + message + ": " + errorDescription;
155
156        // We need to map some jfxMedia error codes to FX Media error codes.
157        Type errorType = errorCodeToType(errorCode);
158        return new MediaException(errorType, exceptionMessage);
159    }
160
161    MediaException(Type type, Throwable t) {
162        super(t);
163        this.type = type;
164    }
165
166    MediaException(Type type, String message, Throwable t) {
167        super(message, t);
168        this.type = type;
169    }
170
171    MediaException(Type type, String message) {
172        super(message);
173        this.type = type;
174    }
175    /**
176     * What caused this error.
177     */
178    private final Type type;
179    /**
180     * Retrieves the category into which this error falls.
181     * @return The type of this error
182     */
183    public Type getType() {
184        return type;
185    }
186    
187    /**
188     * Returns a string representation of this <code>MediaException</code> object.
189     * @return a string representation of this <code>MediaException</code> object.
190     */ 
191    @Override
192    public String toString() {
193        String errString = "MediaException: " + type;
194        if (getMessage() != null) errString += " : " + getMessage();
195        if (getCause() != null) errString += " : " + getCause();
196        return errString;
197    }
198};