blob: 6720cd7f568c25ab926a0baddbfceac3a36010db [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 }
36
37 public static FrameType fromNative(int nativeIndex) {
38 for (FrameType type : FrameType.values()) {
39 if (type.nativeIndex == nativeIndex) {
40 return type;
41 }
42 }
43 throw new IllegalArgumentException("Unknown native frame type: " + nativeIndex);
44 }
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020045 }
46
47 public final ByteBuffer buffer;
48 public final int encodedWidth;
49 public final int encodedHeight;
sakale172d892017-08-31 02:37:28 -070050 public final long captureTimeMs; // Deprecated
51 public final long captureTimeNs;
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020052 public final FrameType frameType;
53 public final int rotation;
54 public final boolean completeFrame;
55 public final Integer qp;
56
sakale172d892017-08-31 02:37:28 -070057 private EncodedImage(ByteBuffer buffer, int encodedWidth, int encodedHeight, long captureTimeNs,
Sami Kalliomäki26ecfcc2017-06-14 13:46:58 +020058 FrameType frameType, int rotation, boolean completeFrame, Integer qp) {
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020059 this.buffer = buffer;
60 this.encodedWidth = encodedWidth;
61 this.encodedHeight = encodedHeight;
sakale172d892017-08-31 02:37:28 -070062 this.captureTimeMs = TimeUnit.NANOSECONDS.toMillis(captureTimeNs);
63 this.captureTimeNs = captureTimeNs;
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020064 this.frameType = frameType;
65 this.rotation = rotation;
66 this.completeFrame = completeFrame;
67 this.qp = qp;
68 }
69
Bjorn Mellem5c4eebb2017-06-12 09:21:03 -070070 public static Builder builder() {
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020071 return new Builder();
72 }
73
Bjorn Mellem5c4eebb2017-06-12 09:21:03 -070074 public static class Builder {
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020075 private ByteBuffer buffer;
76 private int encodedWidth;
77 private int encodedHeight;
sakale172d892017-08-31 02:37:28 -070078 private long captureTimeNs;
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020079 private EncodedImage.FrameType frameType;
80 private int rotation;
81 private boolean completeFrame;
82 private Integer qp;
83
84 private Builder() {}
85
86 public Builder setBuffer(ByteBuffer buffer) {
87 this.buffer = buffer;
88 return this;
89 }
90
91 public Builder setEncodedWidth(int encodedWidth) {
92 this.encodedWidth = encodedWidth;
93 return this;
94 }
95
96 public Builder setEncodedHeight(int encodedHeight) {
97 this.encodedHeight = encodedHeight;
98 return this;
99 }
100
sakale172d892017-08-31 02:37:28 -0700101 @Deprecated
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200102 public Builder setCaptureTimeMs(long captureTimeMs) {
sakale172d892017-08-31 02:37:28 -0700103 this.captureTimeNs = TimeUnit.MILLISECONDS.toNanos(captureTimeMs);
104 return this;
105 }
106
107 public Builder setCaptureTimeNs(long captureTimeNs) {
108 this.captureTimeNs = captureTimeNs;
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200109 return this;
110 }
111
112 public Builder setFrameType(EncodedImage.FrameType frameType) {
113 this.frameType = frameType;
114 return this;
115 }
116
117 public Builder setRotation(int rotation) {
118 this.rotation = rotation;
119 return this;
120 }
121
122 public Builder setCompleteFrame(boolean completeFrame) {
123 this.completeFrame = completeFrame;
124 return this;
125 }
126
127 public Builder setQp(Integer qp) {
128 this.qp = qp;
129 return this;
130 }
131
132 public EncodedImage createEncodedImage() {
sakale172d892017-08-31 02:37:28 -0700133 return new EncodedImage(buffer, encodedWidth, encodedHeight, captureTimeNs, frameType,
Sami Kalliomäki26ecfcc2017-06-14 13:46:58 +0200134 rotation, completeFrame, qp);
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200135 }
136 }
137}