Index: webrtc/api/android/java/src/org/webrtc/FileVideoCapturer.java |
diff --git a/webrtc/api/android/java/src/org/webrtc/FileVideoCapturer.java b/webrtc/api/android/java/src/org/webrtc/FileVideoCapturer.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..16a9cb166ef94642f0642629d517838450bdea81 |
--- /dev/null |
+++ b/webrtc/api/android/java/src/org/webrtc/FileVideoCapturer.java |
@@ -0,0 +1,143 @@ |
+/* |
+ * Copyright 2016 The WebRTC Project Authors. All rights reserved. |
+ * |
+ * Use of this source code is governed by a BSD-style license |
+ * that can be found in the LICENSE file in the root of the source |
+ * tree. An additional intellectual property rights grant can be found |
+ * in the file PATENTS. All contributing project authors may |
+ * be found in the AUTHORS file in the root of the source tree. |
+ */ |
+package org.webrtc; |
+ |
+import android.content.Context; |
+import android.os.SystemClock; |
+ |
+import java.util.List; |
+import java.util.ArrayList; |
+import java.util.concurrent.TimeUnit; |
+ |
+import java.io.File; |
+import java.io.RandomAccessFile; |
+import java.io.IOException; |
+ |
+public class FileVideoCapturer { |
+ private interface VideoReader { |
+ int getFrameWidth(); |
+ int getFrameHeight(); |
+ byte[] getNextFrame(); |
+ void close(); |
+ } |
+ |
+ private class VideoReaderYuv implements VideoReader { |
+ private final static String TAG = "VideoReaderYuv"; |
+ private int frameWidth; |
+ private int frameHeight; |
+ private int frameSize; |
+ private RandomAccessFile mediaFileStream; |
+ |
+ @Override |
+ public int getFrameWidth() { |
+ return frameWidth; |
+ } |
+ |
+ @Override |
+ public int getFrameHeight() { |
+ return frameHeight; |
+ } |
+ |
+ VideoReaderYuv(String file, int width, int height) throws IOException { |
+ mediaFileStream = new RandomAccessFile(file, "r"); |
+ this.frameWidth = width; |
+ this.frameHeight = height; |
+ this.frameSize = width * height * 3 / 2; |
+ } |
+ |
+ @Override |
+ public byte[] getNextFrame() { |
+ byte[] frame = new byte[frameSize]; |
+ try { |
+ if (mediaFileStream.length() - mediaFileStream.getFilePointer() < frameSize) { |
+ mediaFileStream.seek(0); |
+ } |
+ mediaFileStream.readFully(frame); |
+ |
+ return frame; |
+ } |
+ catch (IOException e) { |
+ Logging.d(TAG, "Problem reading file"); |
+ return null; |
+ } |
+ } |
+ |
+ @Override |
+ public void close() { |
+ try { |
+ mediaFileStream.close(); |
+ } |
+ catch (IOException e) { |
+ Logging.d(TAG, "Problem closing file"); |
+ } |
+ } |
+ } |
+ |
+ private final static String TAG = "FileVideoCapturer"; |
+ |
+ private VideoReader videoReader; |
+ |
+ private int getFrameWidth() { |
+ return videoReader.getFrameWidth(); |
+ } |
+ |
+ private int getFrameHeight() { |
+ return videoReader.getFrameHeight(); |
+ } |
+ |
+ public FileVideoCapturer(String inputFile, int fileVideoWidth, int fileVideoHeight |
+ ) throws IOException { |
+ openMediaFile(inputFile, fileVideoWidth, fileVideoHeight); |
+ } |
+ |
+ public static FileVideoCapturer create(String inputFile, int fileVideoWidth, |
+ int fileVideoHeight) { |
+ try { |
+ return new FileVideoCapturer( |
+ inputFile, fileVideoWidth, fileVideoHeight); |
+ } catch (IOException e) { |
+ Logging.e(TAG, "Error opening input file.", e); |
+ return null; |
+ } |
+ } |
+ |
+ public void dispose() { |
+ videoReader.close(); |
+ } |
+ |
+ private void openMediaFile(String file, int fileVideoWidth, int fileVideoHeight) |
+ throws IOException |
+ { |
+ try { |
+ videoReader = new VideoReaderYuv(file, fileVideoWidth, fileVideoHeight); |
+ } |
+ catch (IOException e) { |
+ Logging.d(TAG, "Could not open video file: " + file); |
+ throw e; |
+ } |
+ } |
+ |
+ private byte[] getNextFrame() { |
+ return videoReader.getNextFrame(); |
+ } |
+ |
+ public void tick(VideoCapturer.CapturerObserver capturerObserver) { |
+ final long captureTimeNs = |
+ TimeUnit.MILLISECONDS.toNanos(SystemClock.elapsedRealtime()); |
+ |
+ byte[] frameData = getNextFrame(); |
+ capturerObserver.onByteBufferFrameCaptured( |
+ frameData, |
+ getFrameWidth(), |
+ getFrameHeight(), |
+ 0, |
+ captureTimeNs); |
+ } |
+} |