blob: 84c72b1db563c6ceb534a9e027fa8618986037fd [file] [log] [blame]
Sami Kalliomäkie2410e92017-06-02 14:46:12 +02001/*
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
11package org.webrtc;
12
13import java.nio.ByteBuffer;
sakale172d892017-08-31 02:37:28 -070014import java.util.concurrent.TimeUnit;
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020015
16/**
17 * An encoded frame from a video stream. Used as an input for decoders and as an output for
18 * encoders.
19 */
20public class EncodedImage {
sakal07a3bd72017-09-04 03:57:21 -070021 // Must be kept in sync with common_types.h FrameType.
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020022 public enum FrameType {
sakal07a3bd72017-09-04 03:57:21 -070023 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 Jedvert4eb01882017-11-20 22:33:40 +010036
Magnus Jedvert4eb01882017-11-20 22:33:40 +010037 @CalledByNative("FrameType")
Magnus Jedvert1f2a3e72017-11-23 16:56:44 +010038 static FrameType fromNativeIndex(int nativeIndex) {
Magnus Jedvert4eb01882017-11-20 22:33:40 +010039 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äkie2410e92017-06-02 14:46:12 +020046 }
47
48 public final ByteBuffer buffer;
49 public final int encodedWidth;
50 public final int encodedHeight;
sakale172d892017-08-31 02:37:28 -070051 public final long captureTimeMs; // Deprecated
52 public final long captureTimeNs;
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020053 public final FrameType frameType;
54 public final int rotation;
55 public final boolean completeFrame;
56 public final Integer qp;
57
Magnus Jedvert1f2a3e72017-11-23 16:56:44 +010058 @CalledByNative
sakale172d892017-08-31 02:37:28 -070059 private EncodedImage(ByteBuffer buffer, int encodedWidth, int encodedHeight, long captureTimeNs,
Sami Kalliomäki26ecfcc2017-06-14 13:46:58 +020060 FrameType frameType, int rotation, boolean completeFrame, Integer qp) {
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020061 this.buffer = buffer;
62 this.encodedWidth = encodedWidth;
63 this.encodedHeight = encodedHeight;
sakale172d892017-08-31 02:37:28 -070064 this.captureTimeMs = TimeUnit.NANOSECONDS.toMillis(captureTimeNs);
65 this.captureTimeNs = captureTimeNs;
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020066 this.frameType = frameType;
67 this.rotation = rotation;
68 this.completeFrame = completeFrame;
69 this.qp = qp;
70 }
71
Bjorn Mellem5c4eebb2017-06-12 09:21:03 -070072 public static Builder builder() {
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020073 return new Builder();
74 }
75
Bjorn Mellem5c4eebb2017-06-12 09:21:03 -070076 public static class Builder {
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020077 private ByteBuffer buffer;
78 private int encodedWidth;
79 private int encodedHeight;
sakale172d892017-08-31 02:37:28 -070080 private long captureTimeNs;
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020081 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
sakale172d892017-08-31 02:37:28 -0700103 @Deprecated
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200104 public Builder setCaptureTimeMs(long captureTimeMs) {
sakale172d892017-08-31 02:37:28 -0700105 this.captureTimeNs = TimeUnit.MILLISECONDS.toNanos(captureTimeMs);
106 return this;
107 }
108
109 public Builder setCaptureTimeNs(long captureTimeNs) {
110 this.captureTimeNs = captureTimeNs;
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200111 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() {
sakale172d892017-08-31 02:37:28 -0700135 return new EncodedImage(buffer, encodedWidth, encodedHeight, captureTimeNs, frameType,
Sami Kalliomäki26ecfcc2017-06-14 13:46:58 +0200136 rotation, completeFrame, qp);
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200137 }
138 }
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200139}