Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | package org.webrtc; |
| 12 | |
| 13 | import java.nio.ByteBuffer; |
sakal | e172d89 | 2017-08-31 02:37:28 -0700 | [diff] [blame] | 14 | import java.util.concurrent.TimeUnit; |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 15 | |
| 16 | /** |
| 17 | * An encoded frame from a video stream. Used as an input for decoders and as an output for |
| 18 | * encoders. |
| 19 | */ |
| 20 | public class EncodedImage { |
sakal | 07a3bd7 | 2017-09-04 03:57:21 -0700 | [diff] [blame] | 21 | // Must be kept in sync with common_types.h FrameType. |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 22 | public enum FrameType { |
sakal | 07a3bd7 | 2017-09-04 03:57:21 -0700 | [diff] [blame] | 23 | EmptyFrame(0), |
| 24 | VideoFrameKey(3), |
| 25 | VideoFrameDelta(4); |
| 26 | |
| 27 | private final int nativeIndex; |
| 28 | |
| 29 | private FrameType(int nativeIndex) { |
| 30 | this.nativeIndex = nativeIndex; |
| 31 | } |
| 32 | |
| 33 | public int getNative() { |
| 34 | return nativeIndex; |
| 35 | } |
Magnus Jedvert | 4eb0188 | 2017-11-20 22:33:40 +0100 | [diff] [blame] | 36 | |
Magnus Jedvert | 4eb0188 | 2017-11-20 22:33:40 +0100 | [diff] [blame] | 37 | @CalledByNative("FrameType") |
Magnus Jedvert | 1f2a3e7 | 2017-11-23 16:56:44 +0100 | [diff] [blame] | 38 | static FrameType fromNativeIndex(int nativeIndex) { |
Magnus Jedvert | 4eb0188 | 2017-11-20 22:33:40 +0100 | [diff] [blame] | 39 | for (FrameType type : FrameType.values()) { |
| 40 | if (type.getNative() == nativeIndex) { |
| 41 | return type; |
| 42 | } |
| 43 | } |
| 44 | throw new IllegalArgumentException("Unknown native frame type: " + nativeIndex); |
| 45 | } |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | public final ByteBuffer buffer; |
| 49 | public final int encodedWidth; |
| 50 | public final int encodedHeight; |
sakal | e172d89 | 2017-08-31 02:37:28 -0700 | [diff] [blame] | 51 | public final long captureTimeMs; // Deprecated |
| 52 | public final long captureTimeNs; |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 53 | public final FrameType frameType; |
| 54 | public final int rotation; |
| 55 | public final boolean completeFrame; |
| 56 | public final Integer qp; |
| 57 | |
Magnus Jedvert | 1f2a3e7 | 2017-11-23 16:56:44 +0100 | [diff] [blame] | 58 | @CalledByNative |
sakal | e172d89 | 2017-08-31 02:37:28 -0700 | [diff] [blame] | 59 | private EncodedImage(ByteBuffer buffer, int encodedWidth, int encodedHeight, long captureTimeNs, |
Sami Kalliomäki | 26ecfcc | 2017-06-14 13:46:58 +0200 | [diff] [blame] | 60 | FrameType frameType, int rotation, boolean completeFrame, Integer qp) { |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 61 | this.buffer = buffer; |
| 62 | this.encodedWidth = encodedWidth; |
| 63 | this.encodedHeight = encodedHeight; |
sakal | e172d89 | 2017-08-31 02:37:28 -0700 | [diff] [blame] | 64 | this.captureTimeMs = TimeUnit.NANOSECONDS.toMillis(captureTimeNs); |
| 65 | this.captureTimeNs = captureTimeNs; |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 66 | this.frameType = frameType; |
| 67 | this.rotation = rotation; |
| 68 | this.completeFrame = completeFrame; |
| 69 | this.qp = qp; |
| 70 | } |
| 71 | |
Bjorn Mellem | 5c4eebb | 2017-06-12 09:21:03 -0700 | [diff] [blame] | 72 | public static Builder builder() { |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 73 | return new Builder(); |
| 74 | } |
| 75 | |
Bjorn Mellem | 5c4eebb | 2017-06-12 09:21:03 -0700 | [diff] [blame] | 76 | public static class Builder { |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 77 | private ByteBuffer buffer; |
| 78 | private int encodedWidth; |
| 79 | private int encodedHeight; |
sakal | e172d89 | 2017-08-31 02:37:28 -0700 | [diff] [blame] | 80 | private long captureTimeNs; |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 81 | private EncodedImage.FrameType frameType; |
| 82 | private int rotation; |
| 83 | private boolean completeFrame; |
| 84 | private Integer qp; |
| 85 | |
| 86 | private Builder() {} |
| 87 | |
| 88 | public Builder setBuffer(ByteBuffer buffer) { |
| 89 | this.buffer = buffer; |
| 90 | return this; |
| 91 | } |
| 92 | |
| 93 | public Builder setEncodedWidth(int encodedWidth) { |
| 94 | this.encodedWidth = encodedWidth; |
| 95 | return this; |
| 96 | } |
| 97 | |
| 98 | public Builder setEncodedHeight(int encodedHeight) { |
| 99 | this.encodedHeight = encodedHeight; |
| 100 | return this; |
| 101 | } |
| 102 | |
sakal | e172d89 | 2017-08-31 02:37:28 -0700 | [diff] [blame] | 103 | @Deprecated |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 104 | public Builder setCaptureTimeMs(long captureTimeMs) { |
sakal | e172d89 | 2017-08-31 02:37:28 -0700 | [diff] [blame] | 105 | this.captureTimeNs = TimeUnit.MILLISECONDS.toNanos(captureTimeMs); |
| 106 | return this; |
| 107 | } |
| 108 | |
| 109 | public Builder setCaptureTimeNs(long captureTimeNs) { |
| 110 | this.captureTimeNs = captureTimeNs; |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 111 | return this; |
| 112 | } |
| 113 | |
| 114 | public Builder setFrameType(EncodedImage.FrameType frameType) { |
| 115 | this.frameType = frameType; |
| 116 | return this; |
| 117 | } |
| 118 | |
| 119 | public Builder setRotation(int rotation) { |
| 120 | this.rotation = rotation; |
| 121 | return this; |
| 122 | } |
| 123 | |
| 124 | public Builder setCompleteFrame(boolean completeFrame) { |
| 125 | this.completeFrame = completeFrame; |
| 126 | return this; |
| 127 | } |
| 128 | |
| 129 | public Builder setQp(Integer qp) { |
| 130 | this.qp = qp; |
| 131 | return this; |
| 132 | } |
| 133 | |
| 134 | public EncodedImage createEncodedImage() { |
sakal | e172d89 | 2017-08-31 02:37:28 -0700 | [diff] [blame] | 135 | return new EncodedImage(buffer, encodedWidth, encodedHeight, captureTimeNs, frameType, |
Sami Kalliomäki | 26ecfcc | 2017-06-14 13:46:58 +0200 | [diff] [blame] | 136 | rotation, completeFrame, qp); |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 137 | } |
| 138 | } |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 139 | } |