|
| 1 | +/* |
| 2 | + * Copyright (c) 2018 Vinicius Tona |
| 3 | + * Copyright (c) 2018 Antonio Morales |
| 4 | + * |
| 5 | + * This file is part of gstreamer-java. |
| 6 | + * |
| 7 | + * This code is free software: you can redistribute it and/or modify it under the terms of the GNU |
| 8 | + * Lesser General Public License version 3 only, as published by the Free Software Foundation. |
| 9 | + * |
| 10 | + * This code is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without |
| 11 | + * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | + * Lesser General Public License version 3 for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU Lesser General Public License version 3 along with |
| 15 | + * this work. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.freedesktop.gstreamer; |
| 19 | + |
| 20 | +import static org.freedesktop.gstreamer.lowlevel.GstPromiseAPI.GSTPROMISE_API; |
| 21 | + |
| 22 | +import org.freedesktop.gstreamer.lowlevel.GstAPI.GstCallback; |
| 23 | + |
| 24 | +import com.sun.jna.Pointer; |
| 25 | + |
| 26 | +public class Promise extends MiniObject { |
| 27 | + public static final String GTYPE_NAME = "GstPromise"; |
| 28 | + |
| 29 | + public static interface PROMISE_CHANGE { |
| 30 | + /** |
| 31 | + * Called whenever the state of the promise is changed from PENDING to any other {@link PromiseResult} |
| 32 | + * |
| 33 | + * @param promise the original promise that had the callback attached to |
| 34 | + */ |
| 35 | + public void onChange(Promise promise); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Creates a new instance of Promise. This constructor is used internally. |
| 40 | + * |
| 41 | + * @param init internal initialization data. |
| 42 | + */ |
| 43 | + public Promise(final Initializer init) { |
| 44 | + super(init); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Creates a new instance of promise |
| 49 | + */ |
| 50 | + public Promise() { |
| 51 | + this(initializer(GSTPROMISE_API.ptr_gst_promise_new())); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Creates a new instance of promise with a callback attached. |
| 56 | + * |
| 57 | + * @param listerner Listener to be called whenever the state of a {@link Promise} is changed |
| 58 | + */ |
| 59 | + public Promise(final PROMISE_CHANGE listener) { |
| 60 | + this(new Initializer(GSTPROMISE_API.ptr_gst_promise_new_with_change_func(new GstCallback() { |
| 61 | + @SuppressWarnings("unused") |
| 62 | + public void callback(Promise promise, Pointer userData) { |
| 63 | + listener.onChange(promise); |
| 64 | + } |
| 65 | + }), false, false)); |
| 66 | + } |
| 67 | + |
| 68 | + protected static Initializer initializer(final Pointer ptr) { |
| 69 | + return new Initializer(ptr, false, true); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Wait for the promise to move out of the PENDING {@link PromiseResult} state. |
| 74 | + * If the promise is not in PENDING then it will immediately return. |
| 75 | + * |
| 76 | + * @return the {@link PromiseResult} of the promise. |
| 77 | + */ |
| 78 | + public PromiseResult waitResult() { |
| 79 | + return GSTPROMISE_API.gst_promise_wait(this); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Set a reply on the promise. |
| 84 | + * |
| 85 | + * Will wake up any waiters on the promise with the REPLIED {@link PromiseResult} state. |
| 86 | + * If the promise has already been interrupted than the replied will not be visible to any waiters |
| 87 | + * |
| 88 | + * @param structure the {@link Structure} to reply the promise with |
| 89 | + */ |
| 90 | + public void reply(final Structure structure) { |
| 91 | + GSTPROMISE_API.gst_promise_reply(this, structure); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Interrupt waiting for the result of the prommise. |
| 96 | + * |
| 97 | + * Any waiters on the promise will receive the INTERRUPTED {@link PromiseResult} state. |
| 98 | + */ |
| 99 | + public void interrupt() { |
| 100 | + GSTPROMISE_API.gst_promise_interrupt(this); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Expire a promise. |
| 105 | + * |
| 106 | + * Any waiters on the promise will recieved the EXPIRED {@link PromiseResult} state. |
| 107 | + */ |
| 108 | + public void expire() { |
| 109 | + GSTPROMISE_API.gst_promise_expire(this); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Retrieve the reply set on the promise. |
| 114 | + * |
| 115 | + * The state of the promise must be in the REPLIED {@link PromiseResult} state. |
| 116 | + * The return structure is owned by the promise and thus cannot be modified. |
| 117 | + * |
| 118 | + * @return the {@link Structure} set on the promise reply. |
| 119 | + */ |
| 120 | + public Structure getReply() { |
| 121 | + return Structure.objectFor(GSTPROMISE_API.ptr_gst_promise_get_reply(this), false, false); |
| 122 | + } |
| 123 | +} |
0 commit comments