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 | } |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | public final ByteBuffer buffer; |
| 39 | public final int encodedWidth; |
| 40 | public final int encodedHeight; |
sakal | e172d89 | 2017-08-31 02:37:28 -0700 | [diff] [blame] | 41 | public final long captureTimeMs; // Deprecated |
| 42 | public final long captureTimeNs; |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 43 | public final FrameType frameType; |
| 44 | public final int rotation; |
| 45 | public final boolean completeFrame; |
| 46 | public final Integer qp; |
| 47 | |
sakal | e172d89 | 2017-08-31 02:37:28 -0700 | [diff] [blame] | 48 | private EncodedImage(ByteBuffer buffer, int encodedWidth, int encodedHeight, long captureTimeNs, |
Sami Kalliomäki | 26ecfcc | 2017-06-14 13:46:58 +0200 | [diff] [blame] | 49 | FrameType frameType, int rotation, boolean completeFrame, Integer qp) { |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 50 | this.buffer = buffer; |
| 51 | this.encodedWidth = encodedWidth; |
| 52 | this.encodedHeight = encodedHeight; |
sakal | e172d89 | 2017-08-31 02:37:28 -0700 | [diff] [blame] | 53 | this.captureTimeMs = TimeUnit.NANOSECONDS.toMillis(captureTimeNs); |
| 54 | this.captureTimeNs = captureTimeNs; |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 55 | this.frameType = frameType; |
| 56 | this.rotation = rotation; |
| 57 | this.completeFrame = completeFrame; |
| 58 | this.qp = qp; |
| 59 | } |
| 60 | |
Bjorn Mellem | 5c4eebb | 2017-06-12 09:21:03 -0700 | [diff] [blame] | 61 | public static Builder builder() { |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 62 | return new Builder(); |
| 63 | } |
| 64 | |
Bjorn Mellem | 5c4eebb | 2017-06-12 09:21:03 -0700 | [diff] [blame] | 65 | public static class Builder { |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 66 | private ByteBuffer buffer; |
| 67 | private int encodedWidth; |
| 68 | private int encodedHeight; |
sakal | e172d89 | 2017-08-31 02:37:28 -0700 | [diff] [blame] | 69 | private long captureTimeNs; |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 70 | private EncodedImage.FrameType frameType; |
| 71 | private int rotation; |
| 72 | private boolean completeFrame; |
| 73 | private Integer qp; |
| 74 | |
| 75 | private Builder() {} |
| 76 | |
| 77 | public Builder setBuffer(ByteBuffer buffer) { |
| 78 | this.buffer = buffer; |
| 79 | return this; |
| 80 | } |
| 81 | |
| 82 | public Builder setEncodedWidth(int encodedWidth) { |
| 83 | this.encodedWidth = encodedWidth; |
| 84 | return this; |
| 85 | } |
| 86 | |
| 87 | public Builder setEncodedHeight(int encodedHeight) { |
| 88 | this.encodedHeight = encodedHeight; |
| 89 | return this; |
| 90 | } |
| 91 | |
sakal | e172d89 | 2017-08-31 02:37:28 -0700 | [diff] [blame] | 92 | @Deprecated |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 93 | public Builder setCaptureTimeMs(long captureTimeMs) { |
sakal | e172d89 | 2017-08-31 02:37:28 -0700 | [diff] [blame] | 94 | this.captureTimeNs = TimeUnit.MILLISECONDS.toNanos(captureTimeMs); |
| 95 | return this; |
| 96 | } |
| 97 | |
| 98 | public Builder setCaptureTimeNs(long captureTimeNs) { |
| 99 | this.captureTimeNs = captureTimeNs; |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 100 | return this; |
| 101 | } |
| 102 | |
| 103 | public Builder setFrameType(EncodedImage.FrameType frameType) { |
| 104 | this.frameType = frameType; |
| 105 | return this; |
| 106 | } |
| 107 | |
| 108 | public Builder setRotation(int rotation) { |
| 109 | this.rotation = rotation; |
| 110 | return this; |
| 111 | } |
| 112 | |
| 113 | public Builder setCompleteFrame(boolean completeFrame) { |
| 114 | this.completeFrame = completeFrame; |
| 115 | return this; |
| 116 | } |
| 117 | |
| 118 | public Builder setQp(Integer qp) { |
| 119 | this.qp = qp; |
| 120 | return this; |
| 121 | } |
| 122 | |
| 123 | public EncodedImage createEncodedImage() { |
sakal | e172d89 | 2017-08-31 02:37:28 -0700 | [diff] [blame] | 124 | return new EncodedImage(buffer, encodedWidth, encodedHeight, captureTimeNs, frameType, |
Sami Kalliomäki | 26ecfcc | 2017-06-14 13:46:58 +0200 | [diff] [blame] | 125 | rotation, completeFrame, qp); |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | } |