Merging gst-examples
[platform/upstream/gstreamer.git] / playback / player / android / app / src / main / java / org / freedesktop / gstreamer / Player.java
1 /* GStreamer
2  *
3  * Copyright (C) 2014-2015 Sebastian Dröge <sebastian@centricular.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 package org.freedesktop.gstreamer;
22
23 import java.io.Closeable;
24 import android.view.Surface;
25 import android.content.Context;
26 import org.freedesktop.gstreamer.GStreamer;
27
28 public class Player implements Closeable {
29     private static native void nativeClassInit();
30     public static void init(Context context) throws Exception {
31         System.loadLibrary("gstreamer_android");
32         GStreamer.init(context);
33
34         System.loadLibrary("gstplayer");
35         nativeClassInit();
36     }
37
38     private long native_player;
39     private native void nativeNew();
40     public Player() {
41         nativeNew();
42     }
43
44     private native void nativeFree();
45     @Override
46     public void close() {
47         nativeFree();
48     }
49
50     private native void nativePlay();
51     public void play() {
52         nativePlay();
53     }
54
55     private native void nativePause();
56     public void pause() {
57         nativePause();
58     }
59
60     private native void nativeStop();
61     public void stop() {
62         nativeStop();
63     }
64
65     private native void nativeSeek(long position);
66     public void seek(long position) {
67         nativeSeek(position);
68     }
69
70     private native String nativeGetUri();
71     public String getUri() {
72         return nativeGetUri();
73     }
74
75     private native void nativeSetUri(String uri);
76     public void setUri(String uri) {
77         nativeSetUri(uri);
78     }
79
80     private native long nativeGetPosition();
81     public long getPosition() {
82         return nativeGetPosition();
83     }
84
85     private native long nativeGetDuration();
86     public long getDuration() {
87         return nativeGetDuration();
88     }
89
90     private native double nativeGetVolume();
91     public double getVolume() {
92         return nativeGetVolume();
93     }
94
95     private native void nativeSetVolume(double volume);
96     public void setVolume(double volume) {
97         nativeSetVolume(volume);
98     }
99
100     private native boolean nativeGetMute();
101     public boolean getMute() {
102         return nativeGetMute();
103     }
104
105     private native void nativeSetMute(boolean mute);
106     public void setMute(boolean mute) {
107         nativeSetMute(mute);
108     }
109
110     private Surface surface;
111     private native void nativeSetSurface(Surface surface);
112     public void setSurface(Surface surface) {
113         this.surface = surface;
114         nativeSetSurface(surface);
115     }
116
117     public Surface getSurface() {
118         return surface;
119     }
120
121     public static interface PositionUpdatedListener {
122         abstract void positionUpdated(Player player, long position);
123     }
124
125     private PositionUpdatedListener positionUpdatedListener;
126     public void setPositionUpdatedListener(PositionUpdatedListener listener) {
127         positionUpdatedListener = listener;
128     }
129
130     private void onPositionUpdated(long position) {
131         if (positionUpdatedListener != null) {
132             positionUpdatedListener.positionUpdated(this, position);
133         }
134     }
135
136     public static interface DurationChangedListener {
137         abstract void durationChanged(Player player, long duration);
138     }
139
140     private DurationChangedListener durationChangedListener;
141     public void setDurationChangedListener(DurationChangedListener listener) {
142         durationChangedListener = listener;
143     }
144
145     private void onDurationChanged(long duration) {
146         if (durationChangedListener != null) {
147             durationChangedListener.durationChanged(this, duration);
148         }
149     }
150
151     private static final State[] stateMap = {State.STOPPED, State.BUFFERING, State.PAUSED, State.PLAYING};
152     public enum State {
153         STOPPED,
154         BUFFERING,
155         PAUSED,
156         PLAYING
157     }
158
159     public static interface StateChangedListener {
160         abstract void stateChanged(Player player, State state);
161     }
162
163     private StateChangedListener stateChangedListener;
164     public void setStateChangedListener(StateChangedListener listener) {
165         stateChangedListener = listener;
166     }
167
168     private void onStateChanged(int stateIdx) {
169         if (stateChangedListener != null) {
170             State state = stateMap[stateIdx];
171             stateChangedListener.stateChanged(this, state);
172         }
173     }
174
175     public static interface BufferingListener {
176         abstract void buffering(Player player, int percent);
177     }
178
179     private BufferingListener bufferingListener;
180     public void setBufferingListener(BufferingListener listener) {
181         bufferingListener = listener;
182     }
183
184     private void onBuffering(int percent) {
185         if (bufferingListener != null) {
186             bufferingListener.buffering(this, percent);
187         }
188     }
189
190     public static interface EndOfStreamListener {
191         abstract void endOfStream(Player player);
192     }
193
194     private EndOfStreamListener endOfStreamListener;
195     public void setEndOfStreamListener(EndOfStreamListener listener) {
196         endOfStreamListener = listener;
197     }
198
199     private void onEndOfStream() {
200         if (endOfStreamListener != null) {
201             endOfStreamListener.endOfStream(this);
202         }
203     }
204
205     // Keep these in sync with gstplayer.h
206     private static final Error[] errorMap = {Error.FAILED};
207     public enum Error {
208         FAILED
209     }
210
211     public static interface ErrorListener {
212         abstract void error(Player player, Error error, String errorMessage);
213     }
214
215     private ErrorListener errorListener;
216     public void setErrorListener(ErrorListener listener) {
217         errorListener = listener;
218     }
219
220     private void onError(int errorCode, String errorMessage) {
221         if (errorListener != null) {
222             Error error = errorMap[errorCode];
223             errorListener.error(this, error, errorMessage);
224         }
225     }
226
227     public static interface VideoDimensionsChangedListener {
228         abstract void videoDimensionsChanged(Player player, int width, int height);
229     }
230
231     private VideoDimensionsChangedListener videoDimensionsChangedListener;
232     public void setVideoDimensionsChangedListener(VideoDimensionsChangedListener listener) {
233         videoDimensionsChangedListener = listener;
234     }
235
236     private void onVideoDimensionsChanged(int width, int height) {
237         if (videoDimensionsChangedListener != null) {
238             videoDimensionsChangedListener.videoDimensionsChanged(this, width, height);
239         }
240     }
241 }