OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/video/capture/android/video_capture_device_android.h" | 5 #include "media/video/capture/android/video_capture_device_android.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
10 #include "base/android/jni_string.h" | 10 #include "base/android/jni_string.h" |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 return ret.release(); | 79 return ret.release(); |
80 return NULL; | 80 return NULL; |
81 } | 81 } |
82 | 82 |
83 // static | 83 // static |
84 bool VideoCaptureDeviceAndroid::RegisterVideoCaptureDevice(JNIEnv* env) { | 84 bool VideoCaptureDeviceAndroid::RegisterVideoCaptureDevice(JNIEnv* env) { |
85 return RegisterNativesImpl(env); | 85 return RegisterNativesImpl(env); |
86 } | 86 } |
87 | 87 |
88 VideoCaptureDeviceAndroid::VideoCaptureDeviceAndroid(const Name& device_name) | 88 VideoCaptureDeviceAndroid::VideoCaptureDeviceAndroid(const Name& device_name) |
89 : state_(kIdle), | 89 : state_(kIdle), got_first_frame_(false), device_name_(device_name) {} |
90 got_first_frame_(false), | |
91 device_name_(device_name), | |
92 current_settings_() { | |
93 } | |
94 | 90 |
95 VideoCaptureDeviceAndroid::~VideoCaptureDeviceAndroid() { | 91 VideoCaptureDeviceAndroid::~VideoCaptureDeviceAndroid() { |
96 StopAndDeAllocate(); | 92 StopAndDeAllocate(); |
97 } | 93 } |
98 | 94 |
99 bool VideoCaptureDeviceAndroid::Init() { | 95 bool VideoCaptureDeviceAndroid::Init() { |
100 int id; | 96 int id; |
101 if (!base::StringToInt(device_name_.id(), &id)) | 97 if (!base::StringToInt(device_name_.id(), &id)) |
102 return false; | 98 return false; |
103 | 99 |
104 JNIEnv* env = AttachCurrentThread(); | 100 JNIEnv* env = AttachCurrentThread(); |
105 | 101 |
106 j_capture_.Reset(Java_VideoCapture_createVideoCapture( | 102 j_capture_.Reset(Java_VideoCapture_createVideoCapture( |
107 env, base::android::GetApplicationContext(), id, | 103 env, base::android::GetApplicationContext(), id, |
108 reinterpret_cast<intptr_t>(this))); | 104 reinterpret_cast<intptr_t>(this))); |
109 | 105 |
110 return true; | 106 return true; |
111 } | 107 } |
112 | 108 |
113 void VideoCaptureDeviceAndroid::AllocateAndStart( | 109 void VideoCaptureDeviceAndroid::AllocateAndStart( |
114 const VideoCaptureCapability& capture_format, | 110 const VideoCaptureParams& params, |
115 scoped_ptr<Client> client) { | 111 scoped_ptr<Client> client) { |
116 DVLOG(1) << "VideoCaptureDeviceAndroid::AllocateAndStart"; | 112 DVLOG(1) << "VideoCaptureDeviceAndroid::AllocateAndStart"; |
117 { | 113 { |
118 base::AutoLock lock(lock_); | 114 base::AutoLock lock(lock_); |
119 if (state_ != kIdle) | 115 if (state_ != kIdle) |
120 return; | 116 return; |
121 client_ = client.Pass(); | 117 client_ = client.Pass(); |
122 got_first_frame_ = false; | 118 got_first_frame_ = false; |
123 } | 119 } |
124 | 120 |
125 JNIEnv* env = AttachCurrentThread(); | 121 JNIEnv* env = AttachCurrentThread(); |
126 | 122 |
127 jboolean ret = Java_VideoCapture_allocate(env, | 123 jboolean ret = |
128 j_capture_.obj(), | 124 Java_VideoCapture_allocate(env, |
129 capture_format.width, | 125 j_capture_.obj(), |
130 capture_format.height, | 126 params.requested_format.frame_size.width(), |
131 capture_format.frame_rate); | 127 params.requested_format.frame_size.height(), |
| 128 params.requested_format.frame_rate); |
132 if (!ret) { | 129 if (!ret) { |
133 SetErrorState("failed to allocate"); | 130 SetErrorState("failed to allocate"); |
134 return; | 131 return; |
135 } | 132 } |
136 | 133 |
137 // Store current width and height. | 134 // Store current width and height. |
138 current_settings_.width = | 135 capture_format_.frame_size.SetSize( |
139 Java_VideoCapture_queryWidth(env, j_capture_.obj()); | 136 Java_VideoCapture_queryWidth(env, j_capture_.obj()), |
140 current_settings_.height = | 137 Java_VideoCapture_queryHeight(env, j_capture_.obj())); |
141 Java_VideoCapture_queryHeight(env, j_capture_.obj()); | 138 capture_format_.frame_rate = |
142 current_settings_.frame_rate = | |
143 Java_VideoCapture_queryFrameRate(env, j_capture_.obj()); | 139 Java_VideoCapture_queryFrameRate(env, j_capture_.obj()); |
144 current_settings_.color = GetColorspace(); | 140 capture_format_.pixel_format = GetColorspace(); |
145 DCHECK_NE(current_settings_.color, media::PIXEL_FORMAT_UNKNOWN); | 141 DCHECK_NE(capture_format_.pixel_format, media::PIXEL_FORMAT_UNKNOWN); |
146 CHECK(current_settings_.width > 0 && !(current_settings_.width % 2)); | 142 CHECK(capture_format_.frame_size.GetArea() > 0); |
147 CHECK(current_settings_.height > 0 && !(current_settings_.height % 2)); | 143 CHECK(!(capture_format_.frame_size.width() % 2)); |
| 144 CHECK(!(capture_format_.frame_size.height() % 2)); |
148 | 145 |
149 if (capture_format.frame_rate > 0) { | 146 if (capture_format_.frame_rate > 0) { |
150 frame_interval_ = base::TimeDelta::FromMicroseconds( | 147 frame_interval_ = base::TimeDelta::FromMicroseconds( |
151 (base::Time::kMicrosecondsPerSecond + capture_format.frame_rate - 1) / | 148 (base::Time::kMicrosecondsPerSecond + capture_format_.frame_rate - 1) / |
152 capture_format.frame_rate); | 149 capture_format_.frame_rate); |
153 } | 150 } |
154 | 151 |
155 DVLOG(1) << "VideoCaptureDeviceAndroid::Allocate: queried width=" | 152 DVLOG(1) << "VideoCaptureDeviceAndroid::Allocate: queried frame_size=" |
156 << current_settings_.width | 153 << capture_format_.frame_size.ToString() |
157 << ", height=" | 154 << ", frame_rate=" << capture_format_.frame_rate; |
158 << current_settings_.height | |
159 << ", frame_rate=" | |
160 << current_settings_.frame_rate; | |
161 | 155 |
162 jint result = Java_VideoCapture_startCapture(env, j_capture_.obj()); | 156 jint result = Java_VideoCapture_startCapture(env, j_capture_.obj()); |
163 if (result < 0) { | 157 if (result < 0) { |
164 SetErrorState("failed to start capture"); | 158 SetErrorState("failed to start capture"); |
165 return; | 159 return; |
166 } | 160 } |
167 | 161 |
168 { | 162 { |
169 base::AutoLock lock(lock_); | 163 base::AutoLock lock(lock_); |
170 state_ = kCapturing; | 164 state_ = kCapturing; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 // Deliver the frame when it doesn't arrive too early. | 221 // Deliver the frame when it doesn't arrive too early. |
228 if (expected_next_frame_time_ <= current_time) { | 222 if (expected_next_frame_time_ <= current_time) { |
229 expected_next_frame_time_ += frame_interval_; | 223 expected_next_frame_time_ += frame_interval_; |
230 | 224 |
231 client_->OnIncomingCapturedFrame(reinterpret_cast<uint8*>(buffer), | 225 client_->OnIncomingCapturedFrame(reinterpret_cast<uint8*>(buffer), |
232 length, | 226 length, |
233 base::Time::Now(), | 227 base::Time::Now(), |
234 rotation, | 228 rotation, |
235 flip_vert, | 229 flip_vert, |
236 flip_horiz, | 230 flip_horiz, |
237 current_settings_); | 231 capture_format_); |
238 } | 232 } |
239 | 233 |
240 env->ReleaseByteArrayElements(data, buffer, JNI_ABORT); | 234 env->ReleaseByteArrayElements(data, buffer, JNI_ABORT); |
241 } | 235 } |
242 | 236 |
243 VideoPixelFormat VideoCaptureDeviceAndroid::GetColorspace() { | 237 VideoPixelFormat VideoCaptureDeviceAndroid::GetColorspace() { |
244 JNIEnv* env = AttachCurrentThread(); | 238 JNIEnv* env = AttachCurrentThread(); |
245 int current_capture_colorspace = | 239 int current_capture_colorspace = |
246 Java_VideoCapture_getColorspace(env, j_capture_.obj()); | 240 Java_VideoCapture_getColorspace(env, j_capture_.obj()); |
247 switch (current_capture_colorspace){ | 241 switch (current_capture_colorspace){ |
(...skipping 16 matching lines...) Expand all Loading... |
264 void VideoCaptureDeviceAndroid::SetErrorState(const std::string& reason) { | 258 void VideoCaptureDeviceAndroid::SetErrorState(const std::string& reason) { |
265 LOG(ERROR) << "VideoCaptureDeviceAndroid::SetErrorState: " << reason; | 259 LOG(ERROR) << "VideoCaptureDeviceAndroid::SetErrorState: " << reason; |
266 { | 260 { |
267 base::AutoLock lock(lock_); | 261 base::AutoLock lock(lock_); |
268 state_ = kError; | 262 state_ = kError; |
269 } | 263 } |
270 client_->OnError(); | 264 client_->OnError(); |
271 } | 265 } |
272 | 266 |
273 } // namespace media | 267 } // namespace media |
OLD | NEW |