PageRenderTime 53ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/media/webrtc/trunk/webrtc/api/mediaconstraintsinterface.cc

https://bitbucket.org/vionika/spin.android
C++ | 275 lines | 216 code | 22 blank | 37 comment | 14 complexity | 424ad5c8d5ed97c33909d8f39f58543c MD5 | raw file
Possible License(s): JSON, 0BSD, AGPL-1.0, BSD-2-Clause, GPL-3.0, LGPL-2.1, LGPL-3.0, CC0-1.0, AGPL-3.0, MPL-2.0, Apache-2.0, MIT, BSD-3-Clause, MPL-2.0-no-copyleft-exception, GPL-2.0, Unlicense
  1. /*
  2. * Copyright 2013 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. #include "api/mediaconstraintsinterface.h"
  11. #include "api/peerconnectioninterface.h"
  12. #include "rtc_base/stringencode.h"
  13. namespace {
  14. // Find the highest-priority instance of the T-valued constraint named by
  15. // |key| and return its value as |value|. |constraints| can be null.
  16. // If |mandatory_constraints| is non-null, it is incremented if the key appears
  17. // among the mandatory constraints.
  18. // Returns true if the key was found and has a valid value for type T.
  19. // If the key appears multiple times as an optional constraint, appearances
  20. // after the first are ignored.
  21. // Note: Because this uses FindFirst, repeated optional constraints whose
  22. // first instance has an unrecognized value are not handled precisely in
  23. // accordance with the specification.
  24. template <typename T>
  25. bool FindConstraint(const webrtc::MediaConstraintsInterface* constraints,
  26. const std::string& key,
  27. T* value,
  28. size_t* mandatory_constraints) {
  29. std::string string_value;
  30. if (!FindConstraint(constraints, key, &string_value, mandatory_constraints)) {
  31. return false;
  32. }
  33. return rtc::FromString(string_value, value);
  34. }
  35. // Specialization for std::string, since a string doesn't need conversion.
  36. template <>
  37. bool FindConstraint(const webrtc::MediaConstraintsInterface* constraints,
  38. const std::string& key,
  39. std::string* value,
  40. size_t* mandatory_constraints) {
  41. if (!constraints) {
  42. return false;
  43. }
  44. if (constraints->GetMandatory().FindFirst(key, value)) {
  45. if (mandatory_constraints) {
  46. ++*mandatory_constraints;
  47. }
  48. return true;
  49. }
  50. if (constraints->GetOptional().FindFirst(key, value)) {
  51. return true;
  52. }
  53. return false;
  54. }
  55. // Converts a constraint (mandatory takes precedence over optional) to an
  56. // rtc::Optional.
  57. template <typename T>
  58. void ConstraintToOptional(const webrtc::MediaConstraintsInterface* constraints,
  59. const std::string& key,
  60. rtc::Optional<T>* value_out) {
  61. T value;
  62. bool present = FindConstraint<T>(constraints, key, &value, nullptr);
  63. if (present) {
  64. *value_out = value;
  65. }
  66. }
  67. } // namespace
  68. namespace webrtc {
  69. const char MediaConstraintsInterface::kValueTrue[] = "true";
  70. const char MediaConstraintsInterface::kValueFalse[] = "false";
  71. // Constraints declared as static members in mediastreaminterface.h
  72. // Specified by draft-alvestrand-constraints-resolution-00b
  73. const char MediaConstraintsInterface::kMinAspectRatio[] = "minAspectRatio";
  74. const char MediaConstraintsInterface::kMaxAspectRatio[] = "maxAspectRatio";
  75. const char MediaConstraintsInterface::kMaxWidth[] = "maxWidth";
  76. const char MediaConstraintsInterface::kMinWidth[] = "minWidth";
  77. const char MediaConstraintsInterface::kMaxHeight[] = "maxHeight";
  78. const char MediaConstraintsInterface::kMinHeight[] = "minHeight";
  79. const char MediaConstraintsInterface::kMaxFrameRate[] = "maxFrameRate";
  80. const char MediaConstraintsInterface::kMinFrameRate[] = "minFrameRate";
  81. // Audio constraints.
  82. const char MediaConstraintsInterface::kEchoCancellation[] =
  83. "echoCancellation";
  84. const char MediaConstraintsInterface::kGoogEchoCancellation[] =
  85. "googEchoCancellation";
  86. const char MediaConstraintsInterface::kExtendedFilterEchoCancellation[] =
  87. "googEchoCancellation2";
  88. const char MediaConstraintsInterface::kDAEchoCancellation[] =
  89. "googDAEchoCancellation";
  90. const char MediaConstraintsInterface::kAutoGainControl[] =
  91. "googAutoGainControl";
  92. const char MediaConstraintsInterface::kExperimentalAutoGainControl[] =
  93. "googAutoGainControl2";
  94. const char MediaConstraintsInterface::kNoiseSuppression[] =
  95. "googNoiseSuppression";
  96. const char MediaConstraintsInterface::kExperimentalNoiseSuppression[] =
  97. "googNoiseSuppression2";
  98. const char MediaConstraintsInterface::kIntelligibilityEnhancer[] =
  99. "intelligibilityEnhancer";
  100. const char MediaConstraintsInterface::kLevelControl[] = "levelControl";
  101. const char MediaConstraintsInterface::kLevelControlInitialPeakLevelDBFS[] =
  102. "levelControlInitialPeakLevelDBFS";
  103. const char MediaConstraintsInterface::kHighpassFilter[] =
  104. "googHighpassFilter";
  105. const char MediaConstraintsInterface::kTypingNoiseDetection[] =
  106. "googTypingNoiseDetection";
  107. const char MediaConstraintsInterface::kAudioMirroring[] = "googAudioMirroring";
  108. const char MediaConstraintsInterface::kAudioNetworkAdaptorConfig[] =
  109. "googAudioNetworkAdaptorConfig";
  110. // Google-specific constraint keys for a local video source (getUserMedia).
  111. const char MediaConstraintsInterface::kNoiseReduction[] = "googNoiseReduction";
  112. // Constraint keys for CreateOffer / CreateAnswer defined in W3C specification.
  113. const char MediaConstraintsInterface::kOfferToReceiveAudio[] =
  114. "OfferToReceiveAudio";
  115. const char MediaConstraintsInterface::kOfferToReceiveVideo[] =
  116. "OfferToReceiveVideo";
  117. const char MediaConstraintsInterface::kVoiceActivityDetection[] =
  118. "VoiceActivityDetection";
  119. const char MediaConstraintsInterface::kIceRestart[] =
  120. "IceRestart";
  121. // Google specific constraint for BUNDLE enable/disable.
  122. const char MediaConstraintsInterface::kUseRtpMux[] =
  123. "googUseRtpMUX";
  124. // Below constraints should be used during PeerConnection construction.
  125. const char MediaConstraintsInterface::kEnableDtlsSrtp[] =
  126. "DtlsSrtpKeyAgreement";
  127. const char MediaConstraintsInterface::kEnableRtpDataChannels[] =
  128. "RtpDataChannels";
  129. // Google-specific constraint keys.
  130. const char MediaConstraintsInterface::kEnableDscp[] = "googDscp";
  131. const char MediaConstraintsInterface::kEnableIPv6[] = "googIPv6";
  132. const char MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate[] =
  133. "googSuspendBelowMinBitrate";
  134. const char MediaConstraintsInterface::kCombinedAudioVideoBwe[] =
  135. "googCombinedAudioVideoBwe";
  136. const char MediaConstraintsInterface::kScreencastMinBitrate[] =
  137. "googScreencastMinBitrate";
  138. // TODO(ronghuawu): Remove once cpu overuse detection is stable.
  139. const char MediaConstraintsInterface::kCpuOveruseDetection[] =
  140. "googCpuOveruseDetection";
  141. const char MediaConstraintsInterface::kPayloadPadding[] = "googPayloadPadding";
  142. // Set |value| to the value associated with the first appearance of |key|, or
  143. // return false if |key| is not found.
  144. bool MediaConstraintsInterface::Constraints::FindFirst(
  145. const std::string& key, std::string* value) const {
  146. for (Constraints::const_iterator iter = begin(); iter != end(); ++iter) {
  147. if (iter->key == key) {
  148. *value = iter->value;
  149. return true;
  150. }
  151. }
  152. return false;
  153. }
  154. bool FindConstraint(const MediaConstraintsInterface* constraints,
  155. const std::string& key, bool* value,
  156. size_t* mandatory_constraints) {
  157. return ::FindConstraint<bool>(constraints, key, value, mandatory_constraints);
  158. }
  159. bool FindConstraint(const MediaConstraintsInterface* constraints,
  160. const std::string& key,
  161. int* value,
  162. size_t* mandatory_constraints) {
  163. return ::FindConstraint<int>(constraints, key, value, mandatory_constraints);
  164. }
  165. void CopyConstraintsIntoRtcConfiguration(
  166. const MediaConstraintsInterface* constraints,
  167. PeerConnectionInterface::RTCConfiguration* configuration) {
  168. // Copy info from constraints into configuration, if present.
  169. if (!constraints) {
  170. return;
  171. }
  172. bool enable_ipv6;
  173. if (FindConstraint(constraints, MediaConstraintsInterface::kEnableIPv6,
  174. &enable_ipv6, nullptr)) {
  175. configuration->disable_ipv6 = !enable_ipv6;
  176. }
  177. FindConstraint(constraints, MediaConstraintsInterface::kEnableDscp,
  178. &configuration->media_config.enable_dscp, nullptr);
  179. FindConstraint(
  180. constraints, MediaConstraintsInterface::kCpuOveruseDetection,
  181. &configuration->media_config.video.enable_cpu_overuse_detection, nullptr);
  182. FindConstraint(constraints, MediaConstraintsInterface::kEnableRtpDataChannels,
  183. &configuration->enable_rtp_data_channel, nullptr);
  184. // Find Suspend Below Min Bitrate constraint.
  185. FindConstraint(constraints,
  186. MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
  187. &configuration->media_config.video.suspend_below_min_bitrate,
  188. nullptr);
  189. ConstraintToOptional<int>(constraints,
  190. MediaConstraintsInterface::kScreencastMinBitrate,
  191. &configuration->screencast_min_bitrate);
  192. ConstraintToOptional<bool>(constraints,
  193. MediaConstraintsInterface::kCombinedAudioVideoBwe,
  194. &configuration->combined_audio_video_bwe);
  195. ConstraintToOptional<bool>(constraints,
  196. MediaConstraintsInterface::kEnableDtlsSrtp,
  197. &configuration->enable_dtls_srtp);
  198. }
  199. void CopyConstraintsIntoAudioOptions(
  200. const MediaConstraintsInterface* constraints,
  201. cricket::AudioOptions* options) {
  202. if (!constraints) {
  203. return;
  204. }
  205. ConstraintToOptional<bool>(constraints,
  206. MediaConstraintsInterface::kGoogEchoCancellation,
  207. &options->echo_cancellation);
  208. ConstraintToOptional<bool>(
  209. constraints, MediaConstraintsInterface::kExtendedFilterEchoCancellation,
  210. &options->extended_filter_aec);
  211. ConstraintToOptional<bool>(constraints,
  212. MediaConstraintsInterface::kDAEchoCancellation,
  213. &options->delay_agnostic_aec);
  214. ConstraintToOptional<bool>(constraints,
  215. MediaConstraintsInterface::kAutoGainControl,
  216. &options->auto_gain_control);
  217. ConstraintToOptional<bool>(
  218. constraints, MediaConstraintsInterface::kExperimentalAutoGainControl,
  219. &options->experimental_agc);
  220. ConstraintToOptional<bool>(constraints,
  221. MediaConstraintsInterface::kNoiseSuppression,
  222. &options->noise_suppression);
  223. ConstraintToOptional<bool>(
  224. constraints, MediaConstraintsInterface::kExperimentalNoiseSuppression,
  225. &options->experimental_ns);
  226. ConstraintToOptional<bool>(
  227. constraints, MediaConstraintsInterface::kIntelligibilityEnhancer,
  228. &options->intelligibility_enhancer);
  229. ConstraintToOptional<bool>(constraints,
  230. MediaConstraintsInterface::kLevelControl,
  231. &options->level_control);
  232. ConstraintToOptional<bool>(constraints,
  233. MediaConstraintsInterface::kHighpassFilter,
  234. &options->highpass_filter);
  235. ConstraintToOptional<bool>(constraints,
  236. MediaConstraintsInterface::kTypingNoiseDetection,
  237. &options->typing_detection);
  238. ConstraintToOptional<bool>(constraints,
  239. MediaConstraintsInterface::kAudioMirroring,
  240. &options->stereo_swapping);
  241. ConstraintToOptional<float>(
  242. constraints, MediaConstraintsInterface::kLevelControlInitialPeakLevelDBFS,
  243. &options->level_control_initial_peak_level_dbfs);
  244. ConstraintToOptional<std::string>(
  245. constraints, MediaConstraintsInterface::kAudioNetworkAdaptorConfig,
  246. &options->audio_network_adaptor_config);
  247. // When |kAudioNetworkAdaptorConfig| is defined, it both means that audio
  248. // network adaptor is desired, and provides the config string.
  249. if (options->audio_network_adaptor_config) {
  250. options->audio_network_adaptor = true;
  251. }
  252. }
  253. } // namespace webrtc