Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(93)

Side by Side Diff: webrtc/api/android/jni/androidvideotracksource.cc

Issue 2547483003: Move /webrtc/api/android files to /webrtc/sdk/android (Closed)
Patch Set: Move to api folder under Android instead of src Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
11 #include "webrtc/api/android/jni/androidvideotracksource.h"
12
13 #include <utility>
14
15 namespace webrtc {
16
17 AndroidVideoTrackSource::AndroidVideoTrackSource(rtc::Thread* signaling_thread,
18 JNIEnv* jni,
19 jobject j_egl_context,
20 bool is_screencast)
21 : signaling_thread_(signaling_thread),
22 surface_texture_helper_(webrtc_jni::SurfaceTextureHelper::create(
23 jni,
24 "Camera SurfaceTextureHelper",
25 j_egl_context)),
26 is_screencast_(is_screencast) {
27 LOG(LS_INFO) << "AndroidVideoTrackSource ctor";
28 camera_thread_checker_.DetachFromThread();
29 }
30
31 void AndroidVideoTrackSource::SetState(SourceState state) {
32 if (rtc::Thread::Current() != signaling_thread_) {
33 invoker_.AsyncInvoke<void>(
34 RTC_FROM_HERE, signaling_thread_,
35 rtc::Bind(&AndroidVideoTrackSource::SetState, this, state));
36 return;
37 }
38
39 if (state_ != state) {
40 state_ = state;
41 FireOnChanged();
42 }
43 }
44
45 void AndroidVideoTrackSource::OnByteBufferFrameCaptured(const void* frame_data,
46 int length,
47 int width,
48 int height,
49 int rotation,
50 int64_t timestamp_ns) {
51 RTC_DCHECK(camera_thread_checker_.CalledOnValidThread());
52 RTC_DCHECK(rotation == 0 || rotation == 90 || rotation == 180 ||
53 rotation == 270);
54
55 int64_t camera_time_us = timestamp_ns / rtc::kNumNanosecsPerMicrosec;
56 int64_t translated_camera_time_us =
57 timestamp_aligner_.TranslateTimestamp(camera_time_us, rtc::TimeMicros());
58
59 int adapted_width;
60 int adapted_height;
61 int crop_width;
62 int crop_height;
63 int crop_x;
64 int crop_y;
65
66 if (!AdaptFrame(width, height, camera_time_us, &adapted_width,
67 &adapted_height, &crop_width, &crop_height, &crop_x,
68 &crop_y)) {
69 return;
70 }
71
72 const uint8_t* y_plane = static_cast<const uint8_t*>(frame_data);
73 const uint8_t* uv_plane = y_plane + width * height;
74 const int uv_width = (width + 1) / 2;
75
76 RTC_CHECK_GE(length, width * height + 2 * uv_width * ((height + 1) / 2));
77
78 // Can only crop at even pixels.
79 crop_x &= ~1;
80 crop_y &= ~1;
81 // Crop just by modifying pointers.
82 y_plane += width * crop_y + crop_x;
83 uv_plane += uv_width * crop_y + crop_x;
84
85 rtc::scoped_refptr<webrtc::I420Buffer> buffer =
86 buffer_pool_.CreateBuffer(adapted_width, adapted_height);
87
88 nv12toi420_scaler_.NV12ToI420Scale(
89 y_plane, width, uv_plane, uv_width * 2, crop_width, crop_height,
90 buffer->MutableDataY(), buffer->StrideY(),
91 // Swap U and V, since we have NV21, not NV12.
92 buffer->MutableDataV(), buffer->StrideV(), buffer->MutableDataU(),
93 buffer->StrideU(), buffer->width(), buffer->height());
94
95 OnFrame(VideoFrame(buffer, static_cast<webrtc::VideoRotation>(rotation),
96 translated_camera_time_us));
97 }
98
99 void AndroidVideoTrackSource::OnTextureFrameCaptured(
100 int width,
101 int height,
102 int rotation,
103 int64_t timestamp_ns,
104 const webrtc_jni::NativeHandleImpl& handle) {
105 RTC_DCHECK(camera_thread_checker_.CalledOnValidThread());
106 RTC_DCHECK(rotation == 0 || rotation == 90 || rotation == 180 ||
107 rotation == 270);
108
109 int64_t camera_time_us = timestamp_ns / rtc::kNumNanosecsPerMicrosec;
110 int64_t translated_camera_time_us =
111 timestamp_aligner_.TranslateTimestamp(camera_time_us, rtc::TimeMicros());
112
113 int adapted_width;
114 int adapted_height;
115 int crop_width;
116 int crop_height;
117 int crop_x;
118 int crop_y;
119
120 if (!AdaptFrame(width, height, camera_time_us, &adapted_width,
121 &adapted_height, &crop_width, &crop_height, &crop_x,
122 &crop_y)) {
123 surface_texture_helper_->ReturnTextureFrame();
124 return;
125 }
126
127 webrtc_jni::Matrix matrix = handle.sampling_matrix;
128
129 matrix.Crop(crop_width / static_cast<float>(width),
130 crop_height / static_cast<float>(height),
131 crop_x / static_cast<float>(width),
132 crop_y / static_cast<float>(height));
133
134 // Make a local copy, since value of apply_rotation() may change
135 // under our feet.
136 bool do_rotate = apply_rotation();
137
138 if (do_rotate) {
139 if (rotation == webrtc::kVideoRotation_90 ||
140 rotation == webrtc::kVideoRotation_270) {
141 std::swap(adapted_width, adapted_height);
142 }
143 matrix.Rotate(static_cast<webrtc::VideoRotation>(rotation));
144 }
145
146 OnFrame(VideoFrame(
147 surface_texture_helper_->CreateTextureFrame(
148 adapted_width, adapted_height,
149 webrtc_jni::NativeHandleImpl(handle.oes_texture_id, matrix)),
150 do_rotate ? webrtc::kVideoRotation_0
151 : static_cast<webrtc::VideoRotation>(rotation),
152 translated_camera_time_us));
153 }
154
155 void AndroidVideoTrackSource::OnOutputFormatRequest(int width,
156 int height,
157 int fps) {
158 cricket::VideoFormat format(width, height,
159 cricket::VideoFormat::FpsToInterval(fps), 0);
160 video_adapter()->OnOutputFormatRequest(format);
161 }
162
163 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/api/android/jni/androidvideotracksource.h ('k') | webrtc/api/android/jni/androidvideotracksource_jni.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698