-
Notifications
You must be signed in to change notification settings - Fork 0
/
VideoViewer.java
497 lines (430 loc) · 15.8 KB
/
VideoViewer.java
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
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ImageFormat;
import android.graphics.PixelFormat;
import android.graphics.Point;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Display;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Toast;
import org.videolan.libvlc.IVLCVout;
import org.videolan.libvlc.LibVLC;
import org.videolan.libvlc.Media;
import org.videolan.libvlc.MediaPlayer;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
public class VideoViewer extends SurfaceView implements IVLCVout.Callback {
static private String TAG = "VideoViewer";
public final static int MSG_VIDEOVIEWER_STOP = 1001;
public final static int MSG_VIDEOVIEWER_NEWLAYOUT = 1002;
public final static int MSG_VIDEOVIEWER_START = 1003;
private SurfaceHolder surfaceHolder;
private LibVLC libvlc;
private static MediaPlayer mMediaPlayer = null;
private Context mContext;
private int mVideoWidth;
private int mVideoHeight;
private int mVideoVisibleWidth;
private int mVideoVisibleHeight;
private int mSarNum ;
private int mSarDen ;
public int layoutW ;
public int layoutH ;
private static Handler mHandler;
private static Media mMedia = null;
public VideoViewer(Context context) {
super(context);
mContext = context;
init();
}
public VideoViewer(Context context,
AttributeSet attrs) {
super(context, attrs);
mContext = context;
init();
}
public VideoViewer(Context context,
AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mContext = context;
init();
}
private void init() {
surfaceHolder = getHolder();
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(ApplicationContext.getContext()) ;
String chroma = pref.getString("chroma_format", "") ;
if (chroma.equals("YV12")) {
surfaceHolder.setFormat(ImageFormat.YV12) ;
} else if (chroma.equals("RV16")) {
surfaceHolder.setFormat(PixelFormat.RGB_565) ;
PixelFormat info = new PixelFormat() ;
PixelFormat.getPixelFormatInfo(PixelFormat.RGB_565, info) ;
} else {
surfaceHolder.setFormat(PixelFormat.RGBX_8888) ;
PixelFormat info = new PixelFormat() ;
PixelFormat.getPixelFormatInfo(PixelFormat.RGBX_8888, info) ;
}
}
public void setHandler(Handler hld){
mHandler = hld;
}
public void freshNewLayout() {
mVideoWidth = 640;
mVideoHeight = 482;
mVideoVisibleWidth = 640;
mVideoVisibleHeight = 480;
mSarNum = 1;
mSarDen = 1;
//
setSize(mVideoWidth, mVideoHeight);
}
@Override
public void onNewLayout(IVLCVout vout, int width, int height, int visibleWidth, int visibleHeight, int sarNum, int sarDen) {
Log.d(TAG, "onNewLayout: ");
if (width * height == 0) {
return;
}
// store video size
mVideoWidth = width;
mVideoHeight = height;
mVideoVisibleWidth = visibleWidth;
mVideoVisibleHeight = visibleHeight;
mSarNum = sarNum;
mSarDen = sarDen;
setSize(mVideoWidth, mVideoHeight);
//setSize(1280, 720);
}
@Override
public void onSurfacesCreated(IVLCVout ivlcVout) {
}
@Override
public void onSurfacesDestroyed(IVLCVout ivlcVout) {
}
@Override
public void onHardwareAccelerationError(IVLCVout ivlcVout) {
}
public void setSize(int width, int height) {
mVideoWidth = width;
mVideoHeight = height;
if (mVideoWidth * mVideoHeight <= 1)
return;
if(surfaceHolder == null || this == null)
return;
WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int dw = size.x;
int dh = size.y;
boolean isPortrait = getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT ;
if (dw > dh && isPortrait || dw < dh && !isPortrait) {
int d = dw ;
dw = dh ;
dh = d ;
}
// sanity check
if (dw * dh == 0 || mVideoWidth * mVideoHeight == 0) {
Log.e(TAG, "Invalid surface size") ;
return ;
}
// compute the aspect ratio
double ar, vw ;
double density = (double) mSarNum / (double) mSarDen ;
if (density == 1.0) {
/* No indication about the density, assuming 1:1 */
vw = mVideoVisibleWidth ;
ar = (double) mVideoVisibleWidth / (double) mVideoVisibleHeight ;
} else {
/* Use the specified aspect ratio */
vw = mVideoVisibleWidth * density ;
ar = vw / mVideoVisibleHeight ;
}
// compute the display aspect ratio
double dar = (double) dw / (double) dh ;
// switch (mCurrentSize) {
// case SURFACE_BEST_FIT:
// if (dar < ar)
// dh = (int) (dw / ar) ;
// else
// dw = (int) (dh * ar) ;
// break ;
// case SURFACE_FIT_HORIZONTAL:
// dh = (int) (dw / ar) ;
// break ;
// case SURFACE_FIT_VERTICAL:
// dw = (int) (dh * ar) ;
// break ;
// case SURFACE_FILL:
// break ;
// case SURFACE_16_9:
ar = 16.0 / 9.0 ;
if (dar < ar)
dh = (int) (dw / ar) ;
else
dw = (int) (dh * ar) ;
// break ;
// case SURFACE_4_3:
// ar = 4.0 / 3.0 ;
// if (dar < ar)
// dh = (int) (dw / ar) ;
// else
// dw = (int) (dh * ar) ;
// break ;
// case SURFACE_ORIGINAL:
// dh = mVideoVisibleHeight ;
// dw = (int) vw ;
// break ;
// }
// force surface buffer size
surfaceHolder.setFixedSize(mVideoWidth, mVideoHeight) ;
// set display size
ViewGroup.LayoutParams lp = this.getLayoutParams() ;
lp.width = dw * mVideoWidth / mVideoVisibleWidth ;
lp.height = dh * mVideoHeight / mVideoVisibleHeight;
this.setLayoutParams(lp) ;
layoutW = dw;
layoutH = dh;
// set frame size (crop if necessary)
// lp = mSurfaceFrame.getLayoutParams() ;
// lp.width = dw ;
// lp.height = dh ;
// mSurfaceFrame.setLayoutParams(lp) ;
this.invalidate() ;
Message message;
//String obj = "OK";
message = mHandler.obtainMessage();
message.arg1 = MSG_VIDEOVIEWER_NEWLAYOUT;
mHandler.sendMessage(message);
/*
// get screen size
WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int w = size.x;
int h = size.y;
boolean isPortrait = getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
if (w > h && isPortrait || w < h && !isPortrait) {
int i = w;
w = h;
h = i;
}
float videoAR = (float) mVideoWidth / (float) mVideoHeight;
float screenAR = (float) w / (float) h;
if (screenAR < videoAR)
h = (int) (w / videoAR);
else
w = (int) (h * videoAR);
// force surface buffer size
surfaceHolder.setFixedSize(mVideoWidth, mVideoHeight);
// set display size
ViewGroup.LayoutParams lp = this.getLayoutParams();
lp.width = w;
lp.height = h;
Log.e("02 3345678 ","LP width= " + lp.width + ",LP height=" + lp.height);
this.setLayoutParams(lp);
this.invalidate();
*/
}
/*
public void startPlay(String media)
{
Uri uri = Uri.parse(media);
Media m = new Media(libvlc, uri);
m.setHWDecoderEnabled(false, false);
//m.addOption(":network-caching=2000");
mMediaPlayer.setMedia(m);
mMediaPlayer.play();
surfaceHolder = getHolder();
Canvas canvas = surfaceHolder.lockCanvas();
//Clear surface by drawing on it
canvas.drawColor(Color.BLACK);
surfaceHolder.unlockCanvasAndPost(canvas);
}
public void startLocalPlay(String media)
{
Media m = new Media(libvlc, media);
m.setHWDecoderEnabled(false, false);
//m.addOption(":network-caching=2000");
mMediaPlayer.setMedia(m);
mMediaPlayer.play();
surfaceHolder = getHolder();
Canvas canvas = surfaceHolder.lockCanvas();
//Clear surface by drawing on it
canvas.drawColor(Color.BLACK);
surfaceHolder.unlockCanvasAndPost(canvas);
}
*/
// public void stopPlay()
// {
// mMediaPlayer.stop();
// }
public void createPlayer(String media,boolean rtsp,int nc) {
// releasePlayer();
Uri uri;
Media m;
int isTcp;
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences( ApplicationContext.getContext());
isTcp = preferences.getInt("_rtsp_tcp", 0);
try {
if (media.length() > 0) {
// Toast toast = Toast.makeText(this, media, Toast.LENGTH_LONG);
// toast.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0,
// 0);
// toast.show();
}
// Create LibVLC
ArrayList<String> options = new ArrayList<String>();
options.add("--aout=opensles");
options.add("--audio-time-stretch"); // time stretching
//options.add("--no-audio");
options.add("--network-caching="+nc);
options.add("--clock-jitter="+nc);
if (isTcp == 1)
options.add("--rtsp-tcp");
//options.add("--enable-iomx");
options.add("-vvv"); // verbosity
libvlc = new LibVLC(options);
// libvlc.setOnHardwareAccelerationError(this);
surfaceHolder = getHolder();
surfaceHolder.setKeepScreenOn(true);
///clean screen
Canvas canvas = surfaceHolder.lockCanvas();
//Clear surface by drawing on it
canvas.drawColor(Color.BLACK);
surfaceHolder.unlockCanvasAndPost(canvas);
// Create media player
mMediaPlayer = new MediaPlayer(libvlc);
mMediaPlayer.setEventListener(mPlayerListener);
// Set up video output
final IVLCVout vout = mMediaPlayer.getVLCVout();
vout.setVideoView(this);
//vout.setSubtitlesView(mSurfaceSubtitles);
vout.addCallback(this);
vout.attachViews();
if (rtsp) {
uri = Uri.parse(media);
m = new Media(libvlc, uri);
}
else
m = new Media(libvlc, media);
m.setHWDecoderEnabled(false, false);
mMediaPlayer.setMedia(m);
mMediaPlayer.play();
mMedia = m;
} catch (Exception e) {
//releasePlayer();
// Toast.makeText(MainActivity.getAppContext(), "Error creating player!", Toast.LENGTH_LONG).show();
}
}
public MediaPlayer getMediaPlayer() {
return mMediaPlayer;
}
public void releasePlayer() {
if (libvlc == null)
return;
mMediaPlayer.stop();
final IVLCVout vout = mMediaPlayer.getVLCVout();
//vout.removeCallback(this);
vout.detachViews();
surfaceHolder = null;
libvlc.release();
libvlc = null;
mVideoWidth = 0;
mVideoHeight = 0;
}
public static boolean isPlaying() {
if (mMediaPlayer == null)
return false;
return mMediaPlayer.isPlaying();
}
private MediaPlayer.EventListener mPlayerListener = new MyPlayerListener(this);
private static class MyPlayerListener implements MediaPlayer.EventListener {
private WeakReference<VideoViewer> mOwner;
public MyPlayerListener(VideoViewer owner) {
mOwner = new WeakReference<VideoViewer>(owner);
}
@Override
public void onEvent(MediaPlayer.Event event) {
VideoViewer player = mOwner.get();
switch(event.type) {
case MediaPlayer.Event.EndReached:
Log.w(TAG, "MediaPlayer EndReached");
player.releasePlayer();
break;
case MediaPlayer.Event.Playing:
Log.e(TAG, "MediaPlayer Playing");
if (mHandler != null) {
Log.e(TAG, "MediaPlayer Playing 1");
Message message;
//String obj = "OK";
message = mHandler.obtainMessage();
message.arg1 = MSG_VIDEOVIEWER_START;
mHandler.sendMessage(message);
}
break;
case MediaPlayer.Event.Buffering:
//Log.e(TAG, "MediaPlayer Buffering:" + mMediaPlayer.getPosition());
Log.e(TAG, "MediaPlayer Buffering:" + event.getBuffering());
break;
case MediaPlayer.Event.Paused:
break;
case MediaPlayer.Event.Stopped:
Log.w(TAG, "MediaPlayer STOP");
if (mHandler != null) {
Message message;
//String obj = "OK";
message = mHandler.obtainMessage();
message.arg1 = MSG_VIDEOVIEWER_STOP;
mHandler.sendMessage(message);
}
break;
default:
break;
}
}
}
//Qos
public static int getBitrate()
{
if ((mMedia != null) && (isPlaying())) {
//Log.e(TAG,"bbb=" + mMedia.getBitrate()*8000);
return (int)(mMedia.getBitrate()*8000);
}
return 0;
}
public static int getLostP()
{
if ((mMedia != null) && (isPlaying())) {
//Log.e(TAG,"bbb=" + mMedia.getBitrate()*8000);
return (int)(mMedia.getLostPictures());
}
return 0;
}
public void pause() {
if (mMediaPlayer != null) {
mMediaPlayer.pause();
}
}
public long getTotalTime() {
if (mMediaPlayer == null)
return 0;
return mMediaPlayer.getLength();
}
public void play() {
if (mMediaPlayer != null) {
mMediaPlayer.play();
}
}
}