blob: c6c56ce5324b455352b6ad49c8fc7cc649b89780 [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 }
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020036 }
37
38 public final ByteBuffer buffer;
39 public final int encodedWidth;
40 public final int encodedHeight;
sakale172d892017-08-31 02:37:28 -070041 public final long captureTimeMs; // Deprecated
42 public final long captureTimeNs;
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020043 public final FrameType frameType;
44 public final int rotation;
45 public final boolean completeFrame;
46 public final Integer qp;
47
sakale172d892017-08-31 02:37:28 -070048 private EncodedImage(ByteBuffer buffer, int encodedWidth, int encodedHeight, long captureTimeNs,
Sami Kalliomäki26ecfcc2017-06-14 13:46:58 +020049 FrameType frameType, int rotation, boolean completeFrame, Integer qp) {
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020050 this.buffer = buffer;
51 this.encodedWidth = encodedWidth;
52 this.encodedHeight = encodedHeight;
sakale172d892017-08-31 02:37:28 -070053 this.captureTimeMs = TimeUnit.NANOSECONDS.toMillis(captureTimeNs);
54 this.captureTimeNs = captureTimeNs;
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020055 this.frameType = frameType;
56 this.rotation = rotation;
57 this.completeFrame = completeFrame;
58 this.qp = qp;
59 }
60
Bjorn Mellem5c4eebb2017-06-12 09:21:03 -070061 public static Builder builder() {
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020062 return new Builder();
63 }
64
Bjorn Mellem5c4eebb2017-06-12 09:21:03 -070065 public static class Builder {
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020066 private ByteBuffer buffer;
67 private int encodedWidth;
68 private int encodedHeight;
sakale172d892017-08-31 02:37:28 -070069 private long captureTimeNs;
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020070 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
sakale172d892017-08-31 02:37:28 -070092 @Deprecated
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020093 public Builder setCaptureTimeMs(long captureTimeMs) {
sakale172d892017-08-31 02:37:28 -070094 this.captureTimeNs = TimeUnit.MILLISECONDS.toNanos(captureTimeMs);
95 return this;
96 }
97
98 public Builder setCaptureTimeNs(long captureTimeNs) {
99 this.captureTimeNs = captureTimeNs;
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200100 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() {
sakale172d892017-08-31 02:37:28 -0700124 return new EncodedImage(buffer, encodedWidth, encodedHeight, captureTimeNs, frameType,
Sami Kalliomäki26ecfcc2017-06-14 13:46:58 +0200125 rotation, completeFrame, qp);
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200126 }
127 }
128}