visageSDK
MediaLoader.java
1 package com.visagetechnologies.showcasedemo;
2 
3 import android.content.ContentResolver;
4 import android.content.Context;
5 import android.graphics.Bitmap;
6 import android.graphics.BitmapFactory;
7 import android.graphics.ImageDecoder;
8 import android.media.Image;
9 import android.media.MediaCodec;
10 import android.media.MediaExtractor;
11 import android.media.MediaFormat;
12 import android.net.Uri;
13 import android.os.Build;
14 import android.provider.MediaStore;
15 import android.util.Log;
16 
17 import java.io.File;
18 import java.io.FileInputStream;
19 import java.io.FileNotFoundException;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.nio.ByteBuffer;
23 
24 import static android.graphics.Bitmap.Config.ARGB_8888;
25 
26 class MediaLoader {
27 
28  static class ImageLoader {
29  private static Bitmap bitmap = null;
30 
31  private static void ResetVariables() {
32  if (bitmap != null) {
33  bitmap.recycle();
34  bitmap = null;
35  }
36  }
37 
46  public static byte[] ConvertToByte(Bitmap bitmap_, boolean opacity) {
47  int w = bitmap_.getWidth();
48  int h = bitmap_.getHeight();
49 
50  int[] rgbStream = new int[w * h];
51  bitmap_.getPixels(rgbStream, 0, w, 0, 0, w, h);
52 
53  byte[] buffer = new byte[w * h * 4];
54  int offset = 0;
55  for (int i = 0; i < w * h; i++) {
56  int pixel = rgbStream[i];
57 
58  int R = (pixel >> 16) & 0xFF;
59  int G = (pixel >> 8) & 0xFF;
60  int B = pixel & 0xFF;
61 
62  buffer[offset + 0] = (byte) R;
63  buffer[offset + 1] = (byte) G;
64  buffer[offset + 2] = (byte) B;
65 
66  if (opacity) {
67  int A = (pixel >> 24) & 0xFF;
68  buffer[offset + 3] = (byte) A;
69  offset = offset + 4;
70  } else {
71 
72  offset = offset + 3;
73  }
74 
75  }
76 
77  return buffer;
78  }
79 
84  public static void LoadPictureSendToVW(ContentResolver resolver, Uri imageUri) throws IOException {
85  if (Build.VERSION.SDK_INT >= 28)
86  {
87  Bitmap tempBitmap = ImageDecoder.decodeBitmap(ImageDecoder.createSource(resolver, imageUri));
88  bitmap = tempBitmap.copy(Bitmap.Config.ARGB_8888,true);
89  }
90  else
91  {
92  BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
93  bitmapOptions.inPreferredConfig=Bitmap.Config.ARGB_8888;
94  InputStream input = resolver.openInputStream(imageUri);
95  bitmap = BitmapFactory.decodeStream(input, null, bitmapOptions);
96  }
97 
98  if (bitmap == null)
99  return;
100 
101  VisageWrapper.SetParameters(bitmap.getWidth(), bitmap.getHeight());
102  VisageWrapper.WriteFrameImage(ConvertToByte(bitmap, false), bitmap.getWidth(), bitmap.getHeight());
103 
104  ResetVariables();
105 
106  }
107 
108  }
109 
110  static class VideoLoaderThread implements Runnable {
111  static final String TAG = "VideoLoaderThread";
112  volatile boolean exitThread = false;
113  Context appContext = null;
114  Thread videoThread = null;
115  Uri videoURI;
116  MediaExtractor extractor = null;
117  MediaCodec decoder = null;
118  MediaCodec.BufferInfo info;
119 
120  Integer width = 0;
121  Integer height = 0;
122  Integer rotationAngle = 0;
123  Integer frameRate = 0;
124 
125  long frameTime = 0;
126 
127 
128  VideoLoaderThread(Uri URI, Context context) {
129 
130  videoURI = URI;
131  appContext = context;
132  }
133 
134  void pause(){
135  videoThread.interrupt();
136  }
137 
138  void start() {
139  videoThread = new Thread(this);
140  videoThread.setName("videoThread");
141  videoThread.start();
142  }
143 
144  void stop() throws InterruptedException {
145  videoThread.interrupt();
146  exitThread = true;
147  }
148 
149  public void run() {
150  initialize();
151 
152  MediaCodec.BufferInfo info = new MediaCodec.BufferInfo();
153  boolean isEOS = false;
154 
155  int frameCount = 0;
156  long currentTime = 0;
157  long startTime = System.currentTimeMillis();
158  while(!exitThread) {
159  try {
160  if (!isEOS) {
161  int inIndex = decoder.dequeueInputBuffer(1000);
162  if (inIndex >= 0) {
163  ByteBuffer buffer = decoder.getInputBuffer(inIndex);
164  int sampleSize = extractor.readSampleData(buffer, 0);
165  long sampleTime = extractor.getSampleTime();
166  if (sampleSize < 0) {
167  decoder.queueInputBuffer(inIndex, 0, 0, sampleTime, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
168  isEOS = true;
169  } else {
170  decoder.queueInputBuffer(inIndex, 0, sampleSize, sampleTime, 0);
171  extractor.advance();
172 
173  }
174  }
175 
176  Integer outIndex = decoder.dequeueOutputBuffer(info, 10000);
177  if (outIndex >= 0) {
178 
179  Image.Plane[] imagePlanes = decoder.getOutputImage(outIndex).getPlanes();
180  currentTime = System.currentTimeMillis() - startTime;
181 
182  // uncomment this if you want to skip frames for
183  // videos that reproduce slower than it's original frame rate
184  /*
185  if(currentTime > frameTime * (frameCount + 1)){
186  frameCount++;
187  decoder.releaseOutputBuffer(outIndex, 1000);
188  continue;
189  }
190  */
191  while(currentTime < frameTime * (frameCount -5)){
192  Thread.sleep(1);
193  currentTime = System.currentTimeMillis() - startTime;
194 
195  }
196  long ts;
197  if (frameRate > 0)
198  ts =(long)((1000.0/frameRate) * frameCount);
199  else
200  ts =-1;
201  VisageWrapper.WriteFrameStream(imagePlanes[0].getBuffer(), imagePlanes[1].getBuffer(), imagePlanes[2].getBuffer(), ts, 0, imagePlanes[1].getPixelStride());
202  frameCount++;
203  decoder.releaseOutputBuffer(outIndex, 1000);
204  }
205  } else{
206  Thread.sleep(10);
207  break;
208  }
209  } catch (InterruptedException e) {
210  System.out.println("InterruptedException occur");
211  Log.w("VideoThread", "exception occur");
212  }
213  }
214  long elapsedTime = System.currentTimeMillis() - startTime;
215  Log.d("DURATION", "elapsed time video decoding " + elapsedTime);
216  release();
217  }
218 
219  void release(){
220  decoder.release();
221  extractor.release();
222  videoURI = null;
223  }
224 
225  private void initialize() {
226  info = new MediaCodec.BufferInfo();
227  extractor = new MediaExtractor();
228 
229  try {
230  extractor.setDataSource(appContext, videoURI, null);
231 
232  int index = extractor.getTrackCount();
233 
234  for (int i = 0; i < index; i++) {
235  MediaFormat format = extractor.getTrackFormat(i);
236 
237  String mime = format.getString(MediaFormat.KEY_MIME);
238  if (mime.startsWith("video/")) {
239  extractor.selectTrack(i);
240  decoder = MediaCodec.createDecoderByType(mime);
241  decoder.configure(format, null, null, 0);
242 
243  width = format.getInteger(MediaFormat.KEY_WIDTH);
244  height = format.getInteger(MediaFormat.KEY_HEIGHT);
245 
246  if(format.containsKey(MediaFormat.KEY_ROTATION)){
247  rotationAngle = format.getInteger(MediaFormat.KEY_ROTATION);
248  } else {
249  rotationAngle = 0;
250  }
251  if (format.containsKey(MediaFormat.KEY_FRAME_RATE)) {
252  frameRate = format.getInteger(MediaFormat.KEY_FRAME_RATE);
253  Log.d("DURATION", "framerate " + frameRate);
254  frameTime = 1000 / (long)frameRate;
255  }
256  if(format.containsKey(MediaFormat.KEY_DURATION)){
257  long duration = format.getLong(MediaFormat.KEY_DURATION);
258  Log.d("DURATION", "duration " + duration);
259  }
260 
261  VisageWrapper.SetParameters(width, height, rotationAngle, 0);
262 
263  break;
264  }
265  }
266 
267  decoder.start();
268  } catch (Exception exception) {
269  exception.printStackTrace();
270  }
271  }
272  }
273 
274 }