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; |
Magnus Jedvert | 4eb0188 | 2017-11-20 22:33:40 +0100 | [diff] [blame^] | 15 | // TODO(bugs.webrtc.org/8556): Remove unnecessary import. |
| 16 | import org.webrtc.EncodedImage; |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 17 | |
| 18 | /** |
| 19 | * An encoded frame from a video stream. Used as an input for decoders and as an output for |
| 20 | * encoders. |
| 21 | */ |
| 22 | public class EncodedImage { |
sakal | 07a3bd7 | 2017-09-04 03:57:21 -0700 | [diff] [blame] | 23 | // Must be kept in sync with common_types.h FrameType. |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 24 | public enum FrameType { |
sakal | 07a3bd7 | 2017-09-04 03:57:21 -0700 | [diff] [blame] | 25 | EmptyFrame(0), |
| 26 | VideoFrameKey(3), |
| 27 | VideoFrameDelta(4); |
| 28 | |
| 29 | private final int nativeIndex; |
| 30 | |
| 31 | private FrameType(int nativeIndex) { |
| 32 | this.nativeIndex = nativeIndex; |
| 33 | } |
| 34 | |
| 35 | public int getNative() { |
| 36 | return nativeIndex; |
| 37 | } |
Magnus Jedvert | 4eb0188 | 2017-11-20 22:33:40 +0100 | [diff] [blame^] | 38 | |
| 39 | // TODO(bugs.webrtc.org/8556): Remove unnecessary 'EncodedImage.'. |
| 40 | @CalledByNative("FrameType") |
| 41 | static EncodedImage.FrameType fromNativeIndex(int nativeIndex) { |
| 42 | for (FrameType type : FrameType.values()) { |
| 43 | if (type.getNative() == nativeIndex) { |
| 44 | return type; |
| 45 | } |
| 46 | } |
| 47 | throw new IllegalArgumentException("Unknown native frame type: " + nativeIndex); |
| 48 | } |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | public final ByteBuffer buffer; |
| 52 | public final int encodedWidth; |
| 53 | public final int encodedHeight; |
sakal | e172d89 | 2017-08-31 02:37:28 -0700 | [diff] [blame] | 54 | public final long captureTimeMs; // Deprecated |
| 55 | public final long captureTimeNs; |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 56 | public final FrameType frameType; |
| 57 | public final int rotation; |
| 58 | public final boolean completeFrame; |
| 59 | public final Integer qp; |
| 60 | |
sakal | e172d89 | 2017-08-31 02:37:28 -0700 | [diff] [blame] | 61 | private EncodedImage(ByteBuffer buffer, int encodedWidth, int encodedHeight, long captureTimeNs, |
Sami Kalliomäki | 26ecfcc | 2017-06-14 13:46:58 +0200 | [diff] [blame] | 62 | FrameType frameType, int rotation, boolean completeFrame, Integer qp) { |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 63 | this.buffer = buffer; |
| 64 | this.encodedWidth = encodedWidth; |
| 65 | this.encodedHeight = encodedHeight; |
sakal | e172d89 | 2017-08-31 02:37:28 -0700 | [diff] [blame] | 66 | this.captureTimeMs = TimeUnit.NANOSECONDS.toMillis(captureTimeNs); |
| 67 | this.captureTimeNs = captureTimeNs; |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 68 | this.frameType = frameType; |
| 69 | this.rotation = rotation; |
| 70 | this.completeFrame = completeFrame; |
| 71 | this.qp = qp; |
| 72 | } |
| 73 | |
Bjorn Mellem | 5c4eebb | 2017-06-12 09:21:03 -0700 | [diff] [blame] | 74 | public static Builder builder() { |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 75 | return new Builder(); |
| 76 | } |
| 77 | |
Bjorn Mellem | 5c4eebb | 2017-06-12 09:21:03 -0700 | [diff] [blame] | 78 | public static class Builder { |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 79 | private ByteBuffer buffer; |
| 80 | private int encodedWidth; |
| 81 | private int encodedHeight; |
sakal | e172d89 | 2017-08-31 02:37:28 -0700 | [diff] [blame] | 82 | private long captureTimeNs; |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 83 | private EncodedImage.FrameType frameType; |
| 84 | private int rotation; |
| 85 | private boolean completeFrame; |
| 86 | private Integer qp; |
| 87 | |
| 88 | private Builder() {} |
| 89 | |
| 90 | public Builder setBuffer(ByteBuffer buffer) { |
| 91 | this.buffer = buffer; |
| 92 | return this; |
| 93 | } |
| 94 | |
| 95 | public Builder setEncodedWidth(int encodedWidth) { |
| 96 | this.encodedWidth = encodedWidth; |
| 97 | return this; |
| 98 | } |
| 99 | |
| 100 | public Builder setEncodedHeight(int encodedHeight) { |
| 101 | this.encodedHeight = encodedHeight; |
| 102 | return this; |
| 103 | } |
| 104 | |
sakal | e172d89 | 2017-08-31 02:37:28 -0700 | [diff] [blame] | 105 | @Deprecated |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 106 | public Builder setCaptureTimeMs(long captureTimeMs) { |
sakal | e172d89 | 2017-08-31 02:37:28 -0700 | [diff] [blame] | 107 | this.captureTimeNs = TimeUnit.MILLISECONDS.toNanos(captureTimeMs); |
| 108 | return this; |
| 109 | } |
| 110 | |
| 111 | public Builder setCaptureTimeNs(long captureTimeNs) { |
| 112 | this.captureTimeNs = captureTimeNs; |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 113 | return this; |
| 114 | } |
| 115 | |
| 116 | public Builder setFrameType(EncodedImage.FrameType frameType) { |
| 117 | this.frameType = frameType; |
| 118 | return this; |
| 119 | } |
| 120 | |
| 121 | public Builder setRotation(int rotation) { |
| 122 | this.rotation = rotation; |
| 123 | return this; |
| 124 | } |
| 125 | |
| 126 | public Builder setCompleteFrame(boolean completeFrame) { |
| 127 | this.completeFrame = completeFrame; |
| 128 | return this; |
| 129 | } |
| 130 | |
| 131 | public Builder setQp(Integer qp) { |
| 132 | this.qp = qp; |
| 133 | return this; |
| 134 | } |
| 135 | |
| 136 | public EncodedImage createEncodedImage() { |
sakal | e172d89 | 2017-08-31 02:37:28 -0700 | [diff] [blame] | 137 | return new EncodedImage(buffer, encodedWidth, encodedHeight, captureTimeNs, frameType, |
Sami Kalliomäki | 26ecfcc | 2017-06-14 13:46:58 +0200 | [diff] [blame] | 138 | rotation, completeFrame, qp); |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 139 | } |
| 140 | } |
Magnus Jedvert | 4eb0188 | 2017-11-20 22:33:40 +0100 | [diff] [blame^] | 141 | |
| 142 | // TODO(bugs.webrtc.org/8551) Remove. |
| 143 | @CalledByNative |
| 144 | static EncodedImage create(ByteBuffer buffer, int encodedWidth, int encodedHeight, |
| 145 | long captureTimeNs, EncodedImage.FrameType frameType, int rotation, boolean completeFrame, |
| 146 | Integer qp) { |
| 147 | return new EncodedImage( |
| 148 | buffer, encodedWidth, encodedHeight, captureTimeNs, frameType, rotation, completeFrame, qp); |
| 149 | } |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 150 | } |