Sami Kalliomaki | 1603212 | 2016-07-20 16:13:08 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "sdk/android/src/jni/androidvideotracksource.h" |
Sami Kalliomaki | 1603212 | 2016-07-20 16:13:08 +0200 | [diff] [blame] | 12 | |
| 13 | #include <utility> |
| 14 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "rtc_base/logging.h" |
| 16 | #include "sdk/android/src/jni/classreferenceholder.h" |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 17 | |
kthelgason | c847417 | 2016-12-08 08:04:51 -0800 | [diff] [blame] | 18 | namespace { |
| 19 | // MediaCodec wants resolution to be divisible by 2. |
| 20 | const int kRequiredResolutionAlignment = 2; |
| 21 | } |
| 22 | |
Sami Kalliomaki | 1603212 | 2016-07-20 16:13:08 +0200 | [diff] [blame] | 23 | namespace webrtc { |
magjed | a3d4f68 | 2017-08-28 16:24:06 -0700 | [diff] [blame] | 24 | namespace jni { |
Sami Kalliomaki | 1603212 | 2016-07-20 16:13:08 +0200 | [diff] [blame] | 25 | |
Sami Kalliomäki | 58c742c | 2017-06-02 09:51:39 +0200 | [diff] [blame] | 26 | AndroidVideoTrackSource::AndroidVideoTrackSource( |
| 27 | rtc::Thread* signaling_thread, |
| 28 | JNIEnv* jni, |
| 29 | jobject j_surface_texture_helper, |
| 30 | bool is_screencast) |
kthelgason | c847417 | 2016-12-08 08:04:51 -0800 | [diff] [blame] | 31 | : AdaptedVideoTrackSource(kRequiredResolutionAlignment), |
| 32 | signaling_thread_(signaling_thread), |
magjed | a3d4f68 | 2017-08-28 16:24:06 -0700 | [diff] [blame] | 33 | surface_texture_helper_(new rtc::RefCountedObject<SurfaceTextureHelper>( |
| 34 | jni, |
| 35 | j_surface_texture_helper)), |
arsany | b75f254 | 2016-08-31 18:50:52 -0700 | [diff] [blame] | 36 | is_screencast_(is_screencast) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 37 | RTC_LOG(LS_INFO) << "AndroidVideoTrackSource ctor"; |
Sami Kalliomaki | 1603212 | 2016-07-20 16:13:08 +0200 | [diff] [blame] | 38 | camera_thread_checker_.DetachFromThread(); |
| 39 | } |
| 40 | |
Sami Kalliomaki | 1603212 | 2016-07-20 16:13:08 +0200 | [diff] [blame] | 41 | void AndroidVideoTrackSource::SetState(SourceState state) { |
| 42 | if (rtc::Thread::Current() != signaling_thread_) { |
| 43 | invoker_.AsyncInvoke<void>( |
| 44 | RTC_FROM_HERE, signaling_thread_, |
| 45 | rtc::Bind(&AndroidVideoTrackSource::SetState, this, state)); |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | if (state_ != state) { |
| 50 | state_ = state; |
| 51 | FireOnChanged(); |
| 52 | } |
| 53 | } |
| 54 | |
Sami Kalliomaki | 1603212 | 2016-07-20 16:13:08 +0200 | [diff] [blame] | 55 | void AndroidVideoTrackSource::OnByteBufferFrameCaptured(const void* frame_data, |
| 56 | int length, |
| 57 | int width, |
| 58 | int height, |
Magnus Jedvert | 3093ef1 | 2017-06-19 15:04:19 +0200 | [diff] [blame] | 59 | VideoRotation rotation, |
Sami Kalliomaki | 1603212 | 2016-07-20 16:13:08 +0200 | [diff] [blame] | 60 | int64_t timestamp_ns) { |
| 61 | RTC_DCHECK(camera_thread_checker_.CalledOnValidThread()); |
Sami Kalliomaki | 1603212 | 2016-07-20 16:13:08 +0200 | [diff] [blame] | 62 | |
nisse | 6f5a6c3 | 2016-09-22 01:25:59 -0700 | [diff] [blame] | 63 | int64_t camera_time_us = timestamp_ns / rtc::kNumNanosecsPerMicrosec; |
| 64 | int64_t translated_camera_time_us = |
| 65 | timestamp_aligner_.TranslateTimestamp(camera_time_us, rtc::TimeMicros()); |
| 66 | |
Sami Kalliomaki | 1603212 | 2016-07-20 16:13:08 +0200 | [diff] [blame] | 67 | int adapted_width; |
| 68 | int adapted_height; |
| 69 | int crop_width; |
| 70 | int crop_height; |
| 71 | int crop_x; |
| 72 | int crop_y; |
Sami Kalliomaki | 1603212 | 2016-07-20 16:13:08 +0200 | [diff] [blame] | 73 | |
sakal | 0ce6aaf | 2016-11-22 01:26:16 -0800 | [diff] [blame] | 74 | if (!AdaptFrame(width, height, camera_time_us, &adapted_width, |
| 75 | &adapted_height, &crop_width, &crop_height, &crop_x, |
| 76 | &crop_y)) { |
Sami Kalliomaki | 1603212 | 2016-07-20 16:13:08 +0200 | [diff] [blame] | 77 | return; |
| 78 | } |
| 79 | |
Sami Kalliomaki | 1603212 | 2016-07-20 16:13:08 +0200 | [diff] [blame] | 80 | const uint8_t* y_plane = static_cast<const uint8_t*>(frame_data); |
| 81 | const uint8_t* uv_plane = y_plane + width * height; |
magjed | 1a7ef1f | 2016-09-17 02:39:03 -0700 | [diff] [blame] | 82 | const int uv_width = (width + 1) / 2; |
Sami Kalliomaki | 1603212 | 2016-07-20 16:13:08 +0200 | [diff] [blame] | 83 | |
| 84 | RTC_CHECK_GE(length, width * height + 2 * uv_width * ((height + 1) / 2)); |
| 85 | |
| 86 | // Can only crop at even pixels. |
| 87 | crop_x &= ~1; |
| 88 | crop_y &= ~1; |
magjed | 1a7ef1f | 2016-09-17 02:39:03 -0700 | [diff] [blame] | 89 | // Crop just by modifying pointers. |
| 90 | y_plane += width * crop_y + crop_x; |
| 91 | uv_plane += uv_width * crop_y + crop_x; |
Sami Kalliomaki | 1603212 | 2016-07-20 16:13:08 +0200 | [diff] [blame] | 92 | |
Magnus Jedvert | 3093ef1 | 2017-06-19 15:04:19 +0200 | [diff] [blame] | 93 | rtc::scoped_refptr<I420Buffer> buffer = |
magjed | 1a7ef1f | 2016-09-17 02:39:03 -0700 | [diff] [blame] | 94 | buffer_pool_.CreateBuffer(adapted_width, adapted_height); |
| 95 | |
| 96 | nv12toi420_scaler_.NV12ToI420Scale( |
sakal | 0ce6aaf | 2016-11-22 01:26:16 -0800 | [diff] [blame] | 97 | y_plane, width, uv_plane, uv_width * 2, crop_width, crop_height, |
magjed | 1a7ef1f | 2016-09-17 02:39:03 -0700 | [diff] [blame] | 98 | buffer->MutableDataY(), buffer->StrideY(), |
jackychen | ed0b0db | 2016-09-09 16:15:11 -0700 | [diff] [blame] | 99 | // Swap U and V, since we have NV21, not NV12. |
sakal | 0ce6aaf | 2016-11-22 01:26:16 -0800 | [diff] [blame] | 100 | buffer->MutableDataV(), buffer->StrideV(), buffer->MutableDataU(), |
| 101 | buffer->StrideU(), buffer->width(), buffer->height()); |
Sami Kalliomaki | 1603212 | 2016-07-20 16:13:08 +0200 | [diff] [blame] | 102 | |
Magnus Jedvert | 3093ef1 | 2017-06-19 15:04:19 +0200 | [diff] [blame] | 103 | OnFrame(VideoFrame(buffer, rotation, translated_camera_time_us)); |
Sami Kalliomaki | 1603212 | 2016-07-20 16:13:08 +0200 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | void AndroidVideoTrackSource::OnTextureFrameCaptured( |
| 107 | int width, |
| 108 | int height, |
Magnus Jedvert | 3093ef1 | 2017-06-19 15:04:19 +0200 | [diff] [blame] | 109 | VideoRotation rotation, |
Sami Kalliomaki | 1603212 | 2016-07-20 16:13:08 +0200 | [diff] [blame] | 110 | int64_t timestamp_ns, |
magjed | a3d4f68 | 2017-08-28 16:24:06 -0700 | [diff] [blame] | 111 | const NativeHandleImpl& handle) { |
Sami Kalliomaki | 1603212 | 2016-07-20 16:13:08 +0200 | [diff] [blame] | 112 | RTC_DCHECK(camera_thread_checker_.CalledOnValidThread()); |
Sami Kalliomaki | 1603212 | 2016-07-20 16:13:08 +0200 | [diff] [blame] | 113 | |
nisse | 6f5a6c3 | 2016-09-22 01:25:59 -0700 | [diff] [blame] | 114 | int64_t camera_time_us = timestamp_ns / rtc::kNumNanosecsPerMicrosec; |
| 115 | int64_t translated_camera_time_us = |
| 116 | timestamp_aligner_.TranslateTimestamp(camera_time_us, rtc::TimeMicros()); |
| 117 | |
Sami Kalliomaki | 1603212 | 2016-07-20 16:13:08 +0200 | [diff] [blame] | 118 | int adapted_width; |
| 119 | int adapted_height; |
| 120 | int crop_width; |
| 121 | int crop_height; |
| 122 | int crop_x; |
| 123 | int crop_y; |
Sami Kalliomaki | 1603212 | 2016-07-20 16:13:08 +0200 | [diff] [blame] | 124 | |
sakal | 0ce6aaf | 2016-11-22 01:26:16 -0800 | [diff] [blame] | 125 | if (!AdaptFrame(width, height, camera_time_us, &adapted_width, |
| 126 | &adapted_height, &crop_width, &crop_height, &crop_x, |
| 127 | &crop_y)) { |
Sami Kalliomaki | 1603212 | 2016-07-20 16:13:08 +0200 | [diff] [blame] | 128 | surface_texture_helper_->ReturnTextureFrame(); |
| 129 | return; |
| 130 | } |
| 131 | |
magjed | a3d4f68 | 2017-08-28 16:24:06 -0700 | [diff] [blame] | 132 | Matrix matrix = handle.sampling_matrix; |
Sami Kalliomaki | 1603212 | 2016-07-20 16:13:08 +0200 | [diff] [blame] | 133 | |
| 134 | matrix.Crop(crop_width / static_cast<float>(width), |
| 135 | crop_height / static_cast<float>(height), |
| 136 | crop_x / static_cast<float>(width), |
| 137 | crop_y / static_cast<float>(height)); |
| 138 | |
Magnus Jedvert | 3093ef1 | 2017-06-19 15:04:19 +0200 | [diff] [blame] | 139 | // Note that apply_rotation() may change under our feet, so we should only |
| 140 | // check once. |
| 141 | if (apply_rotation()) { |
| 142 | if (rotation == kVideoRotation_90 || rotation == kVideoRotation_270) { |
Sami Kalliomaki | 1603212 | 2016-07-20 16:13:08 +0200 | [diff] [blame] | 143 | std::swap(adapted_width, adapted_height); |
| 144 | } |
Magnus Jedvert | 3093ef1 | 2017-06-19 15:04:19 +0200 | [diff] [blame] | 145 | matrix.Rotate(rotation); |
| 146 | rotation = kVideoRotation_0; |
Sami Kalliomaki | 1603212 | 2016-07-20 16:13:08 +0200 | [diff] [blame] | 147 | } |
| 148 | |
magjed | a3d4f68 | 2017-08-28 16:24:06 -0700 | [diff] [blame] | 149 | OnFrame(VideoFrame(surface_texture_helper_->CreateTextureFrame( |
| 150 | adapted_width, adapted_height, |
| 151 | NativeHandleImpl(handle.oes_texture_id, matrix)), |
| 152 | rotation, translated_camera_time_us)); |
Sami Kalliomaki | 1603212 | 2016-07-20 16:13:08 +0200 | [diff] [blame] | 153 | } |
| 154 | |
sakal | be91046 | 2017-08-11 00:26:05 -0700 | [diff] [blame] | 155 | void AndroidVideoTrackSource::OnFrameCaptured(JNIEnv* jni, |
| 156 | int width, |
| 157 | int height, |
| 158 | int64_t timestamp_ns, |
| 159 | VideoRotation rotation, |
| 160 | jobject j_video_frame_buffer) { |
| 161 | RTC_DCHECK(camera_thread_checker_.CalledOnValidThread()); |
| 162 | |
| 163 | int64_t camera_time_us = timestamp_ns / rtc::kNumNanosecsPerMicrosec; |
| 164 | int64_t translated_camera_time_us = |
| 165 | timestamp_aligner_.TranslateTimestamp(camera_time_us, rtc::TimeMicros()); |
| 166 | |
| 167 | int adapted_width; |
| 168 | int adapted_height; |
| 169 | int crop_width; |
| 170 | int crop_height; |
| 171 | int crop_x; |
| 172 | int crop_y; |
| 173 | |
| 174 | if (!AdaptFrame(width, height, camera_time_us, &adapted_width, |
| 175 | &adapted_height, &crop_width, &crop_height, &crop_x, |
| 176 | &crop_y)) { |
| 177 | return; |
| 178 | } |
| 179 | |
sakal | be91046 | 2017-08-11 00:26:05 -0700 | [diff] [blame] | 180 | rtc::scoped_refptr<VideoFrameBuffer> buffer = |
Magnus Jedvert | 202be39 | 2017-11-18 16:09:17 +0100 | [diff] [blame^] | 181 | AndroidVideoBuffer::Create(jni, j_video_frame_buffer) |
| 182 | ->CropAndScale(jni, crop_x, crop_y, crop_width, crop_height, |
| 183 | adapted_width, adapted_height); |
sakal | be91046 | 2017-08-11 00:26:05 -0700 | [diff] [blame] | 184 | |
| 185 | // AdaptedVideoTrackSource handles applying rotation for I420 frames. |
Sami Kalliomäki | cbc4b1d | 2017-09-29 11:50:25 +0200 | [diff] [blame] | 186 | if (apply_rotation() && rotation != kVideoRotation_0) { |
sakal | be91046 | 2017-08-11 00:26:05 -0700 | [diff] [blame] | 187 | buffer = buffer->ToI420(); |
| 188 | } |
| 189 | |
| 190 | OnFrame(VideoFrame(buffer, rotation, translated_camera_time_us)); |
| 191 | } |
| 192 | |
Sami Kalliomaki | 1603212 | 2016-07-20 16:13:08 +0200 | [diff] [blame] | 193 | void AndroidVideoTrackSource::OnOutputFormatRequest(int width, |
| 194 | int height, |
| 195 | int fps) { |
Sami Kalliomaki | 1603212 | 2016-07-20 16:13:08 +0200 | [diff] [blame] | 196 | cricket::VideoFormat format(width, height, |
| 197 | cricket::VideoFormat::FpsToInterval(fps), 0); |
nisse | 6f5a6c3 | 2016-09-22 01:25:59 -0700 | [diff] [blame] | 198 | video_adapter()->OnOutputFormatRequest(format); |
Sami Kalliomaki | 1603212 | 2016-07-20 16:13:08 +0200 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | } // namespace webrtc |
magjed | a3d4f68 | 2017-08-28 16:24:06 -0700 | [diff] [blame] | 202 | } // namespace webrtc |