| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright 2012 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/videocapturertracksource.h" | |
| 12 | |
| 13 #include <cstdlib> | |
| 14 #include <string> | |
| 15 #include <vector> | |
| 16 | |
| 17 #include "webrtc/api/mediaconstraintsinterface.h" | |
| 18 #include "webrtc/base/arraysize.h" | |
| 19 | |
| 20 using cricket::CaptureState; | |
| 21 using webrtc::MediaConstraintsInterface; | |
| 22 using webrtc::MediaSourceInterface; | |
| 23 | |
| 24 namespace { | |
| 25 | |
| 26 const double kRoundingTruncation = 0.0005; | |
| 27 | |
| 28 // Default resolution. If no constraint is specified, this is the resolution we | |
| 29 // will use. | |
| 30 static const cricket::VideoFormatPod kDefaultFormat = { | |
| 31 640, 480, FPS_TO_INTERVAL(30), cricket::FOURCC_ANY}; | |
| 32 | |
| 33 // List of formats used if the camera doesn't support capability enumeration. | |
| 34 static const cricket::VideoFormatPod kVideoFormats[] = { | |
| 35 {1920, 1080, FPS_TO_INTERVAL(30), cricket::FOURCC_ANY}, | |
| 36 {1280, 720, FPS_TO_INTERVAL(30), cricket::FOURCC_ANY}, | |
| 37 {960, 720, FPS_TO_INTERVAL(30), cricket::FOURCC_ANY}, | |
| 38 {640, 360, FPS_TO_INTERVAL(30), cricket::FOURCC_ANY}, | |
| 39 {640, 480, FPS_TO_INTERVAL(30), cricket::FOURCC_ANY}, | |
| 40 {320, 240, FPS_TO_INTERVAL(30), cricket::FOURCC_ANY}, | |
| 41 {320, 180, FPS_TO_INTERVAL(30), cricket::FOURCC_ANY}}; | |
| 42 | |
| 43 MediaSourceInterface::SourceState GetReadyState(cricket::CaptureState state) { | |
| 44 switch (state) { | |
| 45 case cricket::CS_STARTING: | |
| 46 return MediaSourceInterface::kInitializing; | |
| 47 case cricket::CS_RUNNING: | |
| 48 return MediaSourceInterface::kLive; | |
| 49 case cricket::CS_FAILED: | |
| 50 case cricket::CS_STOPPED: | |
| 51 return MediaSourceInterface::kEnded; | |
| 52 default: | |
| 53 ASSERT(false && "GetReadyState unknown state"); | |
| 54 } | |
| 55 return MediaSourceInterface::kEnded; | |
| 56 } | |
| 57 | |
| 58 void SetUpperLimit(int new_limit, int* original_limit) { | |
| 59 if (*original_limit < 0 || new_limit < *original_limit) | |
| 60 *original_limit = new_limit; | |
| 61 } | |
| 62 | |
| 63 // Updates |format_upper_limit| from |constraint|. | |
| 64 // If constraint.maxFoo is smaller than format_upper_limit.foo, | |
| 65 // set format_upper_limit.foo to constraint.maxFoo. | |
| 66 void SetUpperLimitFromConstraint( | |
| 67 const MediaConstraintsInterface::Constraint& constraint, | |
| 68 cricket::VideoFormat* format_upper_limit) { | |
| 69 if (constraint.key == MediaConstraintsInterface::kMaxWidth) { | |
| 70 int value = rtc::FromString<int>(constraint.value); | |
| 71 SetUpperLimit(value, &(format_upper_limit->width)); | |
| 72 } else if (constraint.key == MediaConstraintsInterface::kMaxHeight) { | |
| 73 int value = rtc::FromString<int>(constraint.value); | |
| 74 SetUpperLimit(value, &(format_upper_limit->height)); | |
| 75 } | |
| 76 } | |
| 77 | |
| 78 // Fills |format_out| with the max width and height allowed by |constraints|. | |
| 79 void FromConstraintsForScreencast( | |
| 80 const MediaConstraintsInterface::Constraints& constraints, | |
| 81 cricket::VideoFormat* format_out) { | |
| 82 typedef MediaConstraintsInterface::Constraints::const_iterator | |
| 83 ConstraintsIterator; | |
| 84 | |
| 85 cricket::VideoFormat upper_limit(-1, -1, 0, 0); | |
| 86 for (ConstraintsIterator constraints_it = constraints.begin(); | |
| 87 constraints_it != constraints.end(); ++constraints_it) | |
| 88 SetUpperLimitFromConstraint(*constraints_it, &upper_limit); | |
| 89 | |
| 90 if (upper_limit.width >= 0) | |
| 91 format_out->width = upper_limit.width; | |
| 92 if (upper_limit.height >= 0) | |
| 93 format_out->height = upper_limit.height; | |
| 94 } | |
| 95 | |
| 96 // Returns true if |constraint| is fulfilled. |format_out| can differ from | |
| 97 // |format_in| if the format is changed by the constraint. Ie - the frame rate | |
| 98 // can be changed by setting maxFrameRate. | |
| 99 bool NewFormatWithConstraints( | |
| 100 const MediaConstraintsInterface::Constraint& constraint, | |
| 101 const cricket::VideoFormat& format_in, | |
| 102 bool mandatory, | |
| 103 cricket::VideoFormat* format_out) { | |
| 104 ASSERT(format_out != NULL); | |
| 105 *format_out = format_in; | |
| 106 | |
| 107 if (constraint.key == MediaConstraintsInterface::kMinWidth) { | |
| 108 int value = rtc::FromString<int>(constraint.value); | |
| 109 return (value <= format_in.width); | |
| 110 } else if (constraint.key == MediaConstraintsInterface::kMaxWidth) { | |
| 111 int value = rtc::FromString<int>(constraint.value); | |
| 112 return (value >= format_in.width); | |
| 113 } else if (constraint.key == MediaConstraintsInterface::kMinHeight) { | |
| 114 int value = rtc::FromString<int>(constraint.value); | |
| 115 return (value <= format_in.height); | |
| 116 } else if (constraint.key == MediaConstraintsInterface::kMaxHeight) { | |
| 117 int value = rtc::FromString<int>(constraint.value); | |
| 118 return (value >= format_in.height); | |
| 119 } else if (constraint.key == MediaConstraintsInterface::kMinFrameRate) { | |
| 120 int value = rtc::FromString<int>(constraint.value); | |
| 121 return (value <= cricket::VideoFormat::IntervalToFps(format_in.interval)); | |
| 122 } else if (constraint.key == MediaConstraintsInterface::kMaxFrameRate) { | |
| 123 int value = rtc::FromString<int>(constraint.value); | |
| 124 if (value == 0) { | |
| 125 if (mandatory) { | |
| 126 // TODO(ronghuawu): Convert the constraint value to float when sub-1fps | |
| 127 // is supported by the capturer. | |
| 128 return false; | |
| 129 } else { | |
| 130 value = 1; | |
| 131 } | |
| 132 } | |
| 133 if (value <= cricket::VideoFormat::IntervalToFps(format_in.interval)) | |
| 134 format_out->interval = cricket::VideoFormat::FpsToInterval(value); | |
| 135 return true; | |
| 136 } else if (constraint.key == MediaConstraintsInterface::kMinAspectRatio) { | |
| 137 double value = rtc::FromString<double>(constraint.value); | |
| 138 // The aspect ratio in |constraint.value| has been converted to a string and | |
| 139 // back to a double, so it may have a rounding error. | |
| 140 // E.g if the value 1/3 is converted to a string, the string will not have | |
| 141 // infinite length. | |
| 142 // We add a margin of 0.0005 which is high enough to detect the same aspect | |
| 143 // ratio but small enough to avoid matching wrong aspect ratios. | |
| 144 double ratio = static_cast<double>(format_in.width) / format_in.height; | |
| 145 return (value <= ratio + kRoundingTruncation); | |
| 146 } else if (constraint.key == MediaConstraintsInterface::kMaxAspectRatio) { | |
| 147 double value = rtc::FromString<double>(constraint.value); | |
| 148 double ratio = static_cast<double>(format_in.width) / format_in.height; | |
| 149 // Subtract 0.0005 to avoid rounding problems. Same as above. | |
| 150 const double kRoundingTruncation = 0.0005; | |
| 151 return (value >= ratio - kRoundingTruncation); | |
| 152 } else if (constraint.key == MediaConstraintsInterface::kNoiseReduction) { | |
| 153 // These are actually options, not constraints, so they can be satisfied | |
| 154 // regardless of the format. | |
| 155 return true; | |
| 156 } | |
| 157 LOG(LS_WARNING) << "Found unknown MediaStream constraint. Name:" | |
| 158 << constraint.key << " Value:" << constraint.value; | |
| 159 return false; | |
| 160 } | |
| 161 | |
| 162 // Removes cricket::VideoFormats from |formats| that don't meet |constraint|. | |
| 163 void FilterFormatsByConstraint( | |
| 164 const MediaConstraintsInterface::Constraint& constraint, | |
| 165 bool mandatory, | |
| 166 std::vector<cricket::VideoFormat>* formats) { | |
| 167 std::vector<cricket::VideoFormat>::iterator format_it = formats->begin(); | |
| 168 while (format_it != formats->end()) { | |
| 169 // Modify the format_it to fulfill the constraint if possible. | |
| 170 // Delete it otherwise. | |
| 171 if (!NewFormatWithConstraints(constraint, (*format_it), mandatory, | |
| 172 &(*format_it))) { | |
| 173 format_it = formats->erase(format_it); | |
| 174 } else { | |
| 175 ++format_it; | |
| 176 } | |
| 177 } | |
| 178 } | |
| 179 | |
| 180 // Returns a vector of cricket::VideoFormat that best match |constraints|. | |
| 181 std::vector<cricket::VideoFormat> FilterFormats( | |
| 182 const MediaConstraintsInterface::Constraints& mandatory, | |
| 183 const MediaConstraintsInterface::Constraints& optional, | |
| 184 const std::vector<cricket::VideoFormat>& supported_formats) { | |
| 185 typedef MediaConstraintsInterface::Constraints::const_iterator | |
| 186 ConstraintsIterator; | |
| 187 std::vector<cricket::VideoFormat> candidates = supported_formats; | |
| 188 | |
| 189 for (ConstraintsIterator constraints_it = mandatory.begin(); | |
| 190 constraints_it != mandatory.end(); ++constraints_it) | |
| 191 FilterFormatsByConstraint(*constraints_it, true, &candidates); | |
| 192 | |
| 193 if (candidates.size() == 0) | |
| 194 return candidates; | |
| 195 | |
| 196 // Ok - all mandatory checked and we still have a candidate. | |
| 197 // Let's try filtering using the optional constraints. | |
| 198 for (ConstraintsIterator constraints_it = optional.begin(); | |
| 199 constraints_it != optional.end(); ++constraints_it) { | |
| 200 std::vector<cricket::VideoFormat> current_candidates = candidates; | |
| 201 FilterFormatsByConstraint(*constraints_it, false, ¤t_candidates); | |
| 202 if (current_candidates.size() > 0) { | |
| 203 candidates = current_candidates; | |
| 204 } | |
| 205 } | |
| 206 | |
| 207 // We have done as good as we can to filter the supported resolutions. | |
| 208 return candidates; | |
| 209 } | |
| 210 | |
| 211 // Find the format that best matches the default video size. | |
| 212 // Constraints are optional and since the performance of a video call | |
| 213 // might be bad due to bitrate limitations, CPU, and camera performance, | |
| 214 // it is better to select a resolution that is as close as possible to our | |
| 215 // default and still meets the contraints. | |
| 216 const cricket::VideoFormat& GetBestCaptureFormat( | |
| 217 const std::vector<cricket::VideoFormat>& formats) { | |
| 218 ASSERT(formats.size() > 0); | |
| 219 | |
| 220 int default_area = kDefaultFormat.width * kDefaultFormat.height; | |
| 221 | |
| 222 std::vector<cricket::VideoFormat>::const_iterator it = formats.begin(); | |
| 223 std::vector<cricket::VideoFormat>::const_iterator best_it = formats.begin(); | |
| 224 int best_diff_area = std::abs(default_area - it->width * it->height); | |
| 225 int64_t best_diff_interval = kDefaultFormat.interval; | |
| 226 for (; it != formats.end(); ++it) { | |
| 227 int diff_area = std::abs(default_area - it->width * it->height); | |
| 228 int64_t diff_interval = std::abs(kDefaultFormat.interval - it->interval); | |
| 229 if (diff_area < best_diff_area || | |
| 230 (diff_area == best_diff_area && diff_interval < best_diff_interval)) { | |
| 231 best_diff_area = diff_area; | |
| 232 best_diff_interval = diff_interval; | |
| 233 best_it = it; | |
| 234 } | |
| 235 } | |
| 236 return *best_it; | |
| 237 } | |
| 238 | |
| 239 // Set |option| to the highest-priority value of |key| in the constraints. | |
| 240 // Return false if the key is mandatory, and the value is invalid. | |
| 241 bool ExtractOption(const MediaConstraintsInterface* all_constraints, | |
| 242 const std::string& key, | |
| 243 rtc::Optional<bool>* option) { | |
| 244 size_t mandatory = 0; | |
| 245 bool value; | |
| 246 if (FindConstraint(all_constraints, key, &value, &mandatory)) { | |
| 247 *option = rtc::Optional<bool>(value); | |
| 248 return true; | |
| 249 } | |
| 250 | |
| 251 return mandatory == 0; | |
| 252 } | |
| 253 | |
| 254 } // anonymous namespace | |
| 255 | |
| 256 namespace webrtc { | |
| 257 | |
| 258 rtc::scoped_refptr<VideoTrackSourceInterface> VideoCapturerTrackSource::Create( | |
| 259 rtc::Thread* worker_thread, | |
| 260 cricket::VideoCapturer* capturer, | |
| 261 const webrtc::MediaConstraintsInterface* constraints, | |
| 262 bool remote) { | |
| 263 RTC_DCHECK(worker_thread != NULL); | |
| 264 RTC_DCHECK(capturer != NULL); | |
| 265 rtc::scoped_refptr<VideoCapturerTrackSource> source( | |
| 266 new rtc::RefCountedObject<VideoCapturerTrackSource>(worker_thread, | |
| 267 capturer, remote)); | |
| 268 source->Initialize(constraints); | |
| 269 return source; | |
| 270 } | |
| 271 | |
| 272 rtc::scoped_refptr<VideoTrackSourceInterface> VideoCapturerTrackSource::Create( | |
| 273 rtc::Thread* worker_thread, | |
| 274 cricket::VideoCapturer* capturer, | |
| 275 bool remote) { | |
| 276 RTC_DCHECK(worker_thread != NULL); | |
| 277 RTC_DCHECK(capturer != NULL); | |
| 278 rtc::scoped_refptr<VideoCapturerTrackSource> source( | |
| 279 new rtc::RefCountedObject<VideoCapturerTrackSource>(worker_thread, | |
| 280 capturer, remote)); | |
| 281 source->Initialize(nullptr); | |
| 282 return source; | |
| 283 } | |
| 284 | |
| 285 VideoCapturerTrackSource::VideoCapturerTrackSource( | |
| 286 rtc::Thread* worker_thread, | |
| 287 cricket::VideoCapturer* capturer, | |
| 288 bool remote) | |
| 289 : VideoTrackSource(capturer, remote), | |
| 290 signaling_thread_(rtc::Thread::Current()), | |
| 291 worker_thread_(worker_thread), | |
| 292 video_capturer_(capturer), | |
| 293 started_(false) { | |
| 294 video_capturer_->SignalStateChange.connect( | |
| 295 this, &VideoCapturerTrackSource::OnStateChange); | |
| 296 } | |
| 297 | |
| 298 VideoCapturerTrackSource::~VideoCapturerTrackSource() { | |
| 299 video_capturer_->SignalStateChange.disconnect(this); | |
| 300 Stop(); | |
| 301 } | |
| 302 | |
| 303 void VideoCapturerTrackSource::Initialize( | |
| 304 const webrtc::MediaConstraintsInterface* constraints) { | |
| 305 std::vector<cricket::VideoFormat> formats = | |
| 306 *video_capturer_->GetSupportedFormats(); | |
| 307 if (formats.empty()) { | |
| 308 if (video_capturer_->IsScreencast()) { | |
| 309 // The screen capturer can accept any resolution and we will derive the | |
| 310 // format from the constraints if any. | |
| 311 // Note that this only affects tab capturing, not desktop capturing, | |
| 312 // since the desktop capturer does not respect the VideoFormat passed in. | |
| 313 formats.push_back(cricket::VideoFormat(kDefaultFormat)); | |
| 314 } else { | |
| 315 // The VideoCapturer implementation doesn't support capability | |
| 316 // enumeration. We need to guess what the camera supports. | |
| 317 for (uint32_t i = 0; i < arraysize(kVideoFormats); ++i) { | |
| 318 formats.push_back(cricket::VideoFormat(kVideoFormats[i])); | |
| 319 } | |
| 320 } | |
| 321 } | |
| 322 | |
| 323 if (constraints) { | |
| 324 MediaConstraintsInterface::Constraints mandatory_constraints = | |
| 325 constraints->GetMandatory(); | |
| 326 MediaConstraintsInterface::Constraints optional_constraints; | |
| 327 optional_constraints = constraints->GetOptional(); | |
| 328 | |
| 329 if (video_capturer_->IsScreencast()) { | |
| 330 // Use the maxWidth and maxHeight allowed by constraints for screencast. | |
| 331 FromConstraintsForScreencast(mandatory_constraints, &(formats[0])); | |
| 332 } | |
| 333 | |
| 334 formats = | |
| 335 FilterFormats(mandatory_constraints, optional_constraints, formats); | |
| 336 } | |
| 337 | |
| 338 if (formats.size() == 0) { | |
| 339 LOG(LS_WARNING) << "Failed to find a suitable video format."; | |
| 340 SetState(kEnded); | |
| 341 return; | |
| 342 } | |
| 343 | |
| 344 if (!ExtractOption(constraints, MediaConstraintsInterface::kNoiseReduction, | |
| 345 &needs_denoising_)) { | |
| 346 LOG(LS_WARNING) << "Invalid mandatory value for" | |
| 347 << MediaConstraintsInterface::kNoiseReduction; | |
| 348 SetState(kEnded); | |
| 349 return; | |
| 350 } | |
| 351 | |
| 352 format_ = GetBestCaptureFormat(formats); | |
| 353 // Start the camera with our best guess. | |
| 354 if (!worker_thread_->Invoke<bool>( | |
| 355 RTC_FROM_HERE, rtc::Bind(&cricket::VideoCapturer::StartCapturing, | |
| 356 video_capturer_.get(), format_))) { | |
| 357 SetState(kEnded); | |
| 358 return; | |
| 359 } | |
| 360 started_ = true; | |
| 361 // Initialize hasn't succeeded until a successful state change has occurred. | |
| 362 } | |
| 363 | |
| 364 bool VideoCapturerTrackSource::GetStats(Stats* stats) { | |
| 365 return video_capturer_->GetInputSize(&stats->input_width, | |
| 366 &stats->input_height); | |
| 367 } | |
| 368 | |
| 369 void VideoCapturerTrackSource::Stop() { | |
| 370 if (!started_) { | |
| 371 return; | |
| 372 } | |
| 373 started_ = false; | |
| 374 worker_thread_->Invoke<void>( | |
| 375 RTC_FROM_HERE, | |
| 376 rtc::Bind(&cricket::VideoCapturer::Stop, video_capturer_.get())); | |
| 377 } | |
| 378 | |
| 379 // OnStateChange listens to the cricket::VideoCapturer::SignalStateChange. | |
| 380 void VideoCapturerTrackSource::OnStateChange( | |
| 381 cricket::VideoCapturer* capturer, | |
| 382 cricket::CaptureState capture_state) { | |
| 383 if (rtc::Thread::Current() != signaling_thread_) { | |
| 384 invoker_.AsyncInvoke<void>( | |
| 385 RTC_FROM_HERE, signaling_thread_, | |
| 386 rtc::Bind(&VideoCapturerTrackSource::OnStateChange, this, capturer, | |
| 387 capture_state)); | |
| 388 return; | |
| 389 } | |
| 390 | |
| 391 if (capturer == video_capturer_.get()) { | |
| 392 SetState(GetReadyState(capture_state)); | |
| 393 } | |
| 394 } | |
| 395 | |
| 396 } // namespace webrtc | |
| OLD | NEW |