wxr
2020-06-15 b8e94316e41eba72d927d5ca7d931b26139ee8ff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
/*****************************************************************************
 * LibVLC.java
 *****************************************************************************
 * Copyright © 2010-2013 VLC authors and VideoLAN
 *
 * This program 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.
 *
 * This program 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 this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/
 
package org.videolan.libvlc;
 
import java.util.ArrayList;
import java.util.Map;
 
import android.content.Context;
import android.os.Build;
import android.util.Log;
import android.view.Surface;
 
public class LibVLC {
    private static final String TAG = "VLC/LibVLC";
    public static final int AOUT_AUDIOTRACK_JAVA = 0;
    public static final int AOUT_AUDIOTRACK = 1;
    public static final int AOUT_OPENSLES = 2;
 
    public static final int VOUT_ANDROID_SURFACE = 0;
    public static final int VOUT_OPEGLES2 = 1;
 
    private static LibVLC sInstance;
 
    /** libVLC instance C pointer */
    private long mLibVlcInstance = 0; // Read-only, reserved for JNI
    /** libvlc_media_player pointer and index */
    private int mInternalMediaPlayerIndex = 0; // Read-only, reserved for JNI
    private long mInternalMediaPlayerInstance = 0; // Read-only, reserved for JNI
 
    private MediaList mMediaList; // Pointer to media list being followed
    private MediaList mPrimaryList; // Primary/default media list; see getPrimaryMediaList()
 
    /** Buffer for VLC messages */
    private StringBuffer mDebugLogBuffer;
    private boolean mIsBufferingLog = false;
 
    private Aout mAout;
 
    /** Keep screen bright */
    //private WakeLock mWakeLock;
 
    /** Settings */
    private int hardwareAcceleration = -1;
    private String subtitlesEncoding = "";
    private int aout = LibVlcUtil.isGingerbreadOrLater() ? AOUT_OPENSLES : AOUT_AUDIOTRACK_JAVA;
    private int vout = VOUT_ANDROID_SURFACE;
    private boolean timeStretching = false;
    private int deblocking = -1;
    private String chroma = "";
    private boolean verboseMode = true;
    private float[] equalizer = null;
    private boolean frameSkip = false;
    private int networkCaching = 0;
 
    /** Check in libVLC already initialized otherwise crash */
    private boolean mIsInitialized = false;
    public native void attachSurface(Surface surface, IVideoPlayer player);
 
    public native void detachSurface();
 
    public native void attachSubtitlesSurface(Surface surface);
    public native void detachSubtitlesSurface();
 
    /* Load library before object instantiation */
    static {
        try {
            if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1)
                System.loadLibrary("iomx-gingerbread");
            else if (Build.VERSION.SDK_INT <= 13)//Build.VERSION_CODES.HONEYCOMB_MR2
                System.loadLibrary("iomx-hc");
            else if (Build.VERSION.SDK_INT <= 18)//Build.VERSION_CODES.JELLY_BEAN_MR2
                System.loadLibrary("iomx-ics");
        } catch (Throwable t) {
            Log.w(TAG, "Unable to load the iomx library: " + t);
        }
        try {
            System.loadLibrary("vlcjni");
        } catch (UnsatisfiedLinkError ule) {
            Log.e(TAG, "Can't load vlcjni library: " + ule);
            /// FIXME Alert user
            System.exit(1);
        } catch (SecurityException se) {
            Log.e(TAG, "Encountered a security issue when loading vlcjni library: " + se);
            /// FIXME Alert user
            System.exit(1);
        }
    }
 
    /**
     * Singleton constructor of libVLC Without surface and vout to create the
     * thumbnail and get information e.g. on the MediaLibraryActivity
     *
     * @return libVLC instance
     * @throws LibVlcException
     */
    public static LibVLC getInstance() throws LibVlcException {
        synchronized (LibVLC.class) {
            if (sInstance == null) {
                /* First call */
                sInstance = new LibVLC();
            }
        }
 
        return sInstance;
    }
 
    /**
     * Return an existing instance of libVLC Call it when it is NOT important
     * that this fails
     *
     * @return libVLC instance OR null
     */
    public static LibVLC getExistingInstance() {
        synchronized (LibVLC.class) {
            return sInstance;
        }
    }
 
    /**
     * Constructor
     * It is private because this class is a singleton.
     */
    private LibVLC() {
        mAout = new Aout();
    }
 
    /**
     * Destructor:
     * It is bad practice to rely on them, so please don't forget to call
     * destroy() before exiting.
     */
    @Override
    public void finalize() {
        if (mLibVlcInstance != 0) {
            Log.d(TAG, "LibVLC is was destroyed yet before finalize()");
            destroy();
        }
    }
 
    /**
     * Get the media list that LibVLC is following right now.
     *
     * @return The media list object being followed
     */
    public MediaList getMediaList() {
        return mMediaList;
    }
 
    /**
     * Set the media list for LibVLC to follow.
     *
     * @param mediaList The media list object to follow
     */
    public void setMediaList(MediaList mediaList) {
        mMediaList = mediaList;
    }
 
    /**
     * Sets LibVLC to follow the default media list (see below)
     */
    public void setMediaList() {
        mMediaList = mPrimaryList;
    }
 
    /**
     * Gets the primary media list, or the "currently playing" list.
     * Not to be confused with the media list pointer from above, which
     * refers the the MediaList object that libVLC is currently following.
     * This list is just one out of many lists that it can be pointed towards.
     *
     * This list will be used for lists of songs that are not user-defined.
     * For example: selecting a song from the Songs list, or from the list
     * displayed after selecting an album.
     *
     * It is loaded as the default list.
     *
     * @return The primary media list
     */
    public MediaList getPrimaryMediaList() {
        return mPrimaryList;
    }
 
    /**
     * Give to LibVLC the surface to draw the video.
     * @param f the surface to draw
     */
    public native void setSurface(Surface f);
 
    public static synchronized void restart(Context context) {
        if (sInstance != null) {
            try {
                sInstance.destroy();
                sInstance.init(context);
            } catch (LibVlcException lve) {
                Log.e(TAG, "Unable to reinit libvlc: " + lve);
            }
        }
    }
 
    /**
     * those get/is* are called from native code to get settings values.
     */
 
    public int getHardwareAcceleration() {
        return this.hardwareAcceleration;
    }
 
    public void setHardwareAcceleration(int hardwareAcceleration) {
        if (hardwareAcceleration < 0) {
            // Automatic mode: activate MediaCodec opaque direct rendering for 4.3 and above.
            if (LibVlcUtil.isJellyBeanMR2OrLater())
                this.hardwareAcceleration = 2;
            else
                this.hardwareAcceleration = 0;
        }
        else
            this.hardwareAcceleration = hardwareAcceleration;
    }
 
    public String getSubtitlesEncoding() {
        return subtitlesEncoding;
    }
 
    public void setSubtitlesEncoding(String subtitlesEncoding) {
        this.subtitlesEncoding = subtitlesEncoding;
    }
 
    public int getAout() {
        return aout;
    }
 
    public void setAout(int aout) {
        if (aout < 0)
            this.aout = LibVlcUtil.isGingerbreadOrLater() ? AOUT_OPENSLES : AOUT_AUDIOTRACK_JAVA;
        else
            this.aout = aout;
    }
 
    public int getVout() {
        return vout;
    }
 
    public void setVout(int vout) {
        if (vout < 0)
            this.vout = VOUT_ANDROID_SURFACE;
        else
            this.vout = vout;
    }
 
    public boolean timeStretchingEnabled() {
        return timeStretching;
    }
 
    public void setTimeStretching(boolean timeStretching) {
        this.timeStretching = timeStretching;
    }
 
    public int getDeblocking() {
        int ret = deblocking;
        if(deblocking < 0) {
            /**
             * Set some reasonable deblocking defaults:
             *
             * Skip all (4) for armv6 and MIPS by default
             * Skip non-ref (1) for all armv7 more than 1.2 Ghz and more than 2 cores
             * Skip non-key (3) for all devices that don't meet anything above
             */
            LibVlcUtil.MachineSpecs m = LibVlcUtil.getMachineSpecs();
            if( (m.hasArmV6 && !(m.hasArmV7)) || m.hasMips )
                ret = 4;
            else if(m.bogoMIPS > 1200 && m.processors > 2)
                ret = 1;
            else
                ret = 3;
        } else if(deblocking > 4) { // sanity check
            ret = 3;
        }
        return ret;
    }
 
    public void setDeblocking(int deblocking) {
        this.deblocking = deblocking;
    }
 
    public String getChroma() {
        return chroma;
    }
 
    public void setChroma(String chroma) {
        this.chroma = chroma.equals("YV12") && !LibVlcUtil.isGingerbreadOrLater() ? "" : chroma;
    }
 
    public boolean isVerboseMode() {
        return verboseMode;
    }
 
    public void setVerboseMode(boolean verboseMode) {
        this.verboseMode = verboseMode;
    }
 
    public float[] getEqualizer()
    {
        return equalizer;
    }
 
    public void setEqualizer(float[] equalizer)
    {
        this.equalizer = equalizer;
        applyEqualizer();
    }
 
    private void applyEqualizer()
    {
        setNativeEqualizer(mInternalMediaPlayerInstance, this.equalizer);
    }
    private native int setNativeEqualizer(long mediaPlayer, float[] bands);
 
    public boolean frameSkipEnabled() {
        return frameSkip;
    }
 
    public void setFrameSkip(boolean frameskip) {
        this.frameSkip = frameskip;
    }
 
    public int getNetworkCaching() {
        return this.networkCaching;
    }
 
    public void setNetworkCaching(int networkcaching) {
        this.networkCaching = networkcaching;
    }
 
    /**
     * Initialize the libVLC class.
     *
     * This function must be called before using any libVLC functions.
     *
     * @throws LibVlcException
     */
    public void init(Context context) throws LibVlcException {
        Log.v(TAG, "Initializing LibVLC");
        mDebugLogBuffer = new StringBuffer();
        if (!mIsInitialized) {
            if(!LibVlcUtil.hasCompatibleCPU(context)) {
                Log.e(TAG, LibVlcUtil.getErrorMsg());
                throw new LibVlcException();
            }
            nativeInit();
            mMediaList = mPrimaryList = new MediaList(this);
            setEventHandler(EventHandler.getInstance());
            mIsInitialized = true;
        }
    }
 
    /**
     * Destroy this libVLC instance
     * @note You must call it before exiting
     */
    public void destroy() {
        Log.v(TAG, "Destroying LibVLC instance");
        nativeDestroy();
        detachEventHandler();
        mIsInitialized = false;
    }
 
    /**
     * Open the Java audio output.
     * This function is called by the native code
     */
    public void initAout(int sampleRateInHz, int channels, int samples) {
        Log.d(TAG, "Opening the java audio output");
        mAout.init(sampleRateInHz, channels, samples);
    }
 
    /**
     * Play an audio buffer taken from the native code
     * This function is called by the native code
     */
    public void playAudio(byte[] audioData, int bufferSize) {
        mAout.playBuffer(audioData, bufferSize);
    }
 
    /**
     * Pause the Java audio output
     * This function is called by the native code
     */
    public void pauseAout() {
        Log.d(TAG, "Pausing the java audio output");
        mAout.pause();
    }
 
    /**
     * Close the Java audio output
     * This function is called by the native code
     */
    public void closeAout() {
        Log.d(TAG, "Closing the java audio output");
        mAout.release();
    }
 
    /**
     * Play a media from the media list (playlist)
     *
     * @param position The index of the media
     */
    public void playIndex(int position) {
        String mrl = mMediaList.getMRL(position);
        if (mrl == null)
            return;
        String[] options = mMediaList.getMediaOptions(position);
        mInternalMediaPlayerIndex = position;
        playMRL(mLibVlcInstance, mrl, options);
    }
 
    /**
     * Play an MRL directly.
     *
     * @param mrl MRL of the media to play.
     */
    public void playMRL(String mrl) {
        // index=-1 will return options from libvlc instance without relying on MediaList
        String[] options = mMediaList.getMediaOptions(-1);
        mInternalMediaPlayerIndex = 0;
        playMRL(mLibVlcInstance, mrl, options);
    }
 
    public TrackInfo[] readTracksInfo(String mrl) {
        return readTracksInfo(mLibVlcInstance, mrl);
    }
 
    /**
     * Get a media thumbnail.
     */
    public byte[] getThumbnail(String mrl, int i_width, int i_height) {
        return getThumbnail(mLibVlcInstance, mrl, i_width, i_height);
    }
 
    /**
     * Return true if there is a video track in the file
     */
    public boolean hasVideoTrack(String mrl) throws java.io.IOException {
        return hasVideoTrack(mLibVlcInstance, mrl);
    }
 
    /**
     * Sets the speed of playback (1 being normal speed, 2 being twice as fast)
     *
     * @param rate
     */
    public native void setRate(float rate);
 
    /**
     * Get the current playback speed
     */
    public native float getRate();
 
    /**
     * Initialize the libvlc C library
     * @return a pointer to the libvlc instance
     */
    private native void nativeInit() throws LibVlcException;
 
    /**
     * Close the libvlc C library
     * @note mLibVlcInstance should be 0 after a call to destroy()
     */
    private native void nativeDestroy();
 
    /**
     * Start buffering to the mDebugLogBuffer.
     */
    public native void startDebugBuffer();
    public native void stopDebugBuffer();
    public String getBufferContent() {
        return mDebugLogBuffer.toString();
    }
 
    public void clearBuffer() {
        mDebugLogBuffer.setLength(0);
    }
 
    public boolean isDebugBuffering() {
        return mIsBufferingLog;
    }
 
    /**
     * Play an mrl
     */
    private native void playMRL(long instance, String mrl, String[] mediaOptions);
 
    /**
     * Returns true if any media is playing
     */
    public native boolean isPlaying();
 
    /**
     * Returns true if any media is seekable
     */
    public native boolean isSeekable();
 
    /**
     * Plays any loaded media
     */
    public native void play();
 
    /**
     * Pauses any playing media
     */
    public native void pause();
 
    /**
     * Stops any playing media
     */
    public native void stop();
 
    /**
     * Gets volume as integer
     */
    public native int getVolume();
 
    /**
     * Sets volume as integer
     * @param volume: Volume level passed as integer
     */
    public native int setVolume(int volume);
 
    /**
     * Gets the current movie time (in ms).
     * @return the movie time (in ms), or -1 if there is no media.
     */
    public native long getTime();
 
    /**
     * Sets the movie time (in ms), if any media is being played.
     * @param time: Time in ms.
     * @return the movie time (in ms), or -1 if there is no media.
     */
    public native long setTime(long time);
 
    /**
     * Gets the movie position.
     * @return the movie position, or -1 for any error.
     */
    public native float getPosition();
 
    /**
     * Sets the movie position.
     * @param pos: movie position.
     */
    public native void setPosition(float pos);
 
    /**
     * Gets current movie's length in ms.
     * @return the movie length (in ms), or -1 if there is no media.
     */
    public native long getLength();
 
    /**
     * Get the libVLC version
     * @return the libVLC version string
     */
    public native String version();
 
    /**
     * Get the libVLC compiler
     * @return the libVLC compiler string
     */
    public native String compiler();
 
    /**
     * Get the libVLC changeset
     * @return the libVLC changeset string
     */
    public native String changeset();
 
    /**
     * Get a media thumbnail.
     * @return a bytearray with the RGBA thumbnail data inside.
     */
    private native byte[] getThumbnail(long instance, String mrl, int i_width, int i_height);
 
    /**
     * Return true if there is a video track in the file
     */
    private native boolean hasVideoTrack(long instance, String mrl);
 
    private native TrackInfo[] readTracksInfo(long instance, String mrl);
 
    public native TrackInfo[] readTracksInfoInternal();
 
    public native int getAudioTracksCount();
 
    public native Map<Integer,String> getAudioTrackDescription();
 
    public native int getAudioTrack();
 
    public native int setAudioTrack(int index);
 
    public native int getVideoTracksCount();
 
    public native int addSubtitleTrack(String path);
 
    public native Map<Integer,String> getSpuTrackDescription();
 
    public native int getSpuTrack();
 
    public native int setSpuTrack(int index);
 
    public native int getSpuTracksCount();
 
    public static native String nativeToURI(String path);
 
    /**
     * Quickly converts path to URIs, which are mandatory in libVLC.
     *
     * @param path
     *            The path to be converted.
     * @return A URI representation of path
     */
    public static String PathToURI(String path) {
        if(path == null) {
            throw new NullPointerException("Cannot convert null path!");
        }
        return LibVLC.nativeToURI(path);
    }
 
    public static native void nativeReadDirectory(String path, ArrayList<String> res);
 
    public native static boolean nativeIsPathDirectory(String path);
 
     /**
      * Expand and continue playing the current media.
      *
      * @return the index of the media was expanded, and -1 if no media was expanded
      */
    public int expandAndPlay() {
        int r = mMediaList.expandMedia(mInternalMediaPlayerIndex);
        if(r == 0)
            this.playIndex(mInternalMediaPlayerIndex);
        return r;
    }
 
    /**
     * Expand the current media.
     * @return the index of the media was expanded, and -1 if no media was expanded
     */
    public int expand() {
        return mMediaList.expandMedia(mInternalMediaPlayerIndex);
    }
 
    private native void setEventHandler(EventHandler eventHandler);
 
    private native void detachEventHandler();
 
    public native float[] getBands();
 
    public native String[] getPresets();
 
    public native float[] getPreset(int index);
}