You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4039 lines
157 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #include "api/peer_connection_interface.h"
  11. #include <limits.h>
  12. #include <stdint.h>
  13. #include <string.h>
  14. #include <memory>
  15. #include <string>
  16. #include <utility>
  17. #include <vector>
  18. #include "absl/strings/str_replace.h"
  19. #include "absl/types/optional.h"
  20. #include "api/audio/audio_mixer.h"
  21. #include "api/audio_codecs/audio_decoder_factory.h"
  22. #include "api/audio_codecs/audio_encoder_factory.h"
  23. #include "api/audio_codecs/builtin_audio_decoder_factory.h"
  24. #include "api/audio_codecs/builtin_audio_encoder_factory.h"
  25. #include "api/call/call_factory_interface.h"
  26. #include "api/create_peerconnection_factory.h"
  27. #include "api/data_channel_interface.h"
  28. #include "api/jsep.h"
  29. #include "api/jsep_session_description.h"
  30. #include "api/media_stream_interface.h"
  31. #include "api/media_types.h"
  32. #include "api/rtc_error.h"
  33. #include "api/rtc_event_log/rtc_event_log.h"
  34. #include "api/rtc_event_log/rtc_event_log_factory.h"
  35. #include "api/rtc_event_log_output.h"
  36. #include "api/rtc_event_log_output_file.h"
  37. #include "api/rtp_receiver_interface.h"
  38. #include "api/rtp_sender_interface.h"
  39. #include "api/rtp_transceiver_interface.h"
  40. #include "api/scoped_refptr.h"
  41. #include "api/task_queue/default_task_queue_factory.h"
  42. #include "api/video_codecs/builtin_video_decoder_factory.h"
  43. #include "api/video_codecs/builtin_video_encoder_factory.h"
  44. #include "api/video_codecs/video_decoder_factory.h"
  45. #include "api/video_codecs/video_encoder_factory.h"
  46. #include "media/base/codec.h"
  47. #include "media/base/media_config.h"
  48. #include "media/base/media_engine.h"
  49. #include "media/base/stream_params.h"
  50. #include "media/engine/webrtc_media_engine.h"
  51. #include "media/engine/webrtc_media_engine_defaults.h"
  52. #include "media/sctp/sctp_transport_internal.h"
  53. #include "modules/audio_device/include/audio_device.h"
  54. #include "modules/audio_processing/include/audio_processing.h"
  55. #include "p2p/base/fake_port_allocator.h"
  56. #include "p2p/base/p2p_constants.h"
  57. #include "p2p/base/port.h"
  58. #include "p2p/base/port_allocator.h"
  59. #include "p2p/base/transport_description.h"
  60. #include "p2p/base/transport_info.h"
  61. #include "pc/audio_track.h"
  62. #include "pc/media_session.h"
  63. #include "pc/media_stream.h"
  64. #include "pc/peer_connection.h"
  65. #include "pc/peer_connection_factory.h"
  66. #include "pc/rtc_stats_collector.h"
  67. #include "pc/rtp_sender.h"
  68. #include "pc/session_description.h"
  69. #include "pc/stream_collection.h"
  70. #include "pc/test/fake_audio_capture_module.h"
  71. #include "pc/test/fake_rtc_certificate_generator.h"
  72. #include "pc/test/fake_video_track_source.h"
  73. #include "pc/test/mock_peer_connection_observers.h"
  74. #include "pc/test/test_sdp_strings.h"
  75. #include "pc/video_track.h"
  76. #include "rtc_base/checks.h"
  77. #include "rtc_base/copy_on_write_buffer.h"
  78. #include "rtc_base/gunit.h"
  79. #include "rtc_base/ref_counted_object.h"
  80. #include "rtc_base/rtc_certificate_generator.h"
  81. #include "rtc_base/socket_address.h"
  82. #include "rtc_base/thread.h"
  83. #include "rtc_base/time_utils.h"
  84. #include "rtc_base/virtual_socket_server.h"
  85. #include "test/gmock.h"
  86. #include "test/gtest.h"
  87. #include "test/testsupport/file_utils.h"
  88. #ifdef WEBRTC_ANDROID
  89. #include "pc/test/android_test_initializer.h"
  90. #endif
  91. namespace webrtc {
  92. namespace {
  93. static const char kStreamId1[] = "local_stream_1";
  94. static const char kStreamId2[] = "local_stream_2";
  95. static const char kStreamId3[] = "local_stream_3";
  96. static const int kDefaultStunPort = 3478;
  97. static const char kStunAddressOnly[] = "stun:address";
  98. static const char kStunInvalidPort[] = "stun:address:-1";
  99. static const char kStunAddressPortAndMore1[] = "stun:address:port:more";
  100. static const char kStunAddressPortAndMore2[] = "stun:address:port more";
  101. static const char kTurnIceServerUri[] = "turn:turn.example.org";
  102. static const char kTurnUsername[] = "user";
  103. static const char kTurnPassword[] = "password";
  104. static const char kTurnHostname[] = "turn.example.org";
  105. static const uint32_t kTimeout = 10000U;
  106. static const char kStreams[][8] = {"stream1", "stream2"};
  107. static const char kAudioTracks[][32] = {"audiotrack0", "audiotrack1"};
  108. static const char kVideoTracks[][32] = {"videotrack0", "videotrack1"};
  109. static const char kRecvonly[] = "recvonly";
  110. static const char kSendrecv[] = "sendrecv";
  111. // Reference SDP with a MediaStream with label "stream1" and audio track with
  112. // id "audio_1" and a video track with id "video_1;
  113. static const char kSdpStringWithStream1PlanB[] =
  114. "v=0\r\n"
  115. "o=- 0 0 IN IP4 127.0.0.1\r\n"
  116. "s=-\r\n"
  117. "t=0 0\r\n"
  118. "m=audio 1 RTP/AVPF 103\r\n"
  119. "a=ice-ufrag:e5785931\r\n"
  120. "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
  121. "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
  122. "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
  123. "a=mid:audio\r\n"
  124. "a=sendrecv\r\n"
  125. "a=rtcp-mux\r\n"
  126. "a=rtpmap:103 ISAC/16000\r\n"
  127. "a=ssrc:1 cname:stream1\r\n"
  128. "a=ssrc:1 mslabel:stream1\r\n"
  129. "a=ssrc:1 label:audiotrack0\r\n"
  130. "m=video 1 RTP/AVPF 120\r\n"
  131. "a=ice-ufrag:e5785931\r\n"
  132. "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
  133. "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
  134. "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
  135. "a=mid:video\r\n"
  136. "a=sendrecv\r\n"
  137. "a=rtcp-mux\r\n"
  138. "a=rtpmap:120 VP8/90000\r\n"
  139. "a=ssrc:2 cname:stream1\r\n"
  140. "a=ssrc:2 mslabel:stream1\r\n"
  141. "a=ssrc:2 label:videotrack0\r\n";
  142. // Same string as above but with the MID changed to the Unified Plan default.
  143. // This is needed so that this SDP can be used as an answer for a Unified Plan
  144. // offer.
  145. static const char kSdpStringWithStream1UnifiedPlan[] =
  146. "v=0\r\n"
  147. "o=- 0 0 IN IP4 127.0.0.1\r\n"
  148. "s=-\r\n"
  149. "t=0 0\r\n"
  150. "m=audio 1 RTP/AVPF 103\r\n"
  151. "a=ice-ufrag:e5785931\r\n"
  152. "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
  153. "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
  154. "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
  155. "a=mid:0\r\n"
  156. "a=sendrecv\r\n"
  157. "a=rtcp-mux\r\n"
  158. "a=rtpmap:103 ISAC/16000\r\n"
  159. "a=ssrc:1 cname:stream1\r\n"
  160. "a=ssrc:1 mslabel:stream1\r\n"
  161. "a=ssrc:1 label:audiotrack0\r\n"
  162. "m=video 1 RTP/AVPF 120\r\n"
  163. "a=ice-ufrag:e5785931\r\n"
  164. "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
  165. "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
  166. "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
  167. "a=mid:1\r\n"
  168. "a=sendrecv\r\n"
  169. "a=rtcp-mux\r\n"
  170. "a=rtpmap:120 VP8/90000\r\n"
  171. "a=ssrc:2 cname:stream1\r\n"
  172. "a=ssrc:2 mslabel:stream1\r\n"
  173. "a=ssrc:2 label:videotrack0\r\n";
  174. // Reference SDP with a MediaStream with label "stream1" and audio track with
  175. // id "audio_1";
  176. static const char kSdpStringWithStream1AudioTrackOnly[] =
  177. "v=0\r\n"
  178. "o=- 0 0 IN IP4 127.0.0.1\r\n"
  179. "s=-\r\n"
  180. "t=0 0\r\n"
  181. "m=audio 1 RTP/AVPF 103\r\n"
  182. "a=ice-ufrag:e5785931\r\n"
  183. "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
  184. "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
  185. "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
  186. "a=mid:audio\r\n"
  187. "a=sendrecv\r\n"
  188. "a=rtpmap:103 ISAC/16000\r\n"
  189. "a=ssrc:1 cname:stream1\r\n"
  190. "a=ssrc:1 mslabel:stream1\r\n"
  191. "a=ssrc:1 label:audiotrack0\r\n"
  192. "a=rtcp-mux\r\n";
  193. // Reference SDP with two MediaStreams with label "stream1" and "stream2. Each
  194. // MediaStreams have one audio track and one video track.
  195. // This uses MSID.
  196. static const char kSdpStringWithStream1And2PlanB[] =
  197. "v=0\r\n"
  198. "o=- 0 0 IN IP4 127.0.0.1\r\n"
  199. "s=-\r\n"
  200. "t=0 0\r\n"
  201. "a=msid-semantic: WMS stream1 stream2\r\n"
  202. "m=audio 1 RTP/AVPF 103\r\n"
  203. "a=ice-ufrag:e5785931\r\n"
  204. "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
  205. "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
  206. "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
  207. "a=mid:audio\r\n"
  208. "a=sendrecv\r\n"
  209. "a=rtcp-mux\r\n"
  210. "a=rtpmap:103 ISAC/16000\r\n"
  211. "a=ssrc:1 cname:stream1\r\n"
  212. "a=ssrc:1 msid:stream1 audiotrack0\r\n"
  213. "a=ssrc:3 cname:stream2\r\n"
  214. "a=ssrc:3 msid:stream2 audiotrack1\r\n"
  215. "m=video 1 RTP/AVPF 120\r\n"
  216. "a=ice-ufrag:e5785931\r\n"
  217. "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
  218. "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
  219. "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
  220. "a=mid:video\r\n"
  221. "a=sendrecv\r\n"
  222. "a=rtcp-mux\r\n"
  223. "a=rtpmap:120 VP8/0\r\n"
  224. "a=ssrc:2 cname:stream1\r\n"
  225. "a=ssrc:2 msid:stream1 videotrack0\r\n"
  226. "a=ssrc:4 cname:stream2\r\n"
  227. "a=ssrc:4 msid:stream2 videotrack1\r\n";
  228. static const char kSdpStringWithStream1And2UnifiedPlan[] =
  229. "v=0\r\n"
  230. "o=- 0 0 IN IP4 127.0.0.1\r\n"
  231. "s=-\r\n"
  232. "t=0 0\r\n"
  233. "a=msid-semantic: WMS stream1 stream2\r\n"
  234. "m=audio 1 RTP/AVPF 103\r\n"
  235. "a=ice-ufrag:e5785931\r\n"
  236. "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
  237. "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
  238. "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
  239. "a=mid:0\r\n"
  240. "a=sendrecv\r\n"
  241. "a=rtcp-mux\r\n"
  242. "a=rtpmap:103 ISAC/16000\r\n"
  243. "a=ssrc:1 cname:stream1\r\n"
  244. "a=ssrc:1 msid:stream1 audiotrack0\r\n"
  245. "m=video 1 RTP/AVPF 120\r\n"
  246. "a=ice-ufrag:e5785931\r\n"
  247. "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
  248. "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
  249. "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
  250. "a=mid:1\r\n"
  251. "a=sendrecv\r\n"
  252. "a=rtcp-mux\r\n"
  253. "a=rtpmap:120 VP8/0\r\n"
  254. "a=ssrc:2 cname:stream1\r\n"
  255. "a=ssrc:2 msid:stream1 videotrack0\r\n"
  256. "m=audio 1 RTP/AVPF 103\r\n"
  257. "a=ice-ufrag:e5785931\r\n"
  258. "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
  259. "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
  260. "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
  261. "a=mid:2\r\n"
  262. "a=sendrecv\r\n"
  263. "a=rtcp-mux\r\n"
  264. "a=rtpmap:103 ISAC/16000\r\n"
  265. "a=ssrc:3 cname:stream2\r\n"
  266. "a=ssrc:3 msid:stream2 audiotrack1\r\n"
  267. "m=video 1 RTP/AVPF 120\r\n"
  268. "a=ice-ufrag:e5785931\r\n"
  269. "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
  270. "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
  271. "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
  272. "a=mid:3\r\n"
  273. "a=sendrecv\r\n"
  274. "a=rtcp-mux\r\n"
  275. "a=rtpmap:120 VP8/0\r\n"
  276. "a=ssrc:4 cname:stream2\r\n"
  277. "a=ssrc:4 msid:stream2 videotrack1\r\n";
  278. // Reference SDP without MediaStreams. Msid is not supported.
  279. static const char kSdpStringWithoutStreams[] =
  280. "v=0\r\n"
  281. "o=- 0 0 IN IP4 127.0.0.1\r\n"
  282. "s=-\r\n"
  283. "t=0 0\r\n"
  284. "m=audio 1 RTP/AVPF 103\r\n"
  285. "a=ice-ufrag:e5785931\r\n"
  286. "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
  287. "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
  288. "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
  289. "a=mid:audio\r\n"
  290. "a=sendrecv\r\n"
  291. "a=rtcp-mux\r\n"
  292. "a=rtpmap:103 ISAC/16000\r\n"
  293. "m=video 1 RTP/AVPF 120\r\n"
  294. "a=ice-ufrag:e5785931\r\n"
  295. "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
  296. "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
  297. "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
  298. "a=mid:video\r\n"
  299. "a=sendrecv\r\n"
  300. "a=rtcp-mux\r\n"
  301. "a=rtpmap:120 VP8/90000\r\n";
  302. // Reference SDP without MediaStreams. Msid is supported.
  303. static const char kSdpStringWithMsidWithoutStreams[] =
  304. "v=0\r\n"
  305. "o=- 0 0 IN IP4 127.0.0.1\r\n"
  306. "s=-\r\n"
  307. "t=0 0\r\n"
  308. "a=msid-semantic: WMS\r\n"
  309. "m=audio 1 RTP/AVPF 103\r\n"
  310. "a=ice-ufrag:e5785931\r\n"
  311. "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
  312. "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
  313. "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
  314. "a=mid:audio\r\n"
  315. "a=sendrecv\r\n"
  316. "a=rtcp-mux\r\n"
  317. "a=rtpmap:103 ISAC/16000\r\n"
  318. "m=video 1 RTP/AVPF 120\r\n"
  319. "a=ice-ufrag:e5785931\r\n"
  320. "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
  321. "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
  322. "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
  323. "a=mid:video\r\n"
  324. "a=sendrecv\r\n"
  325. "a=rtcp-mux\r\n"
  326. "a=rtpmap:120 VP8/90000\r\n";
  327. // Reference SDP without MediaStreams and audio only.
  328. static const char kSdpStringWithoutStreamsAudioOnly[] =
  329. "v=0\r\n"
  330. "o=- 0 0 IN IP4 127.0.0.1\r\n"
  331. "s=-\r\n"
  332. "t=0 0\r\n"
  333. "m=audio 1 RTP/AVPF 103\r\n"
  334. "a=ice-ufrag:e5785931\r\n"
  335. "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
  336. "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
  337. "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
  338. "a=mid:audio\r\n"
  339. "a=sendrecv\r\n"
  340. "a=rtcp-mux\r\n"
  341. "a=rtpmap:103 ISAC/16000\r\n";
  342. // Reference SENDONLY SDP without MediaStreams. Msid is not supported.
  343. static const char kSdpStringSendOnlyWithoutStreams[] =
  344. "v=0\r\n"
  345. "o=- 0 0 IN IP4 127.0.0.1\r\n"
  346. "s=-\r\n"
  347. "t=0 0\r\n"
  348. "m=audio 1 RTP/AVPF 103\r\n"
  349. "a=ice-ufrag:e5785931\r\n"
  350. "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
  351. "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
  352. "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
  353. "a=mid:audio\r\n"
  354. "a=sendrecv\r\n"
  355. "a=sendonly\r\n"
  356. "a=rtcp-mux\r\n"
  357. "a=rtpmap:103 ISAC/16000\r\n"
  358. "m=video 1 RTP/AVPF 120\r\n"
  359. "a=ice-ufrag:e5785931\r\n"
  360. "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
  361. "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
  362. "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
  363. "a=mid:video\r\n"
  364. "a=sendrecv\r\n"
  365. "a=sendonly\r\n"
  366. "a=rtcp-mux\r\n"
  367. "a=rtpmap:120 VP8/90000\r\n";
  368. static const char kSdpStringInit[] =
  369. "v=0\r\n"
  370. "o=- 0 0 IN IP4 127.0.0.1\r\n"
  371. "s=-\r\n"
  372. "t=0 0\r\n"
  373. "a=msid-semantic: WMS\r\n";
  374. static const char kSdpStringAudio[] =
  375. "m=audio 1 RTP/AVPF 103\r\n"
  376. "a=ice-ufrag:e5785931\r\n"
  377. "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
  378. "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
  379. "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
  380. "a=mid:audio\r\n"
  381. "a=sendrecv\r\n"
  382. "a=rtcp-mux\r\n"
  383. "a=rtpmap:103 ISAC/16000\r\n";
  384. static const char kSdpStringVideo[] =
  385. "m=video 1 RTP/AVPF 120\r\n"
  386. "a=ice-ufrag:e5785931\r\n"
  387. "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
  388. "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
  389. "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
  390. "a=mid:video\r\n"
  391. "a=sendrecv\r\n"
  392. "a=rtcp-mux\r\n"
  393. "a=rtpmap:120 VP8/90000\r\n";
  394. static const char kSdpStringMs1Audio0[] =
  395. "a=ssrc:1 cname:stream1\r\n"
  396. "a=ssrc:1 msid:stream1 audiotrack0\r\n";
  397. static const char kSdpStringMs1Video0[] =
  398. "a=ssrc:2 cname:stream1\r\n"
  399. "a=ssrc:2 msid:stream1 videotrack0\r\n";
  400. static const char kSdpStringMs1Audio1[] =
  401. "a=ssrc:3 cname:stream1\r\n"
  402. "a=ssrc:3 msid:stream1 audiotrack1\r\n";
  403. static const char kSdpStringMs1Video1[] =
  404. "a=ssrc:4 cname:stream1\r\n"
  405. "a=ssrc:4 msid:stream1 videotrack1\r\n";
  406. static const char kDtlsSdesFallbackSdp[] =
  407. "v=0\r\n"
  408. "o=xxxxxx 7 2 IN IP4 0.0.0.0\r\n"
  409. "s=-\r\n"
  410. "c=IN IP4 0.0.0.0\r\n"
  411. "t=0 0\r\n"
  412. "a=group:BUNDLE audio\r\n"
  413. "a=msid-semantic: WMS\r\n"
  414. "m=audio 1 RTP/SAVPF 0\r\n"
  415. "a=sendrecv\r\n"
  416. "a=rtcp-mux\r\n"
  417. "a=mid:audio\r\n"
  418. "a=ssrc:1 cname:stream1\r\n"
  419. "a=ssrc:1 mslabel:stream1\r\n"
  420. "a=ssrc:1 label:audiotrack0\r\n"
  421. "a=ice-ufrag:e5785931\r\n"
  422. "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
  423. "a=rtpmap:0 pcmu/8000\r\n"
  424. "a=fingerprint:sha-1 "
  425. "4A:AD:B9:B1:3F:82:18:3B:54:02:12:DF:3E:5D:49:6B:19:E5:7C:AB\r\n"
  426. "a=setup:actpass\r\n"
  427. "a=crypto:0 AES_CM_128_HMAC_SHA1_80 "
  428. "inline:NzB4d1BINUAvLEw6UzF3WSJ+PSdFcGdUJShpX1Zj|2^20|1:32 "
  429. "dummy_session_params\r\n";
  430. class RtcEventLogOutputNull final : public RtcEventLogOutput {
  431. public:
  432. bool IsActive() const override { return true; }
  433. bool Write(const std::string& output) override { return true; }
  434. };
  435. using ::cricket::StreamParams;
  436. using ::testing::Exactly;
  437. using ::testing::Values;
  438. using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
  439. using RTCOfferAnswerOptions = PeerConnectionInterface::RTCOfferAnswerOptions;
  440. // Gets the first ssrc of given content type from the ContentInfo.
  441. bool GetFirstSsrc(const cricket::ContentInfo* content_info, int* ssrc) {
  442. if (!content_info || !ssrc) {
  443. return false;
  444. }
  445. const cricket::MediaContentDescription* media_desc =
  446. content_info->media_description();
  447. if (!media_desc || media_desc->streams().empty()) {
  448. return false;
  449. }
  450. *ssrc = media_desc->streams().begin()->first_ssrc();
  451. return true;
  452. }
  453. // Get the ufrags out of an SDP blob. Useful for testing ICE restart
  454. // behavior.
  455. std::vector<std::string> GetUfrags(
  456. const webrtc::SessionDescriptionInterface* desc) {
  457. std::vector<std::string> ufrags;
  458. for (const cricket::TransportInfo& info :
  459. desc->description()->transport_infos()) {
  460. ufrags.push_back(info.description.ice_ufrag);
  461. }
  462. return ufrags;
  463. }
  464. void SetSsrcToZero(std::string* sdp) {
  465. const char kSdpSsrcAtribute[] = "a=ssrc:";
  466. const char kSdpSsrcAtributeZero[] = "a=ssrc:0";
  467. size_t ssrc_pos = 0;
  468. while ((ssrc_pos = sdp->find(kSdpSsrcAtribute, ssrc_pos)) !=
  469. std::string::npos) {
  470. size_t end_ssrc = sdp->find(" ", ssrc_pos);
  471. sdp->replace(ssrc_pos, end_ssrc - ssrc_pos, kSdpSsrcAtributeZero);
  472. ssrc_pos = end_ssrc;
  473. }
  474. }
  475. // Check if |streams| contains the specified track.
  476. bool ContainsTrack(const std::vector<cricket::StreamParams>& streams,
  477. const std::string& stream_id,
  478. const std::string& track_id) {
  479. for (const cricket::StreamParams& params : streams) {
  480. if (params.first_stream_id() == stream_id && params.id == track_id) {
  481. return true;
  482. }
  483. }
  484. return false;
  485. }
  486. // Check if |senders| contains the specified sender, by id.
  487. bool ContainsSender(
  488. const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
  489. const std::string& id) {
  490. for (const auto& sender : senders) {
  491. if (sender->id() == id) {
  492. return true;
  493. }
  494. }
  495. return false;
  496. }
  497. // Check if |senders| contains the specified sender, by id and stream id.
  498. bool ContainsSender(
  499. const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
  500. const std::string& id,
  501. const std::string& stream_id) {
  502. for (const auto& sender : senders) {
  503. if (sender->id() == id && sender->stream_ids()[0] == stream_id) {
  504. return true;
  505. }
  506. }
  507. return false;
  508. }
  509. // Create a collection of streams.
  510. // CreateStreamCollection(1) creates a collection that
  511. // correspond to kSdpStringWithStream1.
  512. // CreateStreamCollection(2) correspond to kSdpStringWithStream1And2.
  513. rtc::scoped_refptr<StreamCollection> CreateStreamCollection(
  514. int number_of_streams,
  515. int tracks_per_stream) {
  516. rtc::scoped_refptr<StreamCollection> local_collection(
  517. StreamCollection::Create());
  518. for (int i = 0; i < number_of_streams; ++i) {
  519. rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
  520. webrtc::MediaStream::Create(kStreams[i]));
  521. for (int j = 0; j < tracks_per_stream; ++j) {
  522. // Add a local audio track.
  523. rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
  524. webrtc::AudioTrack::Create(kAudioTracks[i * tracks_per_stream + j],
  525. nullptr));
  526. stream->AddTrack(audio_track);
  527. // Add a local video track.
  528. rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
  529. webrtc::VideoTrack::Create(kVideoTracks[i * tracks_per_stream + j],
  530. webrtc::FakeVideoTrackSource::Create(),
  531. rtc::Thread::Current()));
  532. stream->AddTrack(video_track);
  533. }
  534. local_collection->AddStream(stream);
  535. }
  536. return local_collection;
  537. }
  538. // Check equality of StreamCollections.
  539. bool CompareStreamCollections(StreamCollectionInterface* s1,
  540. StreamCollectionInterface* s2) {
  541. if (s1 == nullptr || s2 == nullptr || s1->count() != s2->count()) {
  542. return false;
  543. }
  544. for (size_t i = 0; i != s1->count(); ++i) {
  545. if (s1->at(i)->id() != s2->at(i)->id()) {
  546. return false;
  547. }
  548. webrtc::AudioTrackVector audio_tracks1 = s1->at(i)->GetAudioTracks();
  549. webrtc::AudioTrackVector audio_tracks2 = s2->at(i)->GetAudioTracks();
  550. webrtc::VideoTrackVector video_tracks1 = s1->at(i)->GetVideoTracks();
  551. webrtc::VideoTrackVector video_tracks2 = s2->at(i)->GetVideoTracks();
  552. if (audio_tracks1.size() != audio_tracks2.size()) {
  553. return false;
  554. }
  555. for (size_t j = 0; j != audio_tracks1.size(); ++j) {
  556. if (audio_tracks1[j]->id() != audio_tracks2[j]->id()) {
  557. return false;
  558. }
  559. }
  560. if (video_tracks1.size() != video_tracks2.size()) {
  561. return false;
  562. }
  563. for (size_t j = 0; j != video_tracks1.size(); ++j) {
  564. if (video_tracks1[j]->id() != video_tracks2[j]->id()) {
  565. return false;
  566. }
  567. }
  568. }
  569. return true;
  570. }
  571. // Helper class to test Observer.
  572. class MockTrackObserver : public ObserverInterface {
  573. public:
  574. explicit MockTrackObserver(NotifierInterface* notifier)
  575. : notifier_(notifier) {
  576. notifier_->RegisterObserver(this);
  577. }
  578. ~MockTrackObserver() { Unregister(); }
  579. void Unregister() {
  580. if (notifier_) {
  581. notifier_->UnregisterObserver(this);
  582. notifier_ = nullptr;
  583. }
  584. }
  585. MOCK_METHOD0(OnChanged, void());
  586. private:
  587. NotifierInterface* notifier_;
  588. };
  589. // The PeerConnectionMediaConfig tests below verify that configuration and
  590. // constraints are propagated into the PeerConnection's MediaConfig. These
  591. // settings are intended for MediaChannel constructors, but that is not
  592. // exercised by these unittest.
  593. class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
  594. public:
  595. static rtc::scoped_refptr<PeerConnectionFactoryForTest>
  596. CreatePeerConnectionFactoryForTest() {
  597. PeerConnectionFactoryDependencies dependencies;
  598. dependencies.worker_thread = rtc::Thread::Current();
  599. dependencies.network_thread = rtc::Thread::Current();
  600. dependencies.signaling_thread = rtc::Thread::Current();
  601. dependencies.task_queue_factory = CreateDefaultTaskQueueFactory();
  602. cricket::MediaEngineDependencies media_deps;
  603. media_deps.task_queue_factory = dependencies.task_queue_factory.get();
  604. // Use fake audio device module since we're only testing the interface
  605. // level, and using a real one could make tests flaky when run in parallel.
  606. media_deps.adm = FakeAudioCaptureModule::Create();
  607. SetMediaEngineDefaults(&media_deps);
  608. dependencies.media_engine =
  609. cricket::CreateMediaEngine(std::move(media_deps));
  610. dependencies.call_factory = webrtc::CreateCallFactory();
  611. dependencies.event_log_factory = std::make_unique<RtcEventLogFactory>(
  612. dependencies.task_queue_factory.get());
  613. return new rtc::RefCountedObject<PeerConnectionFactoryForTest>(
  614. std::move(dependencies));
  615. }
  616. using PeerConnectionFactory::PeerConnectionFactory;
  617. private:
  618. rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
  619. };
  620. // TODO(steveanton): Convert to use the new PeerConnectionWrapper.
  621. class PeerConnectionInterfaceBaseTest : public ::testing::Test {
  622. protected:
  623. explicit PeerConnectionInterfaceBaseTest(SdpSemantics sdp_semantics)
  624. : vss_(new rtc::VirtualSocketServer()),
  625. main_(vss_.get()),
  626. sdp_semantics_(sdp_semantics) {
  627. #ifdef WEBRTC_ANDROID
  628. webrtc::InitializeAndroidObjects();
  629. #endif
  630. }
  631. virtual void SetUp() {
  632. // Use fake audio capture module since we're only testing the interface
  633. // level, and using a real one could make tests flaky when run in parallel.
  634. fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
  635. pc_factory_ = webrtc::CreatePeerConnectionFactory(
  636. rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
  637. rtc::scoped_refptr<webrtc::AudioDeviceModule>(
  638. fake_audio_capture_module_),
  639. webrtc::CreateBuiltinAudioEncoderFactory(),
  640. webrtc::CreateBuiltinAudioDecoderFactory(),
  641. webrtc::CreateBuiltinVideoEncoderFactory(),
  642. webrtc::CreateBuiltinVideoDecoderFactory(), nullptr /* audio_mixer */,
  643. nullptr /* audio_processing */);
  644. ASSERT_TRUE(pc_factory_);
  645. pc_factory_for_test_ =
  646. PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
  647. pc_factory_for_test_->Initialize();
  648. }
  649. void CreatePeerConnection() {
  650. CreatePeerConnection(PeerConnectionInterface::RTCConfiguration());
  651. }
  652. // DTLS does not work in a loopback call, so is disabled for most of the
  653. // tests in this file.
  654. void CreatePeerConnectionWithoutDtls() {
  655. RTCConfiguration config;
  656. config.enable_dtls_srtp = false;
  657. CreatePeerConnection(config);
  658. }
  659. void CreatePeerConnectionWithIceTransportsType(
  660. PeerConnectionInterface::IceTransportsType type) {
  661. PeerConnectionInterface::RTCConfiguration config;
  662. config.type = type;
  663. return CreatePeerConnection(config);
  664. }
  665. void CreatePeerConnectionWithIceServer(const std::string& uri,
  666. const std::string& username,
  667. const std::string& password) {
  668. PeerConnectionInterface::RTCConfiguration config;
  669. PeerConnectionInterface::IceServer server;
  670. server.uri = uri;
  671. server.username = username;
  672. server.password = password;
  673. config.servers.push_back(server);
  674. CreatePeerConnection(config);
  675. }
  676. void CreatePeerConnection(const RTCConfiguration& config) {
  677. std::unique_ptr<cricket::FakePortAllocator> port_allocator(
  678. new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
  679. port_allocator_ = port_allocator.get();
  680. // Create certificate generator unless DTLS constraint is explicitly set to
  681. // false.
  682. std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator;
  683. if (config.enable_dtls_srtp.value_or(true)) {
  684. fake_certificate_generator_ = new FakeRTCCertificateGenerator();
  685. cert_generator.reset(fake_certificate_generator_);
  686. }
  687. RTCConfiguration modified_config = config;
  688. modified_config.sdp_semantics = sdp_semantics_;
  689. pc_ = pc_factory_->CreatePeerConnection(
  690. modified_config, std::move(port_allocator), std::move(cert_generator),
  691. &observer_);
  692. ASSERT_TRUE(pc_.get() != NULL);
  693. observer_.SetPeerConnectionInterface(pc_.get());
  694. EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
  695. }
  696. void CreatePeerConnectionExpectFail(const std::string& uri) {
  697. PeerConnectionInterface::RTCConfiguration config;
  698. PeerConnectionInterface::IceServer server;
  699. server.uri = uri;
  700. config.servers.push_back(server);
  701. config.sdp_semantics = sdp_semantics_;
  702. rtc::scoped_refptr<PeerConnectionInterface> pc =
  703. pc_factory_->CreatePeerConnection(config, nullptr, nullptr, &observer_);
  704. EXPECT_EQ(nullptr, pc);
  705. }
  706. void CreatePeerConnectionExpectFail(
  707. PeerConnectionInterface::RTCConfiguration config) {
  708. PeerConnectionInterface::IceServer server;
  709. server.uri = kTurnIceServerUri;
  710. server.password = kTurnPassword;
  711. config.servers.push_back(server);
  712. config.sdp_semantics = sdp_semantics_;
  713. rtc::scoped_refptr<PeerConnectionInterface> pc =
  714. pc_factory_->CreatePeerConnection(config, nullptr, nullptr, &observer_);
  715. EXPECT_EQ(nullptr, pc);
  716. }
  717. void CreatePeerConnectionWithDifferentConfigurations() {
  718. CreatePeerConnectionWithIceServer(kStunAddressOnly, "", "");
  719. EXPECT_EQ(1u, port_allocator_->stun_servers().size());
  720. EXPECT_EQ(0u, port_allocator_->turn_servers().size());
  721. EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
  722. EXPECT_EQ(kDefaultStunPort,
  723. port_allocator_->stun_servers().begin()->port());
  724. CreatePeerConnectionExpectFail(kStunInvalidPort);
  725. CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
  726. CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
  727. CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnUsername,
  728. kTurnPassword);
  729. EXPECT_EQ(0u, port_allocator_->stun_servers().size());
  730. EXPECT_EQ(1u, port_allocator_->turn_servers().size());
  731. EXPECT_EQ(kTurnUsername,
  732. port_allocator_->turn_servers()[0].credentials.username);
  733. EXPECT_EQ(kTurnPassword,
  734. port_allocator_->turn_servers()[0].credentials.password);
  735. EXPECT_EQ(kTurnHostname,
  736. port_allocator_->turn_servers()[0].ports[0].address.hostname());
  737. }
  738. void ReleasePeerConnection() {
  739. pc_ = NULL;
  740. observer_.SetPeerConnectionInterface(NULL);
  741. }
  742. rtc::scoped_refptr<VideoTrackInterface> CreateVideoTrack(
  743. const std::string& label) {
  744. return pc_factory_->CreateVideoTrack(label, FakeVideoTrackSource::Create());
  745. }
  746. void AddVideoTrack(const std::string& track_label,
  747. const std::vector<std::string>& stream_ids = {}) {
  748. auto sender_or_error =
  749. pc_->AddTrack(CreateVideoTrack(track_label), stream_ids);
  750. ASSERT_EQ(RTCErrorType::NONE, sender_or_error.error().type());
  751. }
  752. void AddVideoStream(const std::string& label) {
  753. rtc::scoped_refptr<MediaStreamInterface> stream(
  754. pc_factory_->CreateLocalMediaStream(label));
  755. stream->AddTrack(CreateVideoTrack(label + "v0"));
  756. ASSERT_TRUE(pc_->AddStream(stream));
  757. }
  758. rtc::scoped_refptr<AudioTrackInterface> CreateAudioTrack(
  759. const std::string& label) {
  760. return pc_factory_->CreateAudioTrack(label, nullptr);
  761. }
  762. void AddAudioTrack(const std::string& track_label,
  763. const std::vector<std::string>& stream_ids = {}) {
  764. auto sender_or_error =
  765. pc_->AddTrack(CreateAudioTrack(track_label), stream_ids);
  766. ASSERT_EQ(RTCErrorType::NONE, sender_or_error.error().type());
  767. }
  768. void AddAudioStream(const std::string& label) {
  769. rtc::scoped_refptr<MediaStreamInterface> stream(
  770. pc_factory_->CreateLocalMediaStream(label));
  771. stream->AddTrack(CreateAudioTrack(label + "a0"));
  772. ASSERT_TRUE(pc_->AddStream(stream));
  773. }
  774. void AddAudioVideoStream(const std::string& stream_id,
  775. const std::string& audio_track_label,
  776. const std::string& video_track_label) {
  777. // Create a local stream.
  778. rtc::scoped_refptr<MediaStreamInterface> stream(
  779. pc_factory_->CreateLocalMediaStream(stream_id));
  780. stream->AddTrack(CreateAudioTrack(audio_track_label));
  781. stream->AddTrack(CreateVideoTrack(video_track_label));
  782. ASSERT_TRUE(pc_->AddStream(stream));
  783. }
  784. rtc::scoped_refptr<RtpReceiverInterface> GetFirstReceiverOfType(
  785. cricket::MediaType media_type) {
  786. for (auto receiver : pc_->GetReceivers()) {
  787. if (receiver->media_type() == media_type) {
  788. return receiver;
  789. }
  790. }
  791. return nullptr;
  792. }
  793. bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
  794. const RTCOfferAnswerOptions* options,
  795. bool offer) {
  796. rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
  797. new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
  798. if (offer) {
  799. pc_->CreateOffer(observer, options ? *options : RTCOfferAnswerOptions());
  800. } else {
  801. pc_->CreateAnswer(observer, options ? *options : RTCOfferAnswerOptions());
  802. }
  803. EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
  804. *desc = observer->MoveDescription();
  805. return observer->result();
  806. }
  807. bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
  808. const RTCOfferAnswerOptions* options) {
  809. return DoCreateOfferAnswer(desc, options, true);
  810. }
  811. bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
  812. const RTCOfferAnswerOptions* options) {
  813. return DoCreateOfferAnswer(desc, options, false);
  814. }
  815. bool DoSetSessionDescription(
  816. std::unique_ptr<SessionDescriptionInterface> desc,
  817. bool local) {
  818. rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
  819. new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
  820. if (local) {
  821. pc_->SetLocalDescription(observer, desc.release());
  822. } else {
  823. pc_->SetRemoteDescription(observer, desc.release());
  824. }
  825. if (pc_->signaling_state() != PeerConnectionInterface::kClosed) {
  826. EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
  827. }
  828. return observer->result();
  829. }
  830. bool DoSetLocalDescription(
  831. std::unique_ptr<SessionDescriptionInterface> desc) {
  832. return DoSetSessionDescription(std::move(desc), true);
  833. }
  834. bool DoSetRemoteDescription(
  835. std::unique_ptr<SessionDescriptionInterface> desc) {
  836. return DoSetSessionDescription(std::move(desc), false);
  837. }
  838. // Calls PeerConnection::GetStats and check the return value.
  839. // It does not verify the values in the StatReports since a RTCP packet might
  840. // be required.
  841. bool DoGetStats(MediaStreamTrackInterface* track) {
  842. rtc::scoped_refptr<MockStatsObserver> observer(
  843. new rtc::RefCountedObject<MockStatsObserver>());
  844. if (!pc_->GetStats(observer, track,
  845. PeerConnectionInterface::kStatsOutputLevelStandard))
  846. return false;
  847. EXPECT_TRUE_WAIT(observer->called(), kTimeout);
  848. return observer->called();
  849. }
  850. // Call the standards-compliant GetStats function.
  851. bool DoGetRTCStats() {
  852. rtc::scoped_refptr<webrtc::MockRTCStatsCollectorCallback> callback(
  853. new rtc::RefCountedObject<webrtc::MockRTCStatsCollectorCallback>());
  854. pc_->GetStats(callback);
  855. EXPECT_TRUE_WAIT(callback->called(), kTimeout);
  856. return callback->called();
  857. }
  858. void InitiateCall() {
  859. CreatePeerConnectionWithoutDtls();
  860. // Create a local stream with audio&video tracks.
  861. if (sdp_semantics_ == SdpSemantics::kPlanB) {
  862. AddAudioVideoStream(kStreamId1, "audio_track", "video_track");
  863. } else {
  864. // Unified Plan does not support AddStream, so just add an audio and video
  865. // track.
  866. AddAudioTrack(kAudioTracks[0], {kStreamId1});
  867. AddVideoTrack(kVideoTracks[0], {kStreamId1});
  868. }
  869. CreateOfferReceiveAnswer();
  870. }
  871. // Verify that RTP Header extensions has been negotiated for audio and video.
  872. void VerifyRemoteRtpHeaderExtensions() {
  873. const cricket::MediaContentDescription* desc =
  874. cricket::GetFirstAudioContentDescription(
  875. pc_->remote_description()->description());
  876. ASSERT_TRUE(desc != NULL);
  877. EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
  878. desc = cricket::GetFirstVideoContentDescription(
  879. pc_->remote_description()->description());
  880. ASSERT_TRUE(desc != NULL);
  881. EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
  882. }
  883. void CreateOfferAsRemoteDescription() {
  884. std::unique_ptr<SessionDescriptionInterface> offer;
  885. ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
  886. std::string sdp;
  887. EXPECT_TRUE(offer->ToString(&sdp));
  888. std::unique_ptr<SessionDescriptionInterface> remote_offer(
  889. webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
  890. EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
  891. EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
  892. }
  893. void CreateAndSetRemoteOffer(const std::string& sdp) {
  894. std::unique_ptr<SessionDescriptionInterface> remote_offer(
  895. webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
  896. EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
  897. EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
  898. }
  899. void CreateAnswerAsLocalDescription() {
  900. std::unique_ptr<SessionDescriptionInterface> answer;
  901. ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
  902. // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
  903. // audio codec change, even if the parameter has nothing to do with
  904. // receiving. Not all parameters are serialized to SDP.
  905. // Since CreatePrAnswerAsLocalDescription serialize/deserialize
  906. // the SessionDescription, it is necessary to do that here to in order to
  907. // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
  908. // https://code.google.com/p/webrtc/issues/detail?id=1356
  909. std::string sdp;
  910. EXPECT_TRUE(answer->ToString(&sdp));
  911. std::unique_ptr<SessionDescriptionInterface> new_answer(
  912. webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
  913. EXPECT_TRUE(DoSetLocalDescription(std::move(new_answer)));
  914. EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
  915. }
  916. void CreatePrAnswerAsLocalDescription() {
  917. std::unique_ptr<SessionDescriptionInterface> answer;
  918. ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
  919. std::string sdp;
  920. EXPECT_TRUE(answer->ToString(&sdp));
  921. std::unique_ptr<SessionDescriptionInterface> pr_answer(
  922. webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
  923. EXPECT_TRUE(DoSetLocalDescription(std::move(pr_answer)));
  924. EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
  925. }
  926. void CreateOfferReceiveAnswer() {
  927. CreateOfferAsLocalDescription();
  928. std::string sdp;
  929. EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
  930. CreateAnswerAsRemoteDescription(sdp);
  931. }
  932. void CreateOfferAsLocalDescription() {
  933. std::unique_ptr<SessionDescriptionInterface> offer;
  934. ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
  935. // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
  936. // audio codec change, even if the parameter has nothing to do with
  937. // receiving. Not all parameters are serialized to SDP.
  938. // Since CreatePrAnswerAsLocalDescription serialize/deserialize
  939. // the SessionDescription, it is necessary to do that here to in order to
  940. // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
  941. // https://code.google.com/p/webrtc/issues/detail?id=1356
  942. std::string sdp;
  943. EXPECT_TRUE(offer->ToString(&sdp));
  944. std::unique_ptr<SessionDescriptionInterface> new_offer(
  945. webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
  946. EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
  947. EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
  948. // Wait for the ice_complete message, so that SDP will have candidates.
  949. EXPECT_TRUE_WAIT(observer_.ice_gathering_complete_, kTimeout);
  950. }
  951. void CreateAnswerAsRemoteDescription(const std::string& sdp) {
  952. std::unique_ptr<SessionDescriptionInterface> answer(
  953. webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
  954. ASSERT_TRUE(answer);
  955. EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
  956. EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
  957. }
  958. void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
  959. std::unique_ptr<SessionDescriptionInterface> pr_answer(
  960. webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
  961. ASSERT_TRUE(pr_answer);
  962. EXPECT_TRUE(DoSetRemoteDescription(std::move(pr_answer)));
  963. EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
  964. std::unique_ptr<SessionDescriptionInterface> answer(
  965. webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
  966. ASSERT_TRUE(answer);
  967. EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
  968. EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
  969. }
  970. // Waits until a remote stream with the given id is signaled. This helper
  971. // function will verify both OnAddTrack and OnAddStream (Plan B only) are
  972. // called with the given stream id and expected number of tracks.
  973. void WaitAndVerifyOnAddStream(const std::string& stream_id,
  974. int expected_num_tracks) {
  975. // Verify that both OnAddStream and OnAddTrack are called.
  976. EXPECT_EQ_WAIT(stream_id, observer_.GetLastAddedStreamId(), kTimeout);
  977. EXPECT_EQ_WAIT(expected_num_tracks,
  978. observer_.CountAddTrackEventsForStream(stream_id), kTimeout);
  979. }
  980. // Creates an offer and applies it as a local session description.
  981. // Creates an answer with the same SDP an the offer but removes all lines
  982. // that start with a:ssrc"
  983. void CreateOfferReceiveAnswerWithoutSsrc() {
  984. CreateOfferAsLocalDescription();
  985. std::string sdp;
  986. EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
  987. SetSsrcToZero(&sdp);
  988. CreateAnswerAsRemoteDescription(sdp);
  989. }
  990. // This function creates a MediaStream with label kStreams[0] and
  991. // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
  992. // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
  993. // is returned and the MediaStream is stored in
  994. // |reference_collection_|
  995. std::unique_ptr<SessionDescriptionInterface>
  996. CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
  997. size_t number_of_video_tracks) {
  998. EXPECT_LE(number_of_audio_tracks, 2u);
  999. EXPECT_LE(number_of_video_tracks, 2u);
  1000. reference_collection_ = StreamCollection::Create();
  1001. std::string sdp_ms1 = std::string(kSdpStringInit);
  1002. std::string mediastream_id = kStreams[0];
  1003. rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
  1004. webrtc::MediaStream::Create(mediastream_id));
  1005. reference_collection_->AddStream(stream);
  1006. if (number_of_audio_tracks > 0) {
  1007. sdp_ms1 += std::string(kSdpStringAudio);
  1008. sdp_ms1 += std::string(kSdpStringMs1Audio0);
  1009. AddAudioTrack(kAudioTracks[0], stream);
  1010. }
  1011. if (number_of_audio_tracks > 1) {
  1012. sdp_ms1 += kSdpStringMs1Audio1;
  1013. AddAudioTrack(kAudioTracks[1], stream);
  1014. }
  1015. if (number_of_video_tracks > 0) {
  1016. sdp_ms1 += std::string(kSdpStringVideo);
  1017. sdp_ms1 += std::string(kSdpStringMs1Video0);
  1018. AddVideoTrack(kVideoTracks[0], stream);
  1019. }
  1020. if (number_of_video_tracks > 1) {
  1021. sdp_ms1 += kSdpStringMs1Video1;
  1022. AddVideoTrack(kVideoTracks[1], stream);
  1023. }
  1024. return std::unique_ptr<SessionDescriptionInterface>(
  1025. webrtc::CreateSessionDescription(SdpType::kOffer, sdp_ms1));
  1026. }
  1027. void AddAudioTrack(const std::string& track_id,
  1028. MediaStreamInterface* stream) {
  1029. rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
  1030. webrtc::AudioTrack::Create(track_id, nullptr));
  1031. ASSERT_TRUE(stream->AddTrack(audio_track));
  1032. }
  1033. void AddVideoTrack(const std::string& track_id,
  1034. MediaStreamInterface* stream) {
  1035. rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
  1036. webrtc::VideoTrack::Create(track_id,
  1037. webrtc::FakeVideoTrackSource::Create(),
  1038. rtc::Thread::Current()));
  1039. ASSERT_TRUE(stream->AddTrack(video_track));
  1040. }
  1041. std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioTrack() {
  1042. CreatePeerConnectionWithoutDtls();
  1043. AddAudioTrack(kAudioTracks[0]);
  1044. std::unique_ptr<SessionDescriptionInterface> offer;
  1045. EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
  1046. return offer;
  1047. }
  1048. std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
  1049. CreatePeerConnectionWithoutDtls();
  1050. AddAudioStream(kStreamId1);
  1051. std::unique_ptr<SessionDescriptionInterface> offer;
  1052. EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
  1053. return offer;
  1054. }
  1055. std::unique_ptr<SessionDescriptionInterface> CreateAnswerWithOneAudioTrack() {
  1056. EXPECT_TRUE(DoSetRemoteDescription(CreateOfferWithOneAudioTrack()));
  1057. std::unique_ptr<SessionDescriptionInterface> answer;
  1058. EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
  1059. return answer;
  1060. }
  1061. std::unique_ptr<SessionDescriptionInterface>
  1062. CreateAnswerWithOneAudioStream() {
  1063. EXPECT_TRUE(DoSetRemoteDescription(CreateOfferWithOneAudioStream()));
  1064. std::unique_ptr<SessionDescriptionInterface> answer;
  1065. EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
  1066. return answer;
  1067. }
  1068. const std::string& GetFirstAudioStreamCname(
  1069. const SessionDescriptionInterface* desc) {
  1070. const cricket::AudioContentDescription* audio_desc =
  1071. cricket::GetFirstAudioContentDescription(desc->description());
  1072. return audio_desc->streams()[0].cname;
  1073. }
  1074. std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOptions(
  1075. const RTCOfferAnswerOptions& offer_answer_options) {
  1076. RTC_DCHECK(pc_);
  1077. rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
  1078. new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
  1079. pc_->CreateOffer(observer, offer_answer_options);
  1080. EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
  1081. return observer->MoveDescription();
  1082. }
  1083. void CreateOfferWithOptionsAsRemoteDescription(
  1084. std::unique_ptr<SessionDescriptionInterface>* desc,
  1085. const RTCOfferAnswerOptions& offer_answer_options) {
  1086. *desc = CreateOfferWithOptions(offer_answer_options);
  1087. ASSERT_TRUE(desc != nullptr);
  1088. std::string sdp;
  1089. EXPECT_TRUE((*desc)->ToString(&sdp));
  1090. std::unique_ptr<SessionDescriptionInterface> remote_offer(
  1091. webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
  1092. EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
  1093. EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
  1094. }
  1095. void CreateOfferWithOptionsAsLocalDescription(
  1096. std::unique_ptr<SessionDescriptionInterface>* desc,
  1097. const RTCOfferAnswerOptions& offer_answer_options) {
  1098. *desc = CreateOfferWithOptions(offer_answer_options);
  1099. ASSERT_TRUE(desc != nullptr);
  1100. std::string sdp;
  1101. EXPECT_TRUE((*desc)->ToString(&sdp));
  1102. std::unique_ptr<SessionDescriptionInterface> new_offer(
  1103. webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
  1104. EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
  1105. EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
  1106. }
  1107. bool HasCNCodecs(const cricket::ContentInfo* content) {
  1108. RTC_DCHECK(content);
  1109. RTC_DCHECK(content->media_description());
  1110. for (const cricket::AudioCodec& codec :
  1111. content->media_description()->as_audio()->codecs()) {
  1112. if (codec.name == "CN") {
  1113. return true;
  1114. }
  1115. }
  1116. return false;
  1117. }
  1118. const char* GetSdpStringWithStream1() const {
  1119. if (sdp_semantics_ == SdpSemantics::kPlanB) {
  1120. return kSdpStringWithStream1PlanB;
  1121. } else {
  1122. return kSdpStringWithStream1UnifiedPlan;
  1123. }
  1124. }
  1125. const char* GetSdpStringWithStream1And2() const {
  1126. if (sdp_semantics_ == SdpSemantics::kPlanB) {
  1127. return kSdpStringWithStream1And2PlanB;
  1128. } else {
  1129. return kSdpStringWithStream1And2UnifiedPlan;
  1130. }
  1131. }
  1132. std::unique_ptr<rtc::VirtualSocketServer> vss_;
  1133. rtc::AutoSocketServerThread main_;
  1134. rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
  1135. cricket::FakePortAllocator* port_allocator_ = nullptr;
  1136. FakeRTCCertificateGenerator* fake_certificate_generator_ = nullptr;
  1137. rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
  1138. rtc::scoped_refptr<PeerConnectionFactoryForTest> pc_factory_for_test_;
  1139. rtc::scoped_refptr<PeerConnectionInterface> pc_;
  1140. MockPeerConnectionObserver observer_;
  1141. rtc::scoped_refptr<StreamCollection> reference_collection_;
  1142. const SdpSemantics sdp_semantics_;
  1143. };
  1144. class PeerConnectionInterfaceTest
  1145. : public PeerConnectionInterfaceBaseTest,
  1146. public ::testing::WithParamInterface<SdpSemantics> {
  1147. protected:
  1148. PeerConnectionInterfaceTest() : PeerConnectionInterfaceBaseTest(GetParam()) {}
  1149. };
  1150. class PeerConnectionInterfaceTestPlanB
  1151. : public PeerConnectionInterfaceBaseTest {
  1152. protected:
  1153. PeerConnectionInterfaceTestPlanB()
  1154. : PeerConnectionInterfaceBaseTest(SdpSemantics::kPlanB) {}
  1155. };
  1156. // Generate different CNAMEs when PeerConnections are created.
  1157. // The CNAMEs are expected to be generated randomly. It is possible
  1158. // that the test fails, though the possibility is very low.
  1159. TEST_P(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
  1160. std::unique_ptr<SessionDescriptionInterface> offer1 =
  1161. CreateOfferWithOneAudioTrack();
  1162. std::unique_ptr<SessionDescriptionInterface> offer2 =
  1163. CreateOfferWithOneAudioTrack();
  1164. EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
  1165. GetFirstAudioStreamCname(offer2.get()));
  1166. }
  1167. TEST_P(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
  1168. std::unique_ptr<SessionDescriptionInterface> answer1 =
  1169. CreateAnswerWithOneAudioTrack();
  1170. std::unique_ptr<SessionDescriptionInterface> answer2 =
  1171. CreateAnswerWithOneAudioTrack();
  1172. EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
  1173. GetFirstAudioStreamCname(answer2.get()));
  1174. }
  1175. TEST_P(PeerConnectionInterfaceTest,
  1176. CreatePeerConnectionWithDifferentConfigurations) {
  1177. CreatePeerConnectionWithDifferentConfigurations();
  1178. }
  1179. TEST_P(PeerConnectionInterfaceTest,
  1180. CreatePeerConnectionWithDifferentIceTransportsTypes) {
  1181. CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
  1182. EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
  1183. CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
  1184. EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
  1185. CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
  1186. EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
  1187. port_allocator_->candidate_filter());
  1188. CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
  1189. EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
  1190. }
  1191. // Test that when a PeerConnection is created with a nonzero candidate pool
  1192. // size, the pooled PortAllocatorSession is created with all the attributes
  1193. // in the RTCConfiguration.
  1194. TEST_P(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
  1195. PeerConnectionInterface::RTCConfiguration config;
  1196. PeerConnectionInterface::IceServer server;
  1197. server.uri = kStunAddressOnly;
  1198. config.servers.push_back(server);
  1199. config.type = PeerConnectionInterface::kRelay;
  1200. config.disable_ipv6 = true;
  1201. config.tcp_candidate_policy =
  1202. PeerConnectionInterface::kTcpCandidatePolicyDisabled;
  1203. config.candidate_network_policy =
  1204. PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
  1205. config.ice_candidate_pool_size = 1;
  1206. CreatePeerConnection(config);
  1207. const cricket::FakePortAllocatorSession* session =
  1208. static_cast<const cricket::FakePortAllocatorSession*>(
  1209. port_allocator_->GetPooledSession());
  1210. ASSERT_NE(nullptr, session);
  1211. EXPECT_EQ(1UL, session->stun_servers().size());
  1212. EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
  1213. EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
  1214. EXPECT_LT(0U,
  1215. session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
  1216. }
  1217. // Test that network-related RTCConfiguration members are applied to the
  1218. // PortAllocator when CreatePeerConnection is called. Specifically:
  1219. // - disable_ipv6_on_wifi
  1220. // - max_ipv6_networks
  1221. // - tcp_candidate_policy
  1222. // - candidate_network_policy
  1223. // - prune_turn_ports
  1224. //
  1225. // Note that the candidate filter (RTCConfiguration::type) is already tested
  1226. // above.
  1227. TEST_P(PeerConnectionInterfaceTest,
  1228. CreatePeerConnectionAppliesNetworkConfigToPortAllocator) {
  1229. // Create fake port allocator.
  1230. std::unique_ptr<cricket::FakePortAllocator> port_allocator(
  1231. new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
  1232. cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
  1233. // Create RTCConfiguration with some network-related fields relevant to
  1234. // PortAllocator populated.
  1235. PeerConnectionInterface::RTCConfiguration config;
  1236. config.disable_ipv6_on_wifi = true;
  1237. config.max_ipv6_networks = 10;
  1238. config.tcp_candidate_policy =
  1239. PeerConnectionInterface::kTcpCandidatePolicyDisabled;
  1240. config.candidate_network_policy =
  1241. PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
  1242. config.prune_turn_ports = true;
  1243. // Create the PC factory and PC with the above config.
  1244. rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
  1245. webrtc::CreatePeerConnectionFactory(
  1246. rtc::Thread::Current(), rtc::Thread::Current(),
  1247. rtc::Thread::Current(), fake_audio_capture_module_,
  1248. webrtc::CreateBuiltinAudioEncoderFactory(),
  1249. webrtc::CreateBuiltinAudioDecoderFactory(),
  1250. webrtc::CreateBuiltinVideoEncoderFactory(),
  1251. webrtc::CreateBuiltinVideoDecoderFactory(), nullptr /* audio_mixer */,
  1252. nullptr /* audio_processing */));
  1253. rtc::scoped_refptr<PeerConnectionInterface> pc(
  1254. pc_factory->CreatePeerConnection(config, std::move(port_allocator),
  1255. nullptr, &observer_));
  1256. EXPECT_TRUE(pc.get());
  1257. observer_.SetPeerConnectionInterface(pc.get());
  1258. // Now validate that the config fields set above were applied to the
  1259. // PortAllocator, as flags or otherwise.
  1260. EXPECT_FALSE(raw_port_allocator->flags() &
  1261. cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
  1262. EXPECT_EQ(10, raw_port_allocator->max_ipv6_networks());
  1263. EXPECT_TRUE(raw_port_allocator->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
  1264. EXPECT_TRUE(raw_port_allocator->flags() &
  1265. cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
  1266. EXPECT_EQ(webrtc::PRUNE_BASED_ON_PRIORITY,
  1267. raw_port_allocator->turn_port_prune_policy());
  1268. }
  1269. // Check that GetConfiguration returns the configuration the PeerConnection was
  1270. // constructed with, before SetConfiguration is called.
  1271. TEST_P(PeerConnectionInterfaceTest, GetConfigurationAfterCreatePeerConnection) {
  1272. PeerConnectionInterface::RTCConfiguration config;
  1273. config.type = PeerConnectionInterface::kRelay;
  1274. CreatePeerConnection(config);
  1275. PeerConnectionInterface::RTCConfiguration returned_config =
  1276. pc_->GetConfiguration();
  1277. EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
  1278. }
  1279. // Check that GetConfiguration returns the last configuration passed into
  1280. // SetConfiguration.
  1281. TEST_P(PeerConnectionInterfaceTest, GetConfigurationAfterSetConfiguration) {
  1282. PeerConnectionInterface::RTCConfiguration starting_config;
  1283. starting_config.bundle_policy =
  1284. webrtc::PeerConnection::kBundlePolicyMaxBundle;
  1285. CreatePeerConnection(starting_config);
  1286. PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
  1287. config.type = PeerConnectionInterface::kRelay;
  1288. config.use_media_transport = true;
  1289. config.use_media_transport_for_data_channels = true;
  1290. EXPECT_TRUE(pc_->SetConfiguration(config).ok());
  1291. PeerConnectionInterface::RTCConfiguration returned_config =
  1292. pc_->GetConfiguration();
  1293. EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
  1294. EXPECT_TRUE(returned_config.use_media_transport);
  1295. EXPECT_TRUE(returned_config.use_media_transport_for_data_channels);
  1296. }
  1297. TEST_P(PeerConnectionInterfaceTest, SetConfigurationFailsAfterClose) {
  1298. CreatePeerConnection();
  1299. pc_->Close();
  1300. EXPECT_FALSE(
  1301. pc_->SetConfiguration(PeerConnectionInterface::RTCConfiguration()).ok());
  1302. }
  1303. TEST_F(PeerConnectionInterfaceTestPlanB, AddStreams) {
  1304. CreatePeerConnectionWithoutDtls();
  1305. AddVideoStream(kStreamId1);
  1306. AddAudioStream(kStreamId2);
  1307. ASSERT_EQ(2u, pc_->local_streams()->count());
  1308. // Test we can add multiple local streams to one peerconnection.
  1309. rtc::scoped_refptr<MediaStreamInterface> stream(
  1310. pc_factory_->CreateLocalMediaStream(kStreamId3));
  1311. rtc::scoped_refptr<AudioTrackInterface> audio_track(
  1312. pc_factory_->CreateAudioTrack(kStreamId3,
  1313. static_cast<AudioSourceInterface*>(NULL)));
  1314. stream->AddTrack(audio_track.get());
  1315. EXPECT_TRUE(pc_->AddStream(stream));
  1316. EXPECT_EQ(3u, pc_->local_streams()->count());
  1317. // Remove the third stream.
  1318. pc_->RemoveStream(pc_->local_streams()->at(2));
  1319. EXPECT_EQ(2u, pc_->local_streams()->count());
  1320. // Remove the second stream.
  1321. pc_->RemoveStream(pc_->local_streams()->at(1));
  1322. EXPECT_EQ(1u, pc_->local_streams()->count());
  1323. // Remove the first stream.
  1324. pc_->RemoveStream(pc_->local_streams()->at(0));
  1325. EXPECT_EQ(0u, pc_->local_streams()->count());
  1326. }
  1327. // Test that the created offer includes streams we added.
  1328. // Don't run under Unified Plan since the stream API is not available.
  1329. TEST_F(PeerConnectionInterfaceTestPlanB, AddedStreamsPresentInOffer) {
  1330. CreatePeerConnectionWithoutDtls();
  1331. AddAudioVideoStream(kStreamId1, "audio_track", "video_track");
  1332. std::unique_ptr<SessionDescriptionInterface> offer;
  1333. ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
  1334. const cricket::AudioContentDescription* audio_desc =
  1335. cricket::GetFirstAudioContentDescription(offer->description());
  1336. EXPECT_TRUE(ContainsTrack(audio_desc->streams(), kStreamId1, "audio_track"));
  1337. const cricket::VideoContentDescription* video_desc =
  1338. cricket::GetFirstVideoContentDescription(offer->description());
  1339. EXPECT_TRUE(ContainsTrack(video_desc->streams(), kStreamId1, "video_track"));
  1340. // Add another stream and ensure the offer includes both the old and new
  1341. // streams.
  1342. AddAudioVideoStream(kStreamId2, "audio_track2", "video_track2");
  1343. ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
  1344. audio_desc = cricket::GetFirstAudioContentDescription(offer->description());
  1345. EXPECT_TRUE(ContainsTrack(audio_desc->streams(), kStreamId1, "audio_track"));
  1346. EXPECT_TRUE(ContainsTrack(audio_desc->streams(), kStreamId2, "audio_track2"));
  1347. video_desc = cricket::GetFirstVideoContentDescription(offer->description());
  1348. EXPECT_TRUE(ContainsTrack(video_desc->streams(), kStreamId1, "video_track"));
  1349. EXPECT_TRUE(ContainsTrack(video_desc->streams(), kStreamId2, "video_track2"));
  1350. }
  1351. // Don't run under Unified Plan since the stream API is not available.
  1352. TEST_F(PeerConnectionInterfaceTestPlanB, RemoveStream) {
  1353. CreatePeerConnectionWithoutDtls();
  1354. AddVideoStream(kStreamId1);
  1355. ASSERT_EQ(1u, pc_->local_streams()->count());
  1356. pc_->RemoveStream(pc_->local_streams()->at(0));
  1357. EXPECT_EQ(0u, pc_->local_streams()->count());
  1358. }
  1359. // Test for AddTrack and RemoveTrack methods.
  1360. // Tests that the created offer includes tracks we added,
  1361. // and that the RtpSenders are created correctly.
  1362. // Also tests that RemoveTrack removes the tracks from subsequent offers.
  1363. // Only tested with Plan B since Unified Plan is covered in more detail by tests
  1364. // in peerconnection_jsep_unittests.cc
  1365. TEST_F(PeerConnectionInterfaceTestPlanB, AddTrackRemoveTrack) {
  1366. CreatePeerConnectionWithoutDtls();
  1367. rtc::scoped_refptr<AudioTrackInterface> audio_track(
  1368. CreateAudioTrack("audio_track"));
  1369. rtc::scoped_refptr<VideoTrackInterface> video_track(
  1370. CreateVideoTrack("video_track"));
  1371. auto audio_sender = pc_->AddTrack(audio_track, {kStreamId1}).MoveValue();
  1372. auto video_sender = pc_->AddTrack(video_track, {kStreamId1}).MoveValue();
  1373. EXPECT_EQ(1UL, audio_sender->stream_ids().size());
  1374. EXPECT_EQ(kStreamId1, audio_sender->stream_ids()[0]);
  1375. EXPECT_EQ("audio_track", audio_sender->id());
  1376. EXPECT_EQ(audio_track, audio_sender->track());
  1377. EXPECT_EQ(1UL, video_sender->stream_ids().size());
  1378. EXPECT_EQ(kStreamId1, video_sender->stream_ids()[0]);
  1379. EXPECT_EQ("video_track", video_sender->id());
  1380. EXPECT_EQ(video_track, video_sender->track());
  1381. // Now create an offer and check for the senders.
  1382. std::unique_ptr<SessionDescriptionInterface> offer;
  1383. ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
  1384. const cricket::ContentInfo* audio_content =
  1385. cricket::GetFirstAudioContent(offer->description());
  1386. EXPECT_TRUE(ContainsTrack(audio_content->media_description()->streams(),
  1387. kStreamId1, "audio_track"));
  1388. const cricket::ContentInfo* video_content =
  1389. cricket::GetFirstVideoContent(offer->description());
  1390. EXPECT_TRUE(ContainsTrack(video_content->media_description()->streams(),
  1391. kStreamId1, "video_track"));
  1392. EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
  1393. // Now try removing the tracks.
  1394. EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
  1395. EXPECT_TRUE(pc_->RemoveTrack(video_sender));
  1396. // Create a new offer and ensure it doesn't contain the removed senders.
  1397. ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
  1398. audio_content = cricket::GetFirstAudioContent(offer->description());
  1399. EXPECT_FALSE(ContainsTrack(audio_content->media_description()->streams(),
  1400. kStreamId1, "audio_track"));
  1401. video_content = cricket::GetFirstVideoContent(offer->description());
  1402. EXPECT_FALSE(ContainsTrack(video_content->media_description()->streams(),
  1403. kStreamId1, "video_track"));
  1404. EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
  1405. // Calling RemoveTrack on a sender no longer attached to a PeerConnection
  1406. // should return false.
  1407. EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
  1408. EXPECT_FALSE(pc_->RemoveTrack(video_sender));
  1409. }
  1410. // Test creating senders without a stream specified,
  1411. // expecting a random stream ID to be generated.
  1412. TEST_P(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
  1413. CreatePeerConnectionWithoutDtls();
  1414. rtc::scoped_refptr<AudioTrackInterface> audio_track(
  1415. CreateAudioTrack("audio_track"));
  1416. rtc::scoped_refptr<VideoTrackInterface> video_track(
  1417. CreateVideoTrack("video_track"));
  1418. auto audio_sender =
  1419. pc_->AddTrack(audio_track, std::vector<std::string>()).MoveValue();
  1420. auto video_sender =
  1421. pc_->AddTrack(video_track, std::vector<std::string>()).MoveValue();
  1422. EXPECT_EQ("audio_track", audio_sender->id());
  1423. EXPECT_EQ(audio_track, audio_sender->track());
  1424. EXPECT_EQ("video_track", video_sender->id());
  1425. EXPECT_EQ(video_track, video_sender->track());
  1426. if (sdp_semantics_ == SdpSemantics::kPlanB) {
  1427. // If the ID is truly a random GUID, it should be infinitely unlikely they
  1428. // will be the same.
  1429. EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
  1430. } else {
  1431. // We allows creating tracks without stream ids under Unified Plan
  1432. // semantics.
  1433. EXPECT_EQ(0u, video_sender->stream_ids().size());
  1434. EXPECT_EQ(0u, audio_sender->stream_ids().size());
  1435. }
  1436. }
  1437. // Test that we can call GetStats() after AddTrack but before connecting
  1438. // the PeerConnection to a peer.
  1439. TEST_P(PeerConnectionInterfaceTest, AddTrackBeforeConnecting) {
  1440. CreatePeerConnectionWithoutDtls();
  1441. rtc::scoped_refptr<AudioTrackInterface> audio_track(
  1442. CreateAudioTrack("audio_track"));
  1443. rtc::scoped_refptr<VideoTrackInterface> video_track(
  1444. CreateVideoTrack("video_track"));
  1445. auto audio_sender = pc_->AddTrack(audio_track, std::vector<std::string>());
  1446. auto video_sender = pc_->AddTrack(video_track, std::vector<std::string>());
  1447. EXPECT_TRUE(DoGetStats(nullptr));
  1448. }
  1449. TEST_P(PeerConnectionInterfaceTest, AttachmentIdIsSetOnAddTrack) {
  1450. CreatePeerConnectionWithoutDtls();
  1451. rtc::scoped_refptr<AudioTrackInterface> audio_track(
  1452. CreateAudioTrack("audio_track"));
  1453. rtc::scoped_refptr<VideoTrackInterface> video_track(
  1454. CreateVideoTrack("video_track"));
  1455. auto audio_sender = pc_->AddTrack(audio_track, std::vector<std::string>());
  1456. ASSERT_TRUE(audio_sender.ok());
  1457. auto* audio_sender_proxy =
  1458. static_cast<RtpSenderProxyWithInternal<RtpSenderInternal>*>(
  1459. audio_sender.value().get());
  1460. EXPECT_NE(0, audio_sender_proxy->internal()->AttachmentId());
  1461. auto video_sender = pc_->AddTrack(video_track, std::vector<std::string>());
  1462. ASSERT_TRUE(video_sender.ok());
  1463. auto* video_sender_proxy =
  1464. static_cast<RtpSenderProxyWithInternal<RtpSenderInternal>*>(
  1465. video_sender.value().get());
  1466. EXPECT_NE(0, video_sender_proxy->internal()->AttachmentId());
  1467. }
  1468. // Don't run under Unified Plan since the stream API is not available.
  1469. TEST_F(PeerConnectionInterfaceTestPlanB, AttachmentIdIsSetOnAddStream) {
  1470. CreatePeerConnectionWithoutDtls();
  1471. AddVideoStream(kStreamId1);
  1472. auto senders = pc_->GetSenders();
  1473. ASSERT_EQ(1u, senders.size());
  1474. auto* sender_proxy =
  1475. static_cast<RtpSenderProxyWithInternal<RtpSenderInternal>*>(
  1476. senders[0].get());
  1477. EXPECT_NE(0, sender_proxy->internal()->AttachmentId());
  1478. }
  1479. TEST_P(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
  1480. InitiateCall();
  1481. WaitAndVerifyOnAddStream(kStreamId1, 2);
  1482. VerifyRemoteRtpHeaderExtensions();
  1483. }
  1484. TEST_P(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
  1485. CreatePeerConnectionWithoutDtls();
  1486. AddVideoTrack(kVideoTracks[0], {kStreamId1});
  1487. CreateOfferAsLocalDescription();
  1488. std::string offer;
  1489. EXPECT_TRUE(pc_->local_description()->ToString(&offer));
  1490. CreatePrAnswerAndAnswerAsRemoteDescription(offer);
  1491. WaitAndVerifyOnAddStream(kStreamId1, 1);
  1492. }
  1493. TEST_P(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
  1494. CreatePeerConnectionWithoutDtls();
  1495. AddVideoTrack(kVideoTracks[0], {kStreamId1});
  1496. CreateOfferAsRemoteDescription();
  1497. CreateAnswerAsLocalDescription();
  1498. WaitAndVerifyOnAddStream(kStreamId1, 1);
  1499. }
  1500. TEST_P(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
  1501. CreatePeerConnectionWithoutDtls();
  1502. AddVideoTrack(kVideoTracks[0], {kStreamId1});
  1503. CreateOfferAsRemoteDescription();
  1504. CreatePrAnswerAsLocalDescription();
  1505. CreateAnswerAsLocalDescription();
  1506. WaitAndVerifyOnAddStream(kStreamId1, 1);
  1507. }
  1508. // Don't run under Unified Plan since the stream API is not available.
  1509. TEST_F(PeerConnectionInterfaceTestPlanB, Renegotiate) {
  1510. InitiateCall();
  1511. ASSERT_EQ(1u, pc_->remote_streams()->count());
  1512. pc_->RemoveStream(pc_->local_streams()->at(0));
  1513. CreateOfferReceiveAnswer();
  1514. EXPECT_EQ(0u, pc_->remote_streams()->count());
  1515. AddVideoStream(kStreamId1);
  1516. CreateOfferReceiveAnswer();
  1517. }
  1518. // Tests that after negotiating an audio only call, the respondent can perform a
  1519. // renegotiation that removes the audio stream.
  1520. TEST_F(PeerConnectionInterfaceTestPlanB, RenegotiateAudioOnly) {
  1521. CreatePeerConnectionWithoutDtls();
  1522. AddAudioStream(kStreamId1);
  1523. CreateOfferAsRemoteDescription();
  1524. CreateAnswerAsLocalDescription();
  1525. ASSERT_EQ(1u, pc_->remote_streams()->count());
  1526. pc_->RemoveStream(pc_->local_streams()->at(0));
  1527. CreateOfferReceiveAnswer();
  1528. EXPECT_EQ(0u, pc_->remote_streams()->count());
  1529. }
  1530. // Test that candidates are generated and that we can parse our own candidates.
  1531. TEST_P(PeerConnectionInterfaceTest, IceCandidates) {
  1532. CreatePeerConnectionWithoutDtls();
  1533. EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate()));
  1534. // SetRemoteDescription takes ownership of offer.
  1535. std::unique_ptr<SessionDescriptionInterface> offer;
  1536. AddVideoTrack(kVideoTracks[0]);
  1537. EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
  1538. EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
  1539. // SetLocalDescription takes ownership of answer.
  1540. std::unique_ptr<SessionDescriptionInterface> answer;
  1541. EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
  1542. EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
  1543. EXPECT_TRUE_WAIT(observer_.last_candidate() != nullptr, kTimeout);
  1544. EXPECT_TRUE_WAIT(observer_.ice_gathering_complete_, kTimeout);
  1545. EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate()));
  1546. }
  1547. // Test that CreateOffer and CreateAnswer will fail if the track labels are
  1548. // not unique.
  1549. TEST_F(PeerConnectionInterfaceTestPlanB, CreateOfferAnswerWithInvalidStream) {
  1550. CreatePeerConnectionWithoutDtls();
  1551. // Create a regular offer for the CreateAnswer test later.
  1552. std::unique_ptr<SessionDescriptionInterface> offer;
  1553. EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
  1554. EXPECT_TRUE(offer);
  1555. offer.reset();
  1556. // Create a local stream with audio&video tracks having same label.
  1557. AddAudioTrack("track_label", {kStreamId1});
  1558. AddVideoTrack("track_label", {kStreamId1});
  1559. // Test CreateOffer
  1560. EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
  1561. // Test CreateAnswer
  1562. std::unique_ptr<SessionDescriptionInterface> answer;
  1563. EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
  1564. }
  1565. // Test that we will get different SSRCs for each tracks in the offer and answer
  1566. // we created.
  1567. TEST_P(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
  1568. CreatePeerConnectionWithoutDtls();
  1569. // Create a local stream with audio&video tracks having different labels.
  1570. AddAudioTrack(kAudioTracks[0], {kStreamId1});
  1571. AddVideoTrack(kVideoTracks[0], {kStreamId1});
  1572. // Test CreateOffer
  1573. std::unique_ptr<SessionDescriptionInterface> offer;
  1574. ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
  1575. int audio_ssrc = 0;
  1576. int video_ssrc = 0;
  1577. EXPECT_TRUE(
  1578. GetFirstSsrc(GetFirstAudioContent(offer->description()), &audio_ssrc));
  1579. EXPECT_TRUE(
  1580. GetFirstSsrc(GetFirstVideoContent(offer->description()), &video_ssrc));
  1581. EXPECT_NE(audio_ssrc, video_ssrc);
  1582. // Test CreateAnswer
  1583. EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
  1584. std::unique_ptr<SessionDescriptionInterface> answer;
  1585. ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
  1586. audio_ssrc = 0;
  1587. video_ssrc = 0;
  1588. EXPECT_TRUE(
  1589. GetFirstSsrc(GetFirstAudioContent(answer->description()), &audio_ssrc));
  1590. EXPECT_TRUE(
  1591. GetFirstSsrc(GetFirstVideoContent(answer->description()), &video_ssrc));
  1592. EXPECT_NE(audio_ssrc, video_ssrc);
  1593. }
  1594. // Test that it's possible to call AddTrack on a MediaStream after adding
  1595. // the stream to a PeerConnection.
  1596. // TODO(deadbeef): Remove this test once this behavior is no longer supported.
  1597. // Don't run under Unified Plan since the stream API is not available.
  1598. TEST_F(PeerConnectionInterfaceTestPlanB, AddTrackAfterAddStream) {
  1599. CreatePeerConnectionWithoutDtls();
  1600. // Create audio stream and add to PeerConnection.
  1601. AddAudioStream(kStreamId1);
  1602. MediaStreamInterface* stream = pc_->local_streams()->at(0);
  1603. // Add video track to the audio-only stream.
  1604. rtc::scoped_refptr<VideoTrackInterface> video_track(
  1605. CreateVideoTrack("video_label"));
  1606. stream->AddTrack(video_track.get());
  1607. std::unique_ptr<SessionDescriptionInterface> offer;
  1608. ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
  1609. const cricket::MediaContentDescription* video_desc =
  1610. cricket::GetFirstVideoContentDescription(offer->description());
  1611. EXPECT_TRUE(video_desc != nullptr);
  1612. }
  1613. // Test that it's possible to call RemoveTrack on a MediaStream after adding
  1614. // the stream to a PeerConnection.
  1615. // TODO(deadbeef): Remove this test once this behavior is no longer supported.
  1616. // Don't run under Unified Plan since the stream API is not available.
  1617. TEST_F(PeerConnectionInterfaceTestPlanB, RemoveTrackAfterAddStream) {
  1618. CreatePeerConnectionWithoutDtls();
  1619. // Create audio/video stream and add to PeerConnection.
  1620. AddAudioVideoStream(kStreamId1, "audio_label", "video_label");
  1621. MediaStreamInterface* stream = pc_->local_streams()->at(0);
  1622. // Remove the video track.
  1623. stream->RemoveTrack(stream->GetVideoTracks()[0]);
  1624. std::unique_ptr<SessionDescriptionInterface> offer;
  1625. ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
  1626. const cricket::MediaContentDescription* video_desc =
  1627. cricket::GetFirstVideoContentDescription(offer->description());
  1628. EXPECT_TRUE(video_desc == nullptr);
  1629. }
  1630. // Test creating a sender with a stream ID, and ensure the ID is populated
  1631. // in the offer.
  1632. // Don't run under Unified Plan since the stream API is not available.
  1633. TEST_F(PeerConnectionInterfaceTestPlanB, CreateSenderWithStream) {
  1634. CreatePeerConnectionWithoutDtls();
  1635. pc_->CreateSender("video", kStreamId1);
  1636. std::unique_ptr<SessionDescriptionInterface> offer;
  1637. ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
  1638. const cricket::MediaContentDescription* video_desc =
  1639. cricket::GetFirstVideoContentDescription(offer->description());
  1640. ASSERT_TRUE(video_desc != nullptr);
  1641. ASSERT_EQ(1u, video_desc->streams().size());
  1642. EXPECT_EQ(kStreamId1, video_desc->streams()[0].first_stream_id());
  1643. }
  1644. // Test that we can specify a certain track that we want statistics about.
  1645. TEST_P(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
  1646. InitiateCall();
  1647. ASSERT_LT(0u, pc_->GetSenders().size());
  1648. ASSERT_LT(0u, pc_->GetReceivers().size());
  1649. rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
  1650. pc_->GetReceivers()[0]->track();
  1651. EXPECT_TRUE(DoGetStats(remote_audio));
  1652. // Remove the stream. Since we are sending to our selves the local
  1653. // and the remote stream is the same.
  1654. pc_->RemoveTrack(pc_->GetSenders()[0]);
  1655. // Do a re-negotiation.
  1656. CreateOfferReceiveAnswer();
  1657. // Test that we still can get statistics for the old track. Even if it is not
  1658. // sent any longer.
  1659. EXPECT_TRUE(DoGetStats(remote_audio));
  1660. }
  1661. // Test that we can get stats on a video track.
  1662. TEST_P(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
  1663. InitiateCall();
  1664. auto video_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_VIDEO);
  1665. ASSERT_TRUE(video_receiver);
  1666. EXPECT_TRUE(DoGetStats(video_receiver->track()));
  1667. }
  1668. // Test that we don't get statistics for an invalid track.
  1669. TEST_P(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
  1670. InitiateCall();
  1671. rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
  1672. pc_factory_->CreateAudioTrack("unknown track", NULL));
  1673. EXPECT_FALSE(DoGetStats(unknown_audio_track));
  1674. }
  1675. TEST_P(PeerConnectionInterfaceTest, GetRTCStatsBeforeAndAfterCalling) {
  1676. CreatePeerConnectionWithoutDtls();
  1677. EXPECT_TRUE(DoGetRTCStats());
  1678. // Clearing stats cache is needed now, but should be temporary.
  1679. // https://bugs.chromium.org/p/webrtc/issues/detail?id=8693
  1680. pc_->ClearStatsCache();
  1681. AddAudioTrack(kAudioTracks[0], {kStreamId1});
  1682. AddVideoTrack(kVideoTracks[0], {kStreamId1});
  1683. EXPECT_TRUE(DoGetRTCStats());
  1684. pc_->ClearStatsCache();
  1685. CreateOfferReceiveAnswer();
  1686. EXPECT_TRUE(DoGetRTCStats());
  1687. }
  1688. // This test setup two RTP data channels in loop back.
  1689. TEST_P(PeerConnectionInterfaceTest, TestDataChannel) {
  1690. RTCConfiguration config;
  1691. config.enable_rtp_data_channel = true;
  1692. config.enable_dtls_srtp = false;
  1693. CreatePeerConnection(config);
  1694. rtc::scoped_refptr<DataChannelInterface> data1 =
  1695. pc_->CreateDataChannel("test1", NULL);
  1696. rtc::scoped_refptr<DataChannelInterface> data2 =
  1697. pc_->CreateDataChannel("test2", NULL);
  1698. ASSERT_TRUE(data1 != NULL);
  1699. std::unique_ptr<MockDataChannelObserver> observer1(
  1700. new MockDataChannelObserver(data1));
  1701. std::unique_ptr<MockDataChannelObserver> observer2(
  1702. new MockDataChannelObserver(data2));
  1703. EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
  1704. EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
  1705. std::string data_to_send1 = "testing testing";
  1706. std::string data_to_send2 = "testing something else";
  1707. EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
  1708. CreateOfferReceiveAnswer();
  1709. EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
  1710. EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
  1711. EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
  1712. EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
  1713. EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
  1714. EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
  1715. EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
  1716. EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
  1717. data1->Close();
  1718. EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
  1719. CreateOfferReceiveAnswer();
  1720. EXPECT_FALSE(observer1->IsOpen());
  1721. EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
  1722. EXPECT_TRUE(observer2->IsOpen());
  1723. data_to_send2 = "testing something else again";
  1724. EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
  1725. EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
  1726. }
  1727. // This test verifies that sendnig binary data over RTP data channels should
  1728. // fail.
  1729. TEST_P(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
  1730. RTCConfiguration config;
  1731. config.enable_rtp_data_channel = true;
  1732. config.enable_dtls_srtp = false;
  1733. CreatePeerConnection(config);
  1734. rtc::scoped_refptr<DataChannelInterface> data1 =
  1735. pc_->CreateDataChannel("test1", NULL);
  1736. rtc::scoped_refptr<DataChannelInterface> data2 =
  1737. pc_->CreateDataChannel("test2", NULL);
  1738. ASSERT_TRUE(data1 != NULL);
  1739. std::unique_ptr<MockDataChannelObserver> observer1(
  1740. new MockDataChannelObserver(data1));
  1741. std::unique_ptr<MockDataChannelObserver> observer2(
  1742. new MockDataChannelObserver(data2));
  1743. EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
  1744. EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
  1745. CreateOfferReceiveAnswer();
  1746. EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
  1747. EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
  1748. EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
  1749. EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
  1750. rtc::CopyOnWriteBuffer buffer("test", 4);
  1751. EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
  1752. }
  1753. // This test setup a RTP data channels in loop back and test that a channel is
  1754. // opened even if the remote end answer with a zero SSRC.
  1755. TEST_P(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
  1756. RTCConfiguration config;
  1757. config.enable_rtp_data_channel = true;
  1758. config.enable_dtls_srtp = false;
  1759. CreatePeerConnection(config);
  1760. rtc::scoped_refptr<DataChannelInterface> data1 =
  1761. pc_->CreateDataChannel("test1", NULL);
  1762. std::unique_ptr<MockDataChannelObserver> observer1(
  1763. new MockDataChannelObserver(data1));
  1764. CreateOfferReceiveAnswerWithoutSsrc();
  1765. EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
  1766. data1->Close();
  1767. EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
  1768. CreateOfferReceiveAnswerWithoutSsrc();
  1769. EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
  1770. EXPECT_FALSE(observer1->IsOpen());
  1771. }
  1772. // This test that if a data channel is added in an answer a receive only channel
  1773. // channel is created.
  1774. TEST_P(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
  1775. RTCConfiguration config;
  1776. config.enable_rtp_data_channel = true;
  1777. config.enable_dtls_srtp = false;
  1778. CreatePeerConnection(config);
  1779. std::string offer_label = "offer_channel";
  1780. rtc::scoped_refptr<DataChannelInterface> offer_channel =
  1781. pc_->CreateDataChannel(offer_label, NULL);
  1782. CreateOfferAsLocalDescription();
  1783. // Replace the data channel label in the offer and apply it as an answer.
  1784. std::string receive_label = "answer_channel";
  1785. std::string sdp;
  1786. EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
  1787. absl::StrReplaceAll({{offer_label, receive_label}}, &sdp);
  1788. CreateAnswerAsRemoteDescription(sdp);
  1789. // Verify that a new incoming data channel has been created and that
  1790. // it is open but can't we written to.
  1791. ASSERT_TRUE(observer_.last_datachannel_ != NULL);
  1792. DataChannelInterface* received_channel = observer_.last_datachannel_;
  1793. EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
  1794. EXPECT_EQ(receive_label, received_channel->label());
  1795. EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
  1796. // Verify that the channel we initially offered has been rejected.
  1797. EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
  1798. // Do another offer / answer exchange and verify that the data channel is
  1799. // opened.
  1800. CreateOfferReceiveAnswer();
  1801. EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
  1802. kTimeout);
  1803. }
  1804. // This test that no data channel is returned if a reliable channel is
  1805. // requested.
  1806. // TODO(perkj): Remove this test once reliable channels are implemented.
  1807. TEST_P(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
  1808. RTCConfiguration rtc_config;
  1809. rtc_config.enable_rtp_data_channel = true;
  1810. CreatePeerConnection(rtc_config);
  1811. std::string label = "test";
  1812. webrtc::DataChannelInit config;
  1813. config.reliable = true;
  1814. rtc::scoped_refptr<DataChannelInterface> channel =
  1815. pc_->CreateDataChannel(label, &config);
  1816. EXPECT_TRUE(channel == NULL);
  1817. }
  1818. // Verifies that duplicated label is not allowed for RTP data channel.
  1819. TEST_P(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
  1820. RTCConfiguration config;
  1821. config.enable_rtp_data_channel = true;
  1822. CreatePeerConnection(config);
  1823. std::string label = "test";
  1824. rtc::scoped_refptr<DataChannelInterface> channel =
  1825. pc_->CreateDataChannel(label, nullptr);
  1826. EXPECT_NE(channel, nullptr);
  1827. rtc::scoped_refptr<DataChannelInterface> dup_channel =
  1828. pc_->CreateDataChannel(label, nullptr);
  1829. EXPECT_EQ(dup_channel, nullptr);
  1830. }
  1831. // This tests that a SCTP data channel is returned using different
  1832. // DataChannelInit configurations.
  1833. TEST_P(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
  1834. RTCConfiguration rtc_config;
  1835. rtc_config.enable_dtls_srtp = true;
  1836. CreatePeerConnection(rtc_config);
  1837. webrtc::DataChannelInit config;
  1838. rtc::scoped_refptr<DataChannelInterface> channel =
  1839. pc_->CreateDataChannel("1", &config);
  1840. EXPECT_TRUE(channel != NULL);
  1841. EXPECT_TRUE(channel->reliable());
  1842. EXPECT_TRUE(observer_.renegotiation_needed_);
  1843. observer_.renegotiation_needed_ = false;
  1844. config.ordered = false;
  1845. channel = pc_->CreateDataChannel("2", &config);
  1846. EXPECT_TRUE(channel != NULL);
  1847. EXPECT_TRUE(channel->reliable());
  1848. EXPECT_FALSE(observer_.renegotiation_needed_);
  1849. config.ordered = true;
  1850. config.maxRetransmits = 0;
  1851. channel = pc_->CreateDataChannel("3", &config);
  1852. EXPECT_TRUE(channel != NULL);
  1853. EXPECT_FALSE(channel->reliable());
  1854. EXPECT_FALSE(observer_.renegotiation_needed_);
  1855. config.maxRetransmits = absl::nullopt;
  1856. config.maxRetransmitTime = 0;
  1857. channel = pc_->CreateDataChannel("4", &config);
  1858. EXPECT_TRUE(channel != NULL);
  1859. EXPECT_FALSE(channel->reliable());
  1860. EXPECT_FALSE(observer_.renegotiation_needed_);
  1861. }
  1862. // For backwards compatibility, we want people who "unset" maxRetransmits
  1863. // and maxRetransmitTime by setting them to -1 to get what they want.
  1864. TEST_P(PeerConnectionInterfaceTest, CreateSctpDataChannelWithMinusOne) {
  1865. RTCConfiguration rtc_config;
  1866. rtc_config.enable_dtls_srtp = true;
  1867. CreatePeerConnection(rtc_config);
  1868. webrtc::DataChannelInit config;
  1869. config.maxRetransmitTime = -1;
  1870. config.maxRetransmits = -1;
  1871. rtc::scoped_refptr<DataChannelInterface> channel =
  1872. pc_->CreateDataChannel("1", &config);
  1873. EXPECT_TRUE(channel != NULL);
  1874. }
  1875. // This tests that no data channel is returned if both maxRetransmits and
  1876. // maxRetransmitTime are set for SCTP data channels.
  1877. TEST_P(PeerConnectionInterfaceTest,
  1878. CreateSctpDataChannelShouldFailForInvalidConfig) {
  1879. RTCConfiguration rtc_config;
  1880. rtc_config.enable_dtls_srtp = true;
  1881. CreatePeerConnection(rtc_config);
  1882. std::string label = "test";
  1883. webrtc::DataChannelInit config;
  1884. config.maxRetransmits = 0;
  1885. config.maxRetransmitTime = 0;
  1886. rtc::scoped_refptr<DataChannelInterface> channel =
  1887. pc_->CreateDataChannel(label, &config);
  1888. EXPECT_TRUE(channel == NULL);
  1889. }
  1890. // The test verifies that creating a SCTP data channel with an id already in use
  1891. // or out of range should fail.
  1892. TEST_P(PeerConnectionInterfaceTest,
  1893. CreateSctpDataChannelWithInvalidIdShouldFail) {
  1894. RTCConfiguration rtc_config;
  1895. rtc_config.enable_dtls_srtp = true;
  1896. CreatePeerConnection(rtc_config);
  1897. webrtc::DataChannelInit config;
  1898. rtc::scoped_refptr<DataChannelInterface> channel;
  1899. config.id = 1;
  1900. config.negotiated = true;
  1901. channel = pc_->CreateDataChannel("1", &config);
  1902. EXPECT_TRUE(channel != NULL);
  1903. EXPECT_EQ(1, channel->id());
  1904. channel = pc_->CreateDataChannel("x", &config);
  1905. EXPECT_TRUE(channel == NULL);
  1906. config.id = cricket::kMaxSctpSid;
  1907. config.negotiated = true;
  1908. channel = pc_->CreateDataChannel("max", &config);
  1909. EXPECT_TRUE(channel != NULL);
  1910. EXPECT_EQ(config.id, channel->id());
  1911. config.id = cricket::kMaxSctpSid + 1;
  1912. config.negotiated = true;
  1913. channel = pc_->CreateDataChannel("x", &config);
  1914. EXPECT_TRUE(channel == NULL);
  1915. }
  1916. // Verifies that duplicated label is allowed for SCTP data channel.
  1917. TEST_P(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
  1918. RTCConfiguration rtc_config;
  1919. rtc_config.enable_dtls_srtp = true;
  1920. CreatePeerConnection(rtc_config);
  1921. std::string label = "test";
  1922. rtc::scoped_refptr<DataChannelInterface> channel =
  1923. pc_->CreateDataChannel(label, nullptr);
  1924. EXPECT_NE(channel, nullptr);
  1925. rtc::scoped_refptr<DataChannelInterface> dup_channel =
  1926. pc_->CreateDataChannel(label, nullptr);
  1927. EXPECT_NE(dup_channel, nullptr);
  1928. }
  1929. // This test verifies that OnRenegotiationNeeded is fired for every new RTP
  1930. // DataChannel.
  1931. TEST_P(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
  1932. RTCConfiguration rtc_config;
  1933. rtc_config.enable_rtp_data_channel = true;
  1934. rtc_config.enable_dtls_srtp = false;
  1935. CreatePeerConnection(rtc_config);
  1936. rtc::scoped_refptr<DataChannelInterface> dc1 =
  1937. pc_->CreateDataChannel("test1", NULL);
  1938. EXPECT_TRUE(observer_.renegotiation_needed_);
  1939. observer_.renegotiation_needed_ = false;
  1940. CreateOfferReceiveAnswer();
  1941. rtc::scoped_refptr<DataChannelInterface> dc2 =
  1942. pc_->CreateDataChannel("test2", NULL);
  1943. EXPECT_EQ(observer_.renegotiation_needed_,
  1944. GetParam() == SdpSemantics::kPlanB);
  1945. }
  1946. // This test that a data channel closes when a PeerConnection is deleted/closed.
  1947. TEST_P(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
  1948. RTCConfiguration rtc_config;
  1949. rtc_config.enable_rtp_data_channel = true;
  1950. rtc_config.enable_dtls_srtp = false;
  1951. CreatePeerConnection(rtc_config);
  1952. rtc::scoped_refptr<DataChannelInterface> data1 =
  1953. pc_->CreateDataChannel("test1", NULL);
  1954. rtc::scoped_refptr<DataChannelInterface> data2 =
  1955. pc_->CreateDataChannel("test2", NULL);
  1956. ASSERT_TRUE(data1 != NULL);
  1957. std::unique_ptr<MockDataChannelObserver> observer1(
  1958. new MockDataChannelObserver(data1));
  1959. std::unique_ptr<MockDataChannelObserver> observer2(
  1960. new MockDataChannelObserver(data2));
  1961. CreateOfferReceiveAnswer();
  1962. EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
  1963. EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
  1964. ReleasePeerConnection();
  1965. EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
  1966. EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
  1967. }
  1968. // This tests that RTP data channels can be rejected in an answer.
  1969. TEST_P(PeerConnectionInterfaceTest, TestRejectRtpDataChannelInAnswer) {
  1970. RTCConfiguration rtc_config;
  1971. rtc_config.enable_rtp_data_channel = true;
  1972. rtc_config.enable_dtls_srtp = false;
  1973. CreatePeerConnection(rtc_config);
  1974. rtc::scoped_refptr<DataChannelInterface> offer_channel(
  1975. pc_->CreateDataChannel("offer_channel", NULL));
  1976. CreateOfferAsLocalDescription();
  1977. // Create an answer where the m-line for data channels are rejected.
  1978. std::string sdp;
  1979. EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
  1980. std::unique_ptr<SessionDescriptionInterface> answer(
  1981. webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
  1982. ASSERT_TRUE(answer);
  1983. cricket::ContentInfo* data_info =
  1984. cricket::GetFirstDataContent(answer->description());
  1985. data_info->rejected = true;
  1986. DoSetRemoteDescription(std::move(answer));
  1987. EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
  1988. }
  1989. #ifdef HAVE_SCTP
  1990. // This tests that SCTP data channels can be rejected in an answer.
  1991. TEST_P(PeerConnectionInterfaceTest, TestRejectSctpDataChannelInAnswer)
  1992. #else
  1993. TEST_P(PeerConnectionInterfaceTest, DISABLED_TestRejectSctpDataChannelInAnswer)
  1994. #endif
  1995. {
  1996. RTCConfiguration rtc_config;
  1997. CreatePeerConnection(rtc_config);
  1998. rtc::scoped_refptr<DataChannelInterface> offer_channel(
  1999. pc_->CreateDataChannel("offer_channel", NULL));
  2000. CreateOfferAsLocalDescription();
  2001. // Create an answer where the m-line for data channels are rejected.
  2002. std::string sdp;
  2003. EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
  2004. std::unique_ptr<SessionDescriptionInterface> answer(
  2005. webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
  2006. ASSERT_TRUE(answer);
  2007. cricket::ContentInfo* data_info =
  2008. cricket::GetFirstDataContent(answer->description());
  2009. data_info->rejected = true;
  2010. DoSetRemoteDescription(std::move(answer));
  2011. EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
  2012. }
  2013. // Test that we can create a session description from an SDP string from
  2014. // FireFox, use it as a remote session description, generate an answer and use
  2015. // the answer as a local description.
  2016. TEST_P(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
  2017. RTCConfiguration rtc_config;
  2018. rtc_config.enable_dtls_srtp = true;
  2019. CreatePeerConnection(rtc_config);
  2020. AddAudioTrack("audio_label");
  2021. AddVideoTrack("video_label");
  2022. std::unique_ptr<SessionDescriptionInterface> desc(
  2023. webrtc::CreateSessionDescription(SdpType::kOffer,
  2024. webrtc::kFireFoxSdpOffer, nullptr));
  2025. EXPECT_TRUE(DoSetSessionDescription(std::move(desc), false));
  2026. CreateAnswerAsLocalDescription();
  2027. ASSERT_TRUE(pc_->local_description() != NULL);
  2028. ASSERT_TRUE(pc_->remote_description() != NULL);
  2029. const cricket::ContentInfo* content =
  2030. cricket::GetFirstAudioContent(pc_->local_description()->description());
  2031. ASSERT_TRUE(content != NULL);
  2032. EXPECT_FALSE(content->rejected);
  2033. content =
  2034. cricket::GetFirstVideoContent(pc_->local_description()->description());
  2035. ASSERT_TRUE(content != NULL);
  2036. EXPECT_FALSE(content->rejected);
  2037. #ifdef HAVE_SCTP
  2038. content =
  2039. cricket::GetFirstDataContent(pc_->local_description()->description());
  2040. ASSERT_TRUE(content != NULL);
  2041. EXPECT_FALSE(content->rejected);
  2042. #endif
  2043. }
  2044. // Test that fallback from DTLS to SDES is not supported.
  2045. // The fallback was previously supported but was removed to simplify the code
  2046. // and because it's non-standard.
  2047. TEST_P(PeerConnectionInterfaceTest, DtlsSdesFallbackNotSupported) {
  2048. RTCConfiguration rtc_config;
  2049. rtc_config.enable_dtls_srtp = true;
  2050. CreatePeerConnection(rtc_config);
  2051. // Wait for fake certificate to be generated. Previously, this is what caused
  2052. // the "a=crypto" lines to be rejected.
  2053. AddAudioTrack("audio_label");
  2054. AddVideoTrack("video_label");
  2055. ASSERT_NE(nullptr, fake_certificate_generator_);
  2056. EXPECT_EQ_WAIT(1, fake_certificate_generator_->generated_certificates(),
  2057. kTimeout);
  2058. std::unique_ptr<SessionDescriptionInterface> desc(
  2059. webrtc::CreateSessionDescription(SdpType::kOffer, kDtlsSdesFallbackSdp,
  2060. nullptr));
  2061. EXPECT_FALSE(DoSetSessionDescription(std::move(desc), /*local=*/false));
  2062. }
  2063. // Test that we can create an audio only offer and receive an answer with a
  2064. // limited set of audio codecs and receive an updated offer with more audio
  2065. // codecs, where the added codecs are not supported.
  2066. TEST_P(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
  2067. CreatePeerConnectionWithoutDtls();
  2068. AddAudioTrack("audio_label");
  2069. CreateOfferAsLocalDescription();
  2070. const char* answer_sdp =
  2071. (sdp_semantics_ == SdpSemantics::kPlanB ? webrtc::kAudioSdpPlanB
  2072. : webrtc::kAudioSdpUnifiedPlan);
  2073. std::unique_ptr<SessionDescriptionInterface> answer(
  2074. webrtc::CreateSessionDescription(SdpType::kAnswer, answer_sdp, nullptr));
  2075. EXPECT_TRUE(DoSetSessionDescription(std::move(answer), false));
  2076. const char* reoffer_sdp =
  2077. (sdp_semantics_ == SdpSemantics::kPlanB
  2078. ? webrtc::kAudioSdpWithUnsupportedCodecsPlanB
  2079. : webrtc::kAudioSdpWithUnsupportedCodecsUnifiedPlan);
  2080. std::unique_ptr<SessionDescriptionInterface> updated_offer(
  2081. webrtc::CreateSessionDescription(SdpType::kOffer, reoffer_sdp, nullptr));
  2082. EXPECT_TRUE(DoSetSessionDescription(std::move(updated_offer), false));
  2083. CreateAnswerAsLocalDescription();
  2084. }
  2085. // Test that if we're receiving (but not sending) a track, subsequent offers
  2086. // will have m-lines with a=recvonly.
  2087. TEST_P(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
  2088. RTCConfiguration rtc_config;
  2089. rtc_config.enable_dtls_srtp = true;
  2090. CreatePeerConnection(rtc_config);
  2091. CreateAndSetRemoteOffer(GetSdpStringWithStream1());
  2092. CreateAnswerAsLocalDescription();
  2093. // At this point we should be receiving stream 1, but not sending anything.
  2094. // A new offer should be recvonly.
  2095. std::unique_ptr<SessionDescriptionInterface> offer;
  2096. DoCreateOffer(&offer, nullptr);
  2097. const cricket::ContentInfo* video_content =
  2098. cricket::GetFirstVideoContent(offer->description());
  2099. ASSERT_EQ(RtpTransceiverDirection::kRecvOnly,
  2100. video_content->media_description()->direction());
  2101. const cricket::ContentInfo* audio_content =
  2102. cricket::GetFirstAudioContent(offer->description());
  2103. ASSERT_EQ(RtpTransceiverDirection::kRecvOnly,
  2104. audio_content->media_description()->direction());
  2105. }
  2106. // Test that if we're receiving (but not sending) a track, and the
  2107. // offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
  2108. // false, the generated m-lines will be a=inactive.
  2109. TEST_P(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
  2110. RTCConfiguration rtc_config;
  2111. rtc_config.enable_dtls_srtp = true;
  2112. CreatePeerConnection(rtc_config);
  2113. CreateAndSetRemoteOffer(GetSdpStringWithStream1());
  2114. CreateAnswerAsLocalDescription();
  2115. // At this point we should be receiving stream 1, but not sending anything.
  2116. // A new offer would be recvonly, but we'll set the "no receive" constraints
  2117. // to make it inactive.
  2118. std::unique_ptr<SessionDescriptionInterface> offer;
  2119. RTCOfferAnswerOptions options;
  2120. options.offer_to_receive_audio = 0;
  2121. options.offer_to_receive_video = 0;
  2122. DoCreateOffer(&offer, &options);
  2123. const cricket::ContentInfo* video_content =
  2124. cricket::GetFirstVideoContent(offer->description());
  2125. ASSERT_EQ(RtpTransceiverDirection::kInactive,
  2126. video_content->media_description()->direction());
  2127. const cricket::ContentInfo* audio_content =
  2128. cricket::GetFirstAudioContent(offer->description());
  2129. ASSERT_EQ(RtpTransceiverDirection::kInactive,
  2130. audio_content->media_description()->direction());
  2131. }
  2132. // Test that we can use SetConfiguration to change the ICE servers of the
  2133. // PortAllocator.
  2134. TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
  2135. CreatePeerConnection();
  2136. PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
  2137. PeerConnectionInterface::IceServer server;
  2138. server.uri = "stun:test_hostname";
  2139. config.servers.push_back(server);
  2140. EXPECT_TRUE(pc_->SetConfiguration(config).ok());
  2141. EXPECT_EQ(1u, port_allocator_->stun_servers().size());
  2142. EXPECT_EQ("test_hostname",
  2143. port_allocator_->stun_servers().begin()->hostname());
  2144. }
  2145. TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
  2146. CreatePeerConnection();
  2147. PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
  2148. config.type = PeerConnectionInterface::kRelay;
  2149. EXPECT_TRUE(pc_->SetConfiguration(config).ok());
  2150. EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
  2151. }
  2152. TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesPruneTurnPortsFlag) {
  2153. PeerConnectionInterface::RTCConfiguration config;
  2154. config.prune_turn_ports = false;
  2155. CreatePeerConnection(config);
  2156. config = pc_->GetConfiguration();
  2157. EXPECT_EQ(webrtc::NO_PRUNE, port_allocator_->turn_port_prune_policy());
  2158. config.prune_turn_ports = true;
  2159. EXPECT_TRUE(pc_->SetConfiguration(config).ok());
  2160. EXPECT_EQ(webrtc::PRUNE_BASED_ON_PRIORITY,
  2161. port_allocator_->turn_port_prune_policy());
  2162. }
  2163. // Test that the ice check interval can be changed. This does not verify that
  2164. // the setting makes it all the way to P2PTransportChannel, as that would
  2165. // require a very complex set of mocks.
  2166. TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesIceCheckInterval) {
  2167. PeerConnectionInterface::RTCConfiguration config;
  2168. config.ice_check_min_interval = absl::nullopt;
  2169. CreatePeerConnection(config);
  2170. config = pc_->GetConfiguration();
  2171. config.ice_check_min_interval = 100;
  2172. EXPECT_TRUE(pc_->SetConfiguration(config).ok());
  2173. config = pc_->GetConfiguration();
  2174. EXPECT_EQ(config.ice_check_min_interval, 100);
  2175. }
  2176. TEST_P(PeerConnectionInterfaceTest,
  2177. SetConfigurationChangesSurfaceIceCandidatesOnIceTransportTypeChanged) {
  2178. PeerConnectionInterface::RTCConfiguration config;
  2179. config.surface_ice_candidates_on_ice_transport_type_changed = false;
  2180. CreatePeerConnection(config);
  2181. config = pc_->GetConfiguration();
  2182. EXPECT_FALSE(config.surface_ice_candidates_on_ice_transport_type_changed);
  2183. config.surface_ice_candidates_on_ice_transport_type_changed = true;
  2184. EXPECT_TRUE(pc_->SetConfiguration(config).ok());
  2185. config = pc_->GetConfiguration();
  2186. EXPECT_TRUE(config.surface_ice_candidates_on_ice_transport_type_changed);
  2187. }
  2188. // Test that when SetConfiguration changes both the pool size and other
  2189. // attributes, the pooled session is created with the updated attributes.
  2190. TEST_P(PeerConnectionInterfaceTest,
  2191. SetConfigurationCreatesPooledSessionCorrectly) {
  2192. CreatePeerConnection();
  2193. PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
  2194. config.ice_candidate_pool_size = 1;
  2195. PeerConnectionInterface::IceServer server;
  2196. server.uri = kStunAddressOnly;
  2197. config.servers.push_back(server);
  2198. config.type = PeerConnectionInterface::kRelay;
  2199. EXPECT_TRUE(pc_->SetConfiguration(config).ok());
  2200. const cricket::FakePortAllocatorSession* session =
  2201. static_cast<const cricket::FakePortAllocatorSession*>(
  2202. port_allocator_->GetPooledSession());
  2203. ASSERT_NE(nullptr, session);
  2204. EXPECT_EQ(1UL, session->stun_servers().size());
  2205. }
  2206. // Test that after SetLocalDescription, changing the pool size is not allowed,
  2207. // and an invalid modification error is returned.
  2208. TEST_P(PeerConnectionInterfaceTest,
  2209. CantChangePoolSizeAfterSetLocalDescription) {
  2210. CreatePeerConnection();
  2211. // Start by setting a size of 1.
  2212. PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
  2213. config.ice_candidate_pool_size = 1;
  2214. EXPECT_TRUE(pc_->SetConfiguration(config).ok());
  2215. // Set remote offer; can still change pool size at this point.
  2216. CreateOfferAsRemoteDescription();
  2217. config.ice_candidate_pool_size = 2;
  2218. EXPECT_TRUE(pc_->SetConfiguration(config).ok());
  2219. // Set local answer; now it's too late.
  2220. CreateAnswerAsLocalDescription();
  2221. config.ice_candidate_pool_size = 3;
  2222. RTCError error = pc_->SetConfiguration(config);
  2223. EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
  2224. }
  2225. // Test that after setting an answer, extra pooled sessions are discarded. The
  2226. // ICE candidate pool is only intended to be used for the first offer/answer.
  2227. TEST_P(PeerConnectionInterfaceTest,
  2228. ExtraPooledSessionsDiscardedAfterApplyingAnswer) {
  2229. CreatePeerConnection();
  2230. // Set a larger-than-necessary size.
  2231. PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
  2232. config.ice_candidate_pool_size = 4;
  2233. EXPECT_TRUE(pc_->SetConfiguration(config).ok());
  2234. // Do offer/answer.
  2235. CreateOfferAsRemoteDescription();
  2236. CreateAnswerAsLocalDescription();
  2237. // Expect no pooled sessions to be left.
  2238. const cricket::PortAllocatorSession* session =
  2239. port_allocator_->GetPooledSession();
  2240. EXPECT_EQ(nullptr, session);
  2241. }
  2242. // After Close is called, pooled candidates should be discarded so as to not
  2243. // waste network resources.
  2244. TEST_P(PeerConnectionInterfaceTest, PooledSessionsDiscardedAfterClose) {
  2245. CreatePeerConnection();
  2246. PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
  2247. config.ice_candidate_pool_size = 3;
  2248. EXPECT_TRUE(pc_->SetConfiguration(config).ok());
  2249. pc_->Close();
  2250. // Expect no pooled sessions to be left.
  2251. const cricket::PortAllocatorSession* session =
  2252. port_allocator_->GetPooledSession();
  2253. EXPECT_EQ(nullptr, session);
  2254. }
  2255. // Test that SetConfiguration returns an invalid modification error if
  2256. // modifying a field in the configuration that isn't allowed to be modified.
  2257. TEST_P(PeerConnectionInterfaceTest,
  2258. SetConfigurationReturnsInvalidModificationError) {
  2259. PeerConnectionInterface::RTCConfiguration config;
  2260. config.bundle_policy = PeerConnectionInterface::kBundlePolicyBalanced;
  2261. config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
  2262. config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE;
  2263. CreatePeerConnection(config);
  2264. PeerConnectionInterface::RTCConfiguration modified_config =
  2265. pc_->GetConfiguration();
  2266. modified_config.bundle_policy =
  2267. PeerConnectionInterface::kBundlePolicyMaxBundle;
  2268. RTCError error = pc_->SetConfiguration(modified_config);
  2269. EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
  2270. modified_config = pc_->GetConfiguration();
  2271. modified_config.rtcp_mux_policy =
  2272. PeerConnectionInterface::kRtcpMuxPolicyRequire;
  2273. error = pc_->SetConfiguration(modified_config);
  2274. EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
  2275. modified_config = pc_->GetConfiguration();
  2276. modified_config.continual_gathering_policy =
  2277. PeerConnectionInterface::GATHER_CONTINUALLY;
  2278. error = pc_->SetConfiguration(modified_config);
  2279. EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
  2280. }
  2281. // Test that SetConfiguration returns a range error if the candidate pool size
  2282. // is negative or larger than allowed by the spec.
  2283. TEST_P(PeerConnectionInterfaceTest,
  2284. SetConfigurationReturnsRangeErrorForBadCandidatePoolSize) {
  2285. PeerConnectionInterface::RTCConfiguration config;
  2286. CreatePeerConnection(config);
  2287. config = pc_->GetConfiguration();
  2288. config.ice_candidate_pool_size = -1;
  2289. RTCError error = pc_->SetConfiguration(config);
  2290. EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
  2291. config.ice_candidate_pool_size = INT_MAX;
  2292. error = pc_->SetConfiguration(config);
  2293. EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
  2294. }
  2295. // Test that SetConfiguration returns a syntax error if parsing an ICE server
  2296. // URL failed.
  2297. TEST_P(PeerConnectionInterfaceTest,
  2298. SetConfigurationReturnsSyntaxErrorFromBadIceUrls) {
  2299. PeerConnectionInterface::RTCConfiguration config;
  2300. CreatePeerConnection(config);
  2301. config = pc_->GetConfiguration();
  2302. PeerConnectionInterface::IceServer bad_server;
  2303. bad_server.uri = "stunn:www.example.com";
  2304. config.servers.push_back(bad_server);
  2305. RTCError error = pc_->SetConfiguration(config);
  2306. EXPECT_EQ(RTCErrorType::SYNTAX_ERROR, error.type());
  2307. }
  2308. // Test that SetConfiguration returns an invalid parameter error if a TURN
  2309. // IceServer is missing a username or password.
  2310. TEST_P(PeerConnectionInterfaceTest,
  2311. SetConfigurationReturnsInvalidParameterIfCredentialsMissing) {
  2312. PeerConnectionInterface::RTCConfiguration config;
  2313. CreatePeerConnection(config);
  2314. config = pc_->GetConfiguration();
  2315. PeerConnectionInterface::IceServer bad_server;
  2316. bad_server.uri = "turn:www.example.com";
  2317. // Missing password.
  2318. bad_server.username = "foo";
  2319. config.servers.push_back(bad_server);
  2320. RTCError error;
  2321. EXPECT_EQ(pc_->SetConfiguration(config).type(),
  2322. RTCErrorType::INVALID_PARAMETER);
  2323. }
  2324. // Test that PeerConnection::Close changes the states to closed and all remote
  2325. // tracks change state to ended.
  2326. TEST_P(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
  2327. // Initialize a PeerConnection and negotiate local and remote session
  2328. // description.
  2329. InitiateCall();
  2330. // With Plan B, verify the stream count. The analog with Unified Plan is the
  2331. // RtpTransceiver count.
  2332. if (sdp_semantics_ == SdpSemantics::kPlanB) {
  2333. ASSERT_EQ(1u, pc_->local_streams()->count());
  2334. ASSERT_EQ(1u, pc_->remote_streams()->count());
  2335. } else {
  2336. ASSERT_EQ(2u, pc_->GetTransceivers().size());
  2337. }
  2338. pc_->Close();
  2339. EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
  2340. EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
  2341. pc_->ice_connection_state());
  2342. EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
  2343. pc_->ice_gathering_state());
  2344. if (sdp_semantics_ == SdpSemantics::kPlanB) {
  2345. EXPECT_EQ(1u, pc_->local_streams()->count());
  2346. EXPECT_EQ(1u, pc_->remote_streams()->count());
  2347. } else {
  2348. // Verify that the RtpTransceivers are still present but all stopped.
  2349. EXPECT_EQ(2u, pc_->GetTransceivers().size());
  2350. for (const auto& transceiver : pc_->GetTransceivers()) {
  2351. EXPECT_TRUE(transceiver->stopped());
  2352. }
  2353. }
  2354. auto audio_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_AUDIO);
  2355. ASSERT_TRUE(audio_receiver);
  2356. auto video_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_VIDEO);
  2357. ASSERT_TRUE(video_receiver);
  2358. // Track state may be updated asynchronously.
  2359. EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
  2360. audio_receiver->track()->state(), kTimeout);
  2361. EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
  2362. video_receiver->track()->state(), kTimeout);
  2363. }
  2364. // Test that PeerConnection methods fails gracefully after
  2365. // PeerConnection::Close has been called.
  2366. // Don't run under Unified Plan since the stream API is not available.
  2367. TEST_F(PeerConnectionInterfaceTestPlanB, CloseAndTestMethods) {
  2368. CreatePeerConnectionWithoutDtls();
  2369. AddAudioVideoStream(kStreamId1, "audio_label", "video_label");
  2370. CreateOfferAsRemoteDescription();
  2371. CreateAnswerAsLocalDescription();
  2372. ASSERT_EQ(1u, pc_->local_streams()->count());
  2373. rtc::scoped_refptr<MediaStreamInterface> local_stream =
  2374. pc_->local_streams()->at(0);
  2375. pc_->Close();
  2376. pc_->RemoveStream(local_stream);
  2377. EXPECT_FALSE(pc_->AddStream(local_stream));
  2378. EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
  2379. EXPECT_TRUE(pc_->local_description() != NULL);
  2380. EXPECT_TRUE(pc_->remote_description() != NULL);
  2381. std::unique_ptr<SessionDescriptionInterface> offer;
  2382. EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
  2383. std::unique_ptr<SessionDescriptionInterface> answer;
  2384. EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
  2385. std::string sdp;
  2386. ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
  2387. std::unique_ptr<SessionDescriptionInterface> remote_offer(
  2388. webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
  2389. EXPECT_FALSE(DoSetRemoteDescription(std::move(remote_offer)));
  2390. ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
  2391. std::unique_ptr<SessionDescriptionInterface> local_offer(
  2392. webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
  2393. EXPECT_FALSE(DoSetLocalDescription(std::move(local_offer)));
  2394. }
  2395. // Test that GetStats can still be called after PeerConnection::Close.
  2396. TEST_P(PeerConnectionInterfaceTest, CloseAndGetStats) {
  2397. InitiateCall();
  2398. pc_->Close();
  2399. DoGetStats(NULL);
  2400. }
  2401. // NOTE: The series of tests below come from what used to be
  2402. // mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
  2403. // setting a remote or local description has the expected effects.
  2404. // This test verifies that the remote MediaStreams corresponding to a received
  2405. // SDP string is created. In this test the two separate MediaStreams are
  2406. // signaled.
  2407. TEST_P(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
  2408. RTCConfiguration config;
  2409. config.enable_dtls_srtp = true;
  2410. CreatePeerConnection(config);
  2411. CreateAndSetRemoteOffer(GetSdpStringWithStream1());
  2412. rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
  2413. EXPECT_TRUE(
  2414. CompareStreamCollections(observer_.remote_streams(), reference.get()));
  2415. MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
  2416. EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
  2417. // Create a session description based on another SDP with another
  2418. // MediaStream.
  2419. CreateAndSetRemoteOffer(GetSdpStringWithStream1And2());
  2420. rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
  2421. EXPECT_TRUE(
  2422. CompareStreamCollections(observer_.remote_streams(), reference2.get()));
  2423. }
  2424. // This test verifies that when remote tracks are added/removed from SDP, the
  2425. // created remote streams are updated appropriately.
  2426. // Don't run under Unified Plan since this test uses Plan B SDP to test Plan B
  2427. // specific behavior.
  2428. TEST_F(PeerConnectionInterfaceTestPlanB,
  2429. AddRemoveTrackFromExistingRemoteMediaStream) {
  2430. RTCConfiguration config;
  2431. config.enable_dtls_srtp = true;
  2432. CreatePeerConnection(config);
  2433. std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
  2434. CreateSessionDescriptionAndReference(1, 1);
  2435. EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1)));
  2436. EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
  2437. reference_collection_));
  2438. // Add extra audio and video tracks to the same MediaStream.
  2439. std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
  2440. CreateSessionDescriptionAndReference(2, 2);
  2441. EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1_two_tracks)));
  2442. EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
  2443. reference_collection_));
  2444. rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
  2445. observer_.remote_streams()->at(0)->GetAudioTracks()[1];
  2446. EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
  2447. rtc::scoped_refptr<VideoTrackInterface> video_track2 =
  2448. observer_.remote_streams()->at(0)->GetVideoTracks()[1];
  2449. EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
  2450. // Remove the extra audio and video tracks.
  2451. std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
  2452. CreateSessionDescriptionAndReference(1, 1);
  2453. MockTrackObserver audio_track_observer(audio_track2);
  2454. MockTrackObserver video_track_observer(video_track2);
  2455. EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
  2456. EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
  2457. EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms2)));
  2458. EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
  2459. reference_collection_));
  2460. // Track state may be updated asynchronously.
  2461. EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
  2462. audio_track2->state(), kTimeout);
  2463. EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
  2464. video_track2->state(), kTimeout);
  2465. }
  2466. // This tests that remote tracks are ended if a local session description is set
  2467. // that rejects the media content type.
  2468. TEST_P(PeerConnectionInterfaceTest, RejectMediaContent) {
  2469. RTCConfiguration config;
  2470. config.enable_dtls_srtp = true;
  2471. CreatePeerConnection(config);
  2472. // First create and set a remote offer, then reject its video content in our
  2473. // answer.
  2474. CreateAndSetRemoteOffer(kSdpStringWithStream1PlanB);
  2475. auto audio_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_AUDIO);
  2476. ASSERT_TRUE(audio_receiver);
  2477. auto video_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_VIDEO);
  2478. ASSERT_TRUE(video_receiver);
  2479. rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
  2480. audio_receiver->track();
  2481. EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
  2482. rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
  2483. video_receiver->track();
  2484. EXPECT_EQ(MediaStreamTrackInterface::kLive, remote_video->state());
  2485. std::unique_ptr<SessionDescriptionInterface> local_answer;
  2486. EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
  2487. cricket::ContentInfo* video_info =
  2488. local_answer->description()->GetContentByName("video");
  2489. video_info->rejected = true;
  2490. EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
  2491. EXPECT_EQ(MediaStreamTrackInterface::kEnded, remote_video->state());
  2492. EXPECT_EQ(MediaStreamTrackInterface::kLive, remote_audio->state());
  2493. // Now create an offer where we reject both video and audio.
  2494. std::unique_ptr<SessionDescriptionInterface> local_offer;
  2495. EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
  2496. video_info = local_offer->description()->GetContentByName("video");
  2497. ASSERT_TRUE(video_info != nullptr);
  2498. video_info->rejected = true;
  2499. cricket::ContentInfo* audio_info =
  2500. local_offer->description()->GetContentByName("audio");
  2501. ASSERT_TRUE(audio_info != nullptr);
  2502. audio_info->rejected = true;
  2503. EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
  2504. // Track state may be updated asynchronously.
  2505. EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded, remote_audio->state(),
  2506. kTimeout);
  2507. EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded, remote_video->state(),
  2508. kTimeout);
  2509. }
  2510. // This tests that we won't crash if the remote track has been removed outside
  2511. // of PeerConnection and then PeerConnection tries to reject the track.
  2512. // Don't run under Unified Plan since the stream API is not available.
  2513. TEST_F(PeerConnectionInterfaceTestPlanB, RemoveTrackThenRejectMediaContent) {
  2514. RTCConfiguration config;
  2515. config.enable_dtls_srtp = true;
  2516. CreatePeerConnection(config);
  2517. CreateAndSetRemoteOffer(GetSdpStringWithStream1());
  2518. MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
  2519. remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
  2520. remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
  2521. std::unique_ptr<SessionDescriptionInterface> local_answer(
  2522. webrtc::CreateSessionDescription(SdpType::kAnswer,
  2523. GetSdpStringWithStream1(), nullptr));
  2524. cricket::ContentInfo* video_info =
  2525. local_answer->description()->GetContentByName("video");
  2526. video_info->rejected = true;
  2527. cricket::ContentInfo* audio_info =
  2528. local_answer->description()->GetContentByName("audio");
  2529. audio_info->rejected = true;
  2530. EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
  2531. // No crash is a pass.
  2532. }
  2533. // This tests that if a recvonly remote description is set, no remote streams
  2534. // will be created, even if the description contains SSRCs/MSIDs.
  2535. // See: https://code.google.com/p/webrtc/issues/detail?id=5054
  2536. TEST_P(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
  2537. RTCConfiguration config;
  2538. config.enable_dtls_srtp = true;
  2539. CreatePeerConnection(config);
  2540. std::string recvonly_offer = GetSdpStringWithStream1();
  2541. absl::StrReplaceAll({{kSendrecv, kRecvonly}}, &recvonly_offer);
  2542. CreateAndSetRemoteOffer(recvonly_offer);
  2543. EXPECT_EQ(0u, observer_.remote_streams()->count());
  2544. }
  2545. // This tests that a default MediaStream is created if a remote session
  2546. // description doesn't contain any streams and no MSID support.
  2547. // It also tests that the default stream is updated if a video m-line is added
  2548. // in a subsequent session description.
  2549. // Don't run under Unified Plan since this behavior is Plan B specific.
  2550. TEST_F(PeerConnectionInterfaceTestPlanB, SdpWithoutMsidCreatesDefaultStream) {
  2551. RTCConfiguration config;
  2552. config.enable_dtls_srtp = true;
  2553. CreatePeerConnection(config);
  2554. CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
  2555. ASSERT_EQ(1u, observer_.remote_streams()->count());
  2556. MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
  2557. EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
  2558. EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
  2559. EXPECT_EQ("default", remote_stream->id());
  2560. CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
  2561. ASSERT_EQ(1u, observer_.remote_streams()->count());
  2562. ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
  2563. EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
  2564. EXPECT_EQ(MediaStreamTrackInterface::kLive,
  2565. remote_stream->GetAudioTracks()[0]->state());
  2566. ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
  2567. EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
  2568. EXPECT_EQ(MediaStreamTrackInterface::kLive,
  2569. remote_stream->GetVideoTracks()[0]->state());
  2570. }
  2571. // This tests that a default MediaStream is created if a remote session
  2572. // description doesn't contain any streams and media direction is send only.
  2573. // Don't run under Unified Plan since this behavior is Plan B specific.
  2574. TEST_F(PeerConnectionInterfaceTestPlanB,
  2575. SendOnlySdpWithoutMsidCreatesDefaultStream) {
  2576. RTCConfiguration config;
  2577. config.enable_dtls_srtp = true;
  2578. CreatePeerConnection(config);
  2579. CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
  2580. ASSERT_EQ(1u, observer_.remote_streams()->count());
  2581. MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
  2582. EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
  2583. EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
  2584. EXPECT_EQ("default", remote_stream->id());
  2585. }
  2586. // This tests that it won't crash when PeerConnection tries to remove
  2587. // a remote track that as already been removed from the MediaStream.
  2588. // Don't run under Unified Plan since this behavior is Plan B specific.
  2589. TEST_F(PeerConnectionInterfaceTestPlanB, RemoveAlreadyGoneRemoteStream) {
  2590. RTCConfiguration config;
  2591. config.enable_dtls_srtp = true;
  2592. CreatePeerConnection(config);
  2593. CreateAndSetRemoteOffer(GetSdpStringWithStream1());
  2594. MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
  2595. remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
  2596. remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
  2597. CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
  2598. // No crash is a pass.
  2599. }
  2600. // This tests that a default MediaStream is created if the remote session
  2601. // description doesn't contain any streams and don't contain an indication if
  2602. // MSID is supported.
  2603. // Don't run under Unified Plan since this behavior is Plan B specific.
  2604. TEST_F(PeerConnectionInterfaceTestPlanB,
  2605. SdpWithoutMsidAndStreamsCreatesDefaultStream) {
  2606. RTCConfiguration config;
  2607. config.enable_dtls_srtp = true;
  2608. CreatePeerConnection(config);
  2609. CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
  2610. ASSERT_EQ(1u, observer_.remote_streams()->count());
  2611. MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
  2612. EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
  2613. EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
  2614. }
  2615. // This tests that a default MediaStream is not created if the remote session
  2616. // description doesn't contain any streams but does support MSID.
  2617. // Don't run under Unified Plan since this behavior is Plan B specific.
  2618. TEST_F(PeerConnectionInterfaceTestPlanB, SdpWithMsidDontCreatesDefaultStream) {
  2619. RTCConfiguration config;
  2620. config.enable_dtls_srtp = true;
  2621. CreatePeerConnection(config);
  2622. CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
  2623. EXPECT_EQ(0u, observer_.remote_streams()->count());
  2624. }
  2625. // This tests that when setting a new description, the old default tracks are
  2626. // not destroyed and recreated.
  2627. // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
  2628. // Don't run under Unified Plan since this behavior is Plan B specific.
  2629. TEST_F(PeerConnectionInterfaceTestPlanB,
  2630. DefaultTracksNotDestroyedAndRecreated) {
  2631. RTCConfiguration config;
  2632. config.enable_dtls_srtp = true;
  2633. CreatePeerConnection(config);
  2634. CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
  2635. ASSERT_EQ(1u, observer_.remote_streams()->count());
  2636. MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
  2637. ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
  2638. // Set the track to "disabled", then set a new description and ensure the
  2639. // track is still disabled, which ensures it hasn't been recreated.
  2640. remote_stream->GetAudioTracks()[0]->set_enabled(false);
  2641. CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
  2642. ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
  2643. EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
  2644. }
  2645. // This tests that a default MediaStream is not created if a remote session
  2646. // description is updated to not have any MediaStreams.
  2647. // Don't run under Unified Plan since this behavior is Plan B specific.
  2648. TEST_F(PeerConnectionInterfaceTestPlanB, VerifyDefaultStreamIsNotCreated) {
  2649. RTCConfiguration config;
  2650. config.enable_dtls_srtp = true;
  2651. CreatePeerConnection(config);
  2652. CreateAndSetRemoteOffer(GetSdpStringWithStream1());
  2653. rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
  2654. EXPECT_TRUE(
  2655. CompareStreamCollections(observer_.remote_streams(), reference.get()));
  2656. CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
  2657. EXPECT_EQ(0u, observer_.remote_streams()->count());
  2658. }
  2659. // This tests that a default MediaStream is created if a remote SDP comes from
  2660. // an endpoint that doesn't signal SSRCs, but signals media stream IDs.
  2661. TEST_F(PeerConnectionInterfaceTestPlanB,
  2662. SdpWithMsidWithoutSsrcCreatesDefaultStream) {
  2663. RTCConfiguration config;
  2664. config.enable_dtls_srtp = true;
  2665. CreatePeerConnection(config);
  2666. std::string sdp_string = kSdpStringWithoutStreamsAudioOnly;
  2667. // Add a=msid lines to simulate a Unified Plan endpoint that only
  2668. // signals stream IDs with a=msid lines.
  2669. sdp_string.append("a=msid:audio_stream_id audio_track_id\n");
  2670. CreateAndSetRemoteOffer(sdp_string);
  2671. ASSERT_EQ(1u, observer_.remote_streams()->count());
  2672. MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
  2673. EXPECT_EQ("default", remote_stream->id());
  2674. ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
  2675. }
  2676. // This tests that when a Plan B endpoint receives an SDP that signals no media
  2677. // stream IDs indicated by the special character "-" in the a=msid line, that
  2678. // a default stream ID will be used for the MediaStream ID. This can occur
  2679. // when a Unified Plan endpoint signals no media stream IDs, but signals both
  2680. // a=ssrc msid and a=msid lines for interop signaling with Plan B.
  2681. TEST_F(PeerConnectionInterfaceTestPlanB,
  2682. SdpWithEmptyMsidAndSsrcCreatesDefaultStreamId) {
  2683. RTCConfiguration config;
  2684. config.enable_dtls_srtp = true;
  2685. CreatePeerConnection(config);
  2686. // Add a a=msid line to the SDP. This is prioritized when parsing the SDP, so
  2687. // the sender's stream ID will be interpreted as no stream IDs.
  2688. std::string sdp_string = kSdpStringWithStream1AudioTrackOnly;
  2689. sdp_string.append("a=msid:- audiotrack0\n");
  2690. CreateAndSetRemoteOffer(sdp_string);
  2691. ASSERT_EQ(1u, observer_.remote_streams()->count());
  2692. // Because SSRCs are signaled the track ID will be what was signaled in the
  2693. // a=msid line.
  2694. EXPECT_EQ("audiotrack0", observer_.last_added_track_label_);
  2695. MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
  2696. EXPECT_EQ("default", remote_stream->id());
  2697. ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
  2698. // Previously a bug ocurred when setting the remote description a second time.
  2699. // This is because we checked equality of the remote StreamParams stream ID
  2700. // (empty), and the previously set stream ID for the remote sender
  2701. // ("default"). This cause a track to be removed, then added, when really
  2702. // nothing should occur because it is the same track.
  2703. CreateAndSetRemoteOffer(sdp_string);
  2704. EXPECT_EQ(0u, observer_.remove_track_events_.size());
  2705. EXPECT_EQ(1u, observer_.add_track_events_.size());
  2706. EXPECT_EQ("audiotrack0", observer_.last_added_track_label_);
  2707. remote_stream = observer_.remote_streams()->at(0);
  2708. EXPECT_EQ("default", remote_stream->id());
  2709. ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
  2710. }
  2711. // This tests that an RtpSender is created when the local description is set
  2712. // after adding a local stream.
  2713. // TODO(deadbeef): This test and the one below it need to be updated when
  2714. // an RtpSender's lifetime isn't determined by when a local description is set.
  2715. // Don't run under Unified Plan since this behavior is Plan B specific.
  2716. TEST_F(PeerConnectionInterfaceTestPlanB, LocalDescriptionChanged) {
  2717. RTCConfiguration config;
  2718. config.enable_dtls_srtp = true;
  2719. CreatePeerConnection(config);
  2720. // Create an offer with 1 stream with 2 tracks of each type.
  2721. rtc::scoped_refptr<StreamCollection> stream_collection =
  2722. CreateStreamCollection(1, 2);
  2723. pc_->AddStream(stream_collection->at(0));
  2724. std::unique_ptr<SessionDescriptionInterface> offer;
  2725. ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
  2726. EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
  2727. auto senders = pc_->GetSenders();
  2728. EXPECT_EQ(4u, senders.size());
  2729. EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
  2730. EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
  2731. EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
  2732. EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
  2733. // Remove an audio and video track.
  2734. pc_->RemoveStream(stream_collection->at(0));
  2735. stream_collection = CreateStreamCollection(1, 1);
  2736. pc_->AddStream(stream_collection->at(0));
  2737. ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
  2738. EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
  2739. senders = pc_->GetSenders();
  2740. EXPECT_EQ(2u, senders.size());
  2741. EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
  2742. EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
  2743. EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
  2744. EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
  2745. }
  2746. // This tests that an RtpSender is created when the local description is set
  2747. // before adding a local stream.
  2748. // Don't run under Unified Plan since this behavior is Plan B specific.
  2749. TEST_F(PeerConnectionInterfaceTestPlanB,
  2750. AddLocalStreamAfterLocalDescriptionChanged) {
  2751. RTCConfiguration config;
  2752. config.enable_dtls_srtp = true;
  2753. CreatePeerConnection(config);
  2754. rtc::scoped_refptr<StreamCollection> stream_collection =
  2755. CreateStreamCollection(1, 2);
  2756. // Add a stream to create the offer, but remove it afterwards.
  2757. pc_->AddStream(stream_collection->at(0));
  2758. std::unique_ptr<SessionDescriptionInterface> offer;
  2759. ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
  2760. pc_->RemoveStream(stream_collection->at(0));
  2761. EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
  2762. auto senders = pc_->GetSenders();
  2763. EXPECT_EQ(0u, senders.size());
  2764. pc_->AddStream(stream_collection->at(0));
  2765. senders = pc_->GetSenders();
  2766. EXPECT_EQ(4u, senders.size());
  2767. EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
  2768. EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
  2769. EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
  2770. EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
  2771. }
  2772. // This tests that the expected behavior occurs if the SSRC on a local track is
  2773. // changed when SetLocalDescription is called.
  2774. TEST_P(PeerConnectionInterfaceTest,
  2775. ChangeSsrcOnTrackInLocalSessionDescription) {
  2776. RTCConfiguration config;
  2777. config.enable_dtls_srtp = true;
  2778. CreatePeerConnection(config);
  2779. AddAudioTrack(kAudioTracks[0]);
  2780. AddVideoTrack(kVideoTracks[0]);
  2781. std::unique_ptr<SessionDescriptionInterface> offer;
  2782. ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
  2783. // Grab a copy of the offer before it gets passed into the PC.
  2784. std::unique_ptr<SessionDescriptionInterface> modified_offer =
  2785. webrtc::CreateSessionDescription(
  2786. webrtc::SdpType::kOffer, offer->session_id(),
  2787. offer->session_version(), offer->description()->Clone());
  2788. EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
  2789. auto senders = pc_->GetSenders();
  2790. EXPECT_EQ(2u, senders.size());
  2791. EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
  2792. EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
  2793. // Change the ssrc of the audio and video track.
  2794. cricket::MediaContentDescription* desc =
  2795. cricket::GetFirstAudioContentDescription(modified_offer->description());
  2796. ASSERT_TRUE(desc != NULL);
  2797. for (StreamParams& stream : desc->mutable_streams()) {
  2798. for (unsigned int& ssrc : stream.ssrcs) {
  2799. ++ssrc;
  2800. }
  2801. }
  2802. desc =
  2803. cricket::GetFirstVideoContentDescription(modified_offer->description());
  2804. ASSERT_TRUE(desc != NULL);
  2805. for (StreamParams& stream : desc->mutable_streams()) {
  2806. for (unsigned int& ssrc : stream.ssrcs) {
  2807. ++ssrc;
  2808. }
  2809. }
  2810. EXPECT_TRUE(DoSetLocalDescription(std::move(modified_offer)));
  2811. senders = pc_->GetSenders();
  2812. EXPECT_EQ(2u, senders.size());
  2813. EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
  2814. EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
  2815. // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
  2816. // changed.
  2817. }
  2818. // This tests that the expected behavior occurs if a new session description is
  2819. // set with the same tracks, but on a different MediaStream.
  2820. // Don't run under Unified Plan since the stream API is not available.
  2821. TEST_F(PeerConnectionInterfaceTestPlanB,
  2822. SignalSameTracksInSeparateMediaStream) {
  2823. RTCConfiguration config;
  2824. config.enable_dtls_srtp = true;
  2825. CreatePeerConnection(config);
  2826. rtc::scoped_refptr<StreamCollection> stream_collection =
  2827. CreateStreamCollection(2, 1);
  2828. pc_->AddStream(stream_collection->at(0));
  2829. std::unique_ptr<SessionDescriptionInterface> offer;
  2830. ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
  2831. EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
  2832. auto senders = pc_->GetSenders();
  2833. EXPECT_EQ(2u, senders.size());
  2834. EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
  2835. EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
  2836. // Add a new MediaStream but with the same tracks as in the first stream.
  2837. rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
  2838. webrtc::MediaStream::Create(kStreams[1]));
  2839. stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
  2840. stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
  2841. pc_->AddStream(stream_1);
  2842. ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
  2843. EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
  2844. auto new_senders = pc_->GetSenders();
  2845. // Should be the same senders as before, but with updated stream id.
  2846. // Note that this behavior is subject to change in the future.
  2847. // We may decide the PC should ignore existing tracks in AddStream.
  2848. EXPECT_EQ(senders, new_senders);
  2849. EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
  2850. EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
  2851. }
  2852. // This tests that PeerConnectionObserver::OnAddTrack is correctly called.
  2853. TEST_P(PeerConnectionInterfaceTest, OnAddTrackCallback) {
  2854. RTCConfiguration config;
  2855. config.enable_dtls_srtp = true;
  2856. CreatePeerConnection(config);
  2857. CreateAndSetRemoteOffer(kSdpStringWithStream1AudioTrackOnly);
  2858. EXPECT_EQ(observer_.num_added_tracks_, 1);
  2859. EXPECT_EQ(observer_.last_added_track_label_, kAudioTracks[0]);
  2860. // Create and set the updated remote SDP.
  2861. CreateAndSetRemoteOffer(kSdpStringWithStream1PlanB);
  2862. EXPECT_EQ(observer_.num_added_tracks_, 2);
  2863. EXPECT_EQ(observer_.last_added_track_label_, kVideoTracks[0]);
  2864. }
  2865. // Test that when SetConfiguration is called and the configuration is
  2866. // changing, the next offer causes an ICE restart.
  2867. TEST_P(PeerConnectionInterfaceTest, SetConfigurationCausingIceRestart) {
  2868. PeerConnectionInterface::RTCConfiguration config;
  2869. config.type = PeerConnectionInterface::kRelay;
  2870. CreatePeerConnection(config);
  2871. config = pc_->GetConfiguration();
  2872. AddAudioTrack(kAudioTracks[0], {kStreamId1});
  2873. AddVideoTrack(kVideoTracks[0], {kStreamId1});
  2874. // Do initial offer/answer so there's something to restart.
  2875. CreateOfferAsLocalDescription();
  2876. CreateAnswerAsRemoteDescription(GetSdpStringWithStream1());
  2877. // Grab the ufrags.
  2878. std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
  2879. // Change ICE policy, which should trigger an ICE restart on the next offer.
  2880. config.type = PeerConnectionInterface::kAll;
  2881. EXPECT_TRUE(pc_->SetConfiguration(config).ok());
  2882. CreateOfferAsLocalDescription();
  2883. // Grab the new ufrags.
  2884. std::vector<std::string> subsequent_ufrags =
  2885. GetUfrags(pc_->local_description());
  2886. // Sanity check.
  2887. EXPECT_EQ(initial_ufrags.size(), subsequent_ufrags.size());
  2888. // Check that each ufrag is different.
  2889. for (int i = 0; i < static_cast<int>(initial_ufrags.size()); ++i) {
  2890. EXPECT_NE(initial_ufrags[i], subsequent_ufrags[i]);
  2891. }
  2892. }
  2893. // Test that when SetConfiguration is called and the configuration *isn't*
  2894. // changing, the next offer does *not* cause an ICE restart.
  2895. TEST_P(PeerConnectionInterfaceTest, SetConfigurationNotCausingIceRestart) {
  2896. PeerConnectionInterface::RTCConfiguration config;
  2897. config.type = PeerConnectionInterface::kRelay;
  2898. CreatePeerConnection(config);
  2899. config = pc_->GetConfiguration();
  2900. AddAudioTrack(kAudioTracks[0]);
  2901. AddVideoTrack(kVideoTracks[0]);
  2902. // Do initial offer/answer so there's something to restart.
  2903. CreateOfferAsLocalDescription();
  2904. CreateAnswerAsRemoteDescription(GetSdpStringWithStream1());
  2905. // Grab the ufrags.
  2906. std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
  2907. // Call SetConfiguration with a config identical to what the PC was
  2908. // constructed with.
  2909. EXPECT_TRUE(pc_->SetConfiguration(config).ok());
  2910. CreateOfferAsLocalDescription();
  2911. // Grab the new ufrags.
  2912. std::vector<std::string> subsequent_ufrags =
  2913. GetUfrags(pc_->local_description());
  2914. EXPECT_EQ(initial_ufrags, subsequent_ufrags);
  2915. }
  2916. // Test for a weird corner case scenario:
  2917. // 1. Audio/video session established.
  2918. // 2. SetConfiguration changes ICE config; ICE restart needed.
  2919. // 3. ICE restart initiated by remote peer, but only for one m= section.
  2920. // 4. Next createOffer should initiate an ICE restart, but only for the other
  2921. // m= section; it would be pointless to do an ICE restart for the m= section
  2922. // that was already restarted.
  2923. TEST_P(PeerConnectionInterfaceTest, SetConfigurationCausingPartialIceRestart) {
  2924. PeerConnectionInterface::RTCConfiguration config;
  2925. config.type = PeerConnectionInterface::kRelay;
  2926. CreatePeerConnection(config);
  2927. config = pc_->GetConfiguration();
  2928. AddAudioTrack(kAudioTracks[0], {kStreamId1});
  2929. AddVideoTrack(kVideoTracks[0], {kStreamId1});
  2930. // Do initial offer/answer so there's something to restart.
  2931. CreateOfferAsLocalDescription();
  2932. CreateAnswerAsRemoteDescription(GetSdpStringWithStream1());
  2933. // Change ICE policy, which should set the "needs-ice-restart" flag.
  2934. config.type = PeerConnectionInterface::kAll;
  2935. EXPECT_TRUE(pc_->SetConfiguration(config).ok());
  2936. // Do ICE restart for the first m= section, initiated by remote peer.
  2937. std::unique_ptr<webrtc::SessionDescriptionInterface> remote_offer(
  2938. webrtc::CreateSessionDescription(SdpType::kOffer,
  2939. GetSdpStringWithStream1(), nullptr));
  2940. ASSERT_TRUE(remote_offer);
  2941. remote_offer->description()->transport_infos()[0].description.ice_ufrag =
  2942. "modified";
  2943. EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
  2944. CreateAnswerAsLocalDescription();
  2945. // Grab the ufrags.
  2946. std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
  2947. ASSERT_EQ(2U, initial_ufrags.size());
  2948. // Create offer and grab the new ufrags.
  2949. CreateOfferAsLocalDescription();
  2950. std::vector<std::string> subsequent_ufrags =
  2951. GetUfrags(pc_->local_description());
  2952. ASSERT_EQ(2U, subsequent_ufrags.size());
  2953. // Ensure that only the ufrag for the second m= section changed.
  2954. EXPECT_EQ(initial_ufrags[0], subsequent_ufrags[0]);
  2955. EXPECT_NE(initial_ufrags[1], subsequent_ufrags[1]);
  2956. }
  2957. // Tests that the methods to return current/pending descriptions work as
  2958. // expected at different points in the offer/answer exchange. This test does
  2959. // one offer/answer exchange as the offerer, then another as the answerer.
  2960. TEST_P(PeerConnectionInterfaceTest, CurrentAndPendingDescriptions) {
  2961. // This disables DTLS so we can apply an answer to ourselves.
  2962. CreatePeerConnection();
  2963. // Create initial local offer and get SDP (which will also be used as
  2964. // answer/pranswer);
  2965. std::unique_ptr<SessionDescriptionInterface> local_offer;
  2966. ASSERT_TRUE(DoCreateOffer(&local_offer, nullptr));
  2967. std::string sdp;
  2968. EXPECT_TRUE(local_offer->ToString(&sdp));
  2969. // Set local offer.
  2970. SessionDescriptionInterface* local_offer_ptr = local_offer.get();
  2971. EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
  2972. EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
  2973. EXPECT_EQ(nullptr, pc_->pending_remote_description());
  2974. EXPECT_EQ(nullptr, pc_->current_local_description());
  2975. EXPECT_EQ(nullptr, pc_->current_remote_description());
  2976. // Set remote pranswer.
  2977. std::unique_ptr<SessionDescriptionInterface> remote_pranswer(
  2978. webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
  2979. SessionDescriptionInterface* remote_pranswer_ptr = remote_pranswer.get();
  2980. EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_pranswer)));
  2981. EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
  2982. EXPECT_EQ(remote_pranswer_ptr, pc_->pending_remote_description());
  2983. EXPECT_EQ(nullptr, pc_->current_local_description());
  2984. EXPECT_EQ(nullptr, pc_->current_remote_description());
  2985. // Set remote answer.
  2986. std::unique_ptr<SessionDescriptionInterface> remote_answer(
  2987. webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
  2988. SessionDescriptionInterface* remote_answer_ptr = remote_answer.get();
  2989. EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_answer)));
  2990. EXPECT_EQ(nullptr, pc_->pending_local_description());
  2991. EXPECT_EQ(nullptr, pc_->pending_remote_description());
  2992. EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
  2993. EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
  2994. // Set remote offer.
  2995. std::unique_ptr<SessionDescriptionInterface> remote_offer(
  2996. webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
  2997. SessionDescriptionInterface* remote_offer_ptr = remote_offer.get();
  2998. EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
  2999. EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
  3000. EXPECT_EQ(nullptr, pc_->pending_local_description());
  3001. EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
  3002. EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
  3003. // Set local pranswer.
  3004. std::unique_ptr<SessionDescriptionInterface> local_pranswer(
  3005. webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
  3006. SessionDescriptionInterface* local_pranswer_ptr = local_pranswer.get();
  3007. EXPECT_TRUE(DoSetLocalDescription(std::move(local_pranswer)));
  3008. EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
  3009. EXPECT_EQ(local_pranswer_ptr, pc_->pending_local_description());
  3010. EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
  3011. EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
  3012. // Set local answer.
  3013. std::unique_ptr<SessionDescriptionInterface> local_answer(
  3014. webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
  3015. SessionDescriptionInterface* local_answer_ptr = local_answer.get();
  3016. EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
  3017. EXPECT_EQ(nullptr, pc_->pending_remote_description());
  3018. EXPECT_EQ(nullptr, pc_->pending_local_description());
  3019. EXPECT_EQ(remote_offer_ptr, pc_->current_remote_description());
  3020. EXPECT_EQ(local_answer_ptr, pc_->current_local_description());
  3021. }
  3022. // Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
  3023. // after the PeerConnection is closed.
  3024. // This version tests the StartRtcEventLog version that receives an object
  3025. // of type |RtcEventLogOutput|.
  3026. TEST_P(PeerConnectionInterfaceTest,
  3027. StartAndStopLoggingToOutputAfterPeerConnectionClosed) {
  3028. CreatePeerConnection();
  3029. // The RtcEventLog will be reset when the PeerConnection is closed.
  3030. pc_->Close();
  3031. EXPECT_FALSE(
  3032. pc_->StartRtcEventLog(std::make_unique<webrtc::RtcEventLogOutputNull>(),
  3033. webrtc::RtcEventLog::kImmediateOutput));
  3034. pc_->StopRtcEventLog();
  3035. }
  3036. // Test that generated offers/answers include "ice-option:trickle".
  3037. TEST_P(PeerConnectionInterfaceTest, OffersAndAnswersHaveTrickleIceOption) {
  3038. CreatePeerConnection();
  3039. // First, create an offer with audio/video.
  3040. RTCOfferAnswerOptions options;
  3041. options.offer_to_receive_audio = 1;
  3042. options.offer_to_receive_video = 1;
  3043. std::unique_ptr<SessionDescriptionInterface> offer;
  3044. ASSERT_TRUE(DoCreateOffer(&offer, &options));
  3045. cricket::SessionDescription* desc = offer->description();
  3046. ASSERT_EQ(2u, desc->transport_infos().size());
  3047. EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
  3048. EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
  3049. // Apply the offer as a remote description, then create an answer.
  3050. EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
  3051. std::unique_ptr<SessionDescriptionInterface> answer;
  3052. ASSERT_TRUE(DoCreateAnswer(&answer, &options));
  3053. desc = answer->description();
  3054. ASSERT_EQ(2u, desc->transport_infos().size());
  3055. EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
  3056. EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
  3057. }
  3058. // Test that ICE renomination isn't offered if it's not enabled in the PC's
  3059. // RTCConfiguration.
  3060. TEST_P(PeerConnectionInterfaceTest, IceRenominationNotOffered) {
  3061. PeerConnectionInterface::RTCConfiguration config;
  3062. config.enable_ice_renomination = false;
  3063. CreatePeerConnection(config);
  3064. AddAudioTrack("foo");
  3065. std::unique_ptr<SessionDescriptionInterface> offer;
  3066. ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
  3067. cricket::SessionDescription* desc = offer->description();
  3068. EXPECT_EQ(1u, desc->transport_infos().size());
  3069. EXPECT_FALSE(
  3070. desc->transport_infos()[0].description.GetIceParameters().renomination);
  3071. }
  3072. // Test that the ICE renomination option is present in generated offers/answers
  3073. // if it's enabled in the PC's RTCConfiguration.
  3074. TEST_P(PeerConnectionInterfaceTest, IceRenominationOptionInOfferAndAnswer) {
  3075. PeerConnectionInterface::RTCConfiguration config;
  3076. config.enable_ice_renomination = true;
  3077. CreatePeerConnection(config);
  3078. AddAudioTrack("foo");
  3079. std::unique_ptr<SessionDescriptionInterface> offer;
  3080. ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
  3081. cricket::SessionDescription* desc = offer->description();
  3082. EXPECT_EQ(1u, desc->transport_infos().size());
  3083. EXPECT_TRUE(
  3084. desc->transport_infos()[0].description.GetIceParameters().renomination);
  3085. // Set the offer as a remote description, then create an answer and ensure it
  3086. // has the renomination flag too.
  3087. EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
  3088. std::unique_ptr<SessionDescriptionInterface> answer;
  3089. ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
  3090. desc = answer->description();
  3091. EXPECT_EQ(1u, desc->transport_infos().size());
  3092. EXPECT_TRUE(
  3093. desc->transport_infos()[0].description.GetIceParameters().renomination);
  3094. }
  3095. // Test that if CreateOffer is called with the deprecated "offer to receive
  3096. // audio/video" constraints, they're processed and result in an offer with
  3097. // audio/video sections just as if RTCOfferAnswerOptions had been used.
  3098. TEST_P(PeerConnectionInterfaceTest, CreateOfferWithOfferToReceiveConstraints) {
  3099. CreatePeerConnection();
  3100. RTCOfferAnswerOptions options;
  3101. options.offer_to_receive_audio = 1;
  3102. options.offer_to_receive_video = 1;
  3103. std::unique_ptr<SessionDescriptionInterface> offer;
  3104. ASSERT_TRUE(DoCreateOffer(&offer, &options));
  3105. cricket::SessionDescription* desc = offer->description();
  3106. const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
  3107. const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
  3108. ASSERT_NE(nullptr, audio);
  3109. ASSERT_NE(nullptr, video);
  3110. EXPECT_FALSE(audio->rejected);
  3111. EXPECT_FALSE(video->rejected);
  3112. }
  3113. // Test that if CreateAnswer is called with the deprecated "offer to receive
  3114. // audio/video" constraints, they're processed and can be used to reject an
  3115. // offered m= section just as can be done with RTCOfferAnswerOptions;
  3116. // Don't run under Unified Plan since this behavior is not supported.
  3117. TEST_F(PeerConnectionInterfaceTestPlanB,
  3118. CreateAnswerWithOfferToReceiveConstraints) {
  3119. CreatePeerConnection();
  3120. // First, create an offer with audio/video and apply it as a remote
  3121. // description.
  3122. RTCOfferAnswerOptions options;
  3123. options.offer_to_receive_audio = 1;
  3124. options.offer_to_receive_video = 1;
  3125. std::unique_ptr<SessionDescriptionInterface> offer;
  3126. ASSERT_TRUE(DoCreateOffer(&offer, &options));
  3127. EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
  3128. // Now create answer that rejects audio/video.
  3129. options.offer_to_receive_audio = 0;
  3130. options.offer_to_receive_video = 0;
  3131. std::unique_ptr<SessionDescriptionInterface> answer;
  3132. ASSERT_TRUE(DoCreateAnswer(&answer, &options));
  3133. cricket::SessionDescription* desc = answer->description();
  3134. const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
  3135. const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
  3136. ASSERT_NE(nullptr, audio);
  3137. ASSERT_NE(nullptr, video);
  3138. EXPECT_TRUE(audio->rejected);
  3139. EXPECT_TRUE(video->rejected);
  3140. }
  3141. // Test that negotiation can succeed with a data channel only, and with the max
  3142. // bundle policy. Previously there was a bug that prevented this.
  3143. #ifdef HAVE_SCTP
  3144. TEST_P(PeerConnectionInterfaceTest, DataChannelOnlyOfferWithMaxBundlePolicy) {
  3145. #else
  3146. TEST_P(PeerConnectionInterfaceTest,
  3147. DISABLED_DataChannelOnlyOfferWithMaxBundlePolicy) {
  3148. #endif // HAVE_SCTP
  3149. PeerConnectionInterface::RTCConfiguration config;
  3150. config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
  3151. CreatePeerConnection(config);
  3152. // First, create an offer with only a data channel and apply it as a remote
  3153. // description.
  3154. pc_->CreateDataChannel("test", nullptr);
  3155. std::unique_ptr<SessionDescriptionInterface> offer;
  3156. ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
  3157. EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
  3158. // Create and set answer as well.
  3159. std::unique_ptr<SessionDescriptionInterface> answer;
  3160. ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
  3161. EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
  3162. }
  3163. TEST_P(PeerConnectionInterfaceTest, SetBitrateWithoutMinSucceeds) {
  3164. CreatePeerConnection();
  3165. PeerConnectionInterface::BitrateParameters bitrate;
  3166. bitrate.current_bitrate_bps = 100000;
  3167. EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
  3168. }
  3169. TEST_P(PeerConnectionInterfaceTest, SetBitrateNegativeMinFails) {
  3170. CreatePeerConnection();
  3171. PeerConnectionInterface::BitrateParameters bitrate;
  3172. bitrate.min_bitrate_bps = -1;
  3173. EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
  3174. }
  3175. TEST_P(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanMinFails) {
  3176. CreatePeerConnection();
  3177. PeerConnectionInterface::BitrateParameters bitrate;
  3178. bitrate.min_bitrate_bps = 5;
  3179. bitrate.current_bitrate_bps = 3;
  3180. EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
  3181. }
  3182. TEST_P(PeerConnectionInterfaceTest, SetBitrateCurrentNegativeFails) {
  3183. CreatePeerConnection();
  3184. PeerConnectionInterface::BitrateParameters bitrate;
  3185. bitrate.current_bitrate_bps = -1;
  3186. EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
  3187. }
  3188. TEST_P(PeerConnectionInterfaceTest, SetBitrateMaxLessThanCurrentFails) {
  3189. CreatePeerConnection();
  3190. PeerConnectionInterface::BitrateParameters bitrate;
  3191. bitrate.current_bitrate_bps = 10;
  3192. bitrate.max_bitrate_bps = 8;
  3193. EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
  3194. }
  3195. TEST_P(PeerConnectionInterfaceTest, SetBitrateMaxLessThanMinFails) {
  3196. CreatePeerConnection();
  3197. PeerConnectionInterface::BitrateParameters bitrate;
  3198. bitrate.min_bitrate_bps = 10;
  3199. bitrate.max_bitrate_bps = 8;
  3200. EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
  3201. }
  3202. TEST_P(PeerConnectionInterfaceTest, SetBitrateMaxNegativeFails) {
  3203. CreatePeerConnection();
  3204. PeerConnectionInterface::BitrateParameters bitrate;
  3205. bitrate.max_bitrate_bps = -1;
  3206. EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
  3207. }
  3208. // ice_regather_interval_range requires WebRTC to be configured for continual
  3209. // gathering already.
  3210. TEST_P(PeerConnectionInterfaceTest,
  3211. SetIceRegatherIntervalRangeWithoutContinualGatheringFails) {
  3212. PeerConnectionInterface::RTCConfiguration config;
  3213. config.ice_regather_interval_range.emplace(1000, 2000);
  3214. config.continual_gathering_policy =
  3215. PeerConnectionInterface::ContinualGatheringPolicy::GATHER_ONCE;
  3216. CreatePeerConnectionExpectFail(config);
  3217. }
  3218. // Ensures that there is no error when ice_regather_interval_range is set with
  3219. // continual gathering enabled.
  3220. TEST_P(PeerConnectionInterfaceTest,
  3221. SetIceRegatherIntervalRangeWithContinualGathering) {
  3222. PeerConnectionInterface::RTCConfiguration config;
  3223. config.ice_regather_interval_range.emplace(1000, 2000);
  3224. config.continual_gathering_policy =
  3225. PeerConnectionInterface::ContinualGatheringPolicy::GATHER_CONTINUALLY;
  3226. CreatePeerConnection(config);
  3227. }
  3228. // The current bitrate from BitrateSettings is currently clamped
  3229. // by Call's BitrateConstraints, which comes from the SDP or a default value.
  3230. // This test checks that a call to SetBitrate with a current bitrate that will
  3231. // be clamped succeeds.
  3232. TEST_P(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanImplicitMin) {
  3233. CreatePeerConnection();
  3234. PeerConnectionInterface::BitrateParameters bitrate;
  3235. bitrate.current_bitrate_bps = 1;
  3236. EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
  3237. }
  3238. // The following tests verify that the offer can be created correctly.
  3239. TEST_P(PeerConnectionInterfaceTest,
  3240. CreateOfferFailsWithInvalidOfferToReceiveAudio) {
  3241. RTCOfferAnswerOptions rtc_options;
  3242. // Setting offer_to_receive_audio to a value lower than kUndefined or greater
  3243. // than kMaxOfferToReceiveMedia should be treated as invalid.
  3244. rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
  3245. CreatePeerConnection();
  3246. EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
  3247. rtc_options.offer_to_receive_audio =
  3248. RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
  3249. EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
  3250. }
  3251. TEST_P(PeerConnectionInterfaceTest,
  3252. CreateOfferFailsWithInvalidOfferToReceiveVideo) {
  3253. RTCOfferAnswerOptions rtc_options;
  3254. // Setting offer_to_receive_video to a value lower than kUndefined or greater
  3255. // than kMaxOfferToReceiveMedia should be treated as invalid.
  3256. rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
  3257. CreatePeerConnection();
  3258. EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
  3259. rtc_options.offer_to_receive_video =
  3260. RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
  3261. EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
  3262. }
  3263. // Test that the audio and video content will be added to an offer if both
  3264. // |offer_to_receive_audio| and |offer_to_receive_video| options are 1.
  3265. TEST_P(PeerConnectionInterfaceTest, CreateOfferWithAudioVideoOptions) {
  3266. RTCOfferAnswerOptions rtc_options;
  3267. rtc_options.offer_to_receive_audio = 1;
  3268. rtc_options.offer_to_receive_video = 1;
  3269. std::unique_ptr<SessionDescriptionInterface> offer;
  3270. CreatePeerConnection();
  3271. offer = CreateOfferWithOptions(rtc_options);
  3272. ASSERT_TRUE(offer);
  3273. EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
  3274. EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
  3275. }
  3276. // Test that only audio content will be added to the offer if only
  3277. // |offer_to_receive_audio| options is 1.
  3278. TEST_P(PeerConnectionInterfaceTest, CreateOfferWithAudioOnlyOptions) {
  3279. RTCOfferAnswerOptions rtc_options;
  3280. rtc_options.offer_to_receive_audio = 1;
  3281. rtc_options.offer_to_receive_video = 0;
  3282. std::unique_ptr<SessionDescriptionInterface> offer;
  3283. CreatePeerConnection();
  3284. offer = CreateOfferWithOptions(rtc_options);
  3285. ASSERT_TRUE(offer);
  3286. EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
  3287. EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
  3288. }
  3289. // Test that only video content will be added if only |offer_to_receive_video|
  3290. // options is 1.
  3291. TEST_P(PeerConnectionInterfaceTest, CreateOfferWithVideoOnlyOptions) {
  3292. RTCOfferAnswerOptions rtc_options;
  3293. rtc_options.offer_to_receive_audio = 0;
  3294. rtc_options.offer_to_receive_video = 1;
  3295. std::unique_ptr<SessionDescriptionInterface> offer;
  3296. CreatePeerConnection();
  3297. offer = CreateOfferWithOptions(rtc_options);
  3298. ASSERT_TRUE(offer);
  3299. EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
  3300. EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
  3301. }
  3302. // Test that no media content will be added to the offer if using default
  3303. // RTCOfferAnswerOptions.
  3304. TEST_P(PeerConnectionInterfaceTest, CreateOfferWithDefaultOfferAnswerOptions) {
  3305. RTCOfferAnswerOptions rtc_options;
  3306. std::unique_ptr<SessionDescriptionInterface> offer;
  3307. CreatePeerConnection();
  3308. offer = CreateOfferWithOptions(rtc_options);
  3309. ASSERT_TRUE(offer);
  3310. EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
  3311. EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
  3312. }
  3313. // Test that if |ice_restart| is true, the ufrag/pwd will change, otherwise
  3314. // ufrag/pwd will be the same in the new offer.
  3315. TEST_P(PeerConnectionInterfaceTest, CreateOfferWithIceRestart) {
  3316. CreatePeerConnection();
  3317. RTCOfferAnswerOptions rtc_options;
  3318. rtc_options.ice_restart = false;
  3319. rtc_options.offer_to_receive_audio = 1;
  3320. std::unique_ptr<SessionDescriptionInterface> offer;
  3321. CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
  3322. std::string mid = cricket::GetFirstAudioContent(offer->description())->name;
  3323. auto ufrag1 =
  3324. offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
  3325. auto pwd1 =
  3326. offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
  3327. // |ice_restart| is false, the ufrag/pwd shouldn't change.
  3328. CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
  3329. auto ufrag2 =
  3330. offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
  3331. auto pwd2 =
  3332. offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
  3333. // |ice_restart| is true, the ufrag/pwd should change.
  3334. rtc_options.ice_restart = true;
  3335. CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
  3336. auto ufrag3 =
  3337. offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
  3338. auto pwd3 =
  3339. offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
  3340. EXPECT_EQ(ufrag1, ufrag2);
  3341. EXPECT_EQ(pwd1, pwd2);
  3342. EXPECT_NE(ufrag2, ufrag3);
  3343. EXPECT_NE(pwd2, pwd3);
  3344. }
  3345. // Test that if |use_rtp_mux| is true, the bundling will be enabled in the
  3346. // offer; if it is false, there won't be any bundle group in the offer.
  3347. TEST_P(PeerConnectionInterfaceTest, CreateOfferWithRtpMux) {
  3348. RTCOfferAnswerOptions rtc_options;
  3349. rtc_options.offer_to_receive_audio = 1;
  3350. rtc_options.offer_to_receive_video = 1;
  3351. std::unique_ptr<SessionDescriptionInterface> offer;
  3352. CreatePeerConnection();
  3353. rtc_options.use_rtp_mux = true;
  3354. offer = CreateOfferWithOptions(rtc_options);
  3355. ASSERT_TRUE(offer);
  3356. EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
  3357. EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
  3358. EXPECT_TRUE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
  3359. rtc_options.use_rtp_mux = false;
  3360. offer = CreateOfferWithOptions(rtc_options);
  3361. ASSERT_TRUE(offer);
  3362. EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
  3363. EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
  3364. EXPECT_FALSE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
  3365. }
  3366. // This test ensures OnRenegotiationNeeded is called when we add track with
  3367. // MediaStream -> AddTrack in the same way it is called when we add track with
  3368. // PeerConnection -> AddTrack.
  3369. // The test can be removed once addStream is rewritten in terms of addTrack
  3370. // https://bugs.chromium.org/p/webrtc/issues/detail?id=7815
  3371. // Don't run under Unified Plan since the stream API is not available.
  3372. TEST_F(PeerConnectionInterfaceTestPlanB,
  3373. MediaStreamAddTrackRemoveTrackRenegotiate) {
  3374. CreatePeerConnectionWithoutDtls();
  3375. rtc::scoped_refptr<MediaStreamInterface> stream(
  3376. pc_factory_->CreateLocalMediaStream(kStreamId1));
  3377. pc_->AddStream(stream);
  3378. rtc::scoped_refptr<AudioTrackInterface> audio_track(
  3379. CreateAudioTrack("audio_track"));
  3380. rtc::scoped_refptr<VideoTrackInterface> video_track(
  3381. CreateVideoTrack("video_track"));
  3382. stream->AddTrack(audio_track);
  3383. EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
  3384. observer_.renegotiation_needed_ = false;
  3385. CreateOfferReceiveAnswer();
  3386. stream->AddTrack(video_track);
  3387. EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
  3388. observer_.renegotiation_needed_ = false;
  3389. CreateOfferReceiveAnswer();
  3390. stream->RemoveTrack(audio_track);
  3391. EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
  3392. observer_.renegotiation_needed_ = false;
  3393. CreateOfferReceiveAnswer();
  3394. stream->RemoveTrack(video_track);
  3395. EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
  3396. observer_.renegotiation_needed_ = false;
  3397. }
  3398. // Tests that an error is returned if a description is applied that has fewer
  3399. // media sections than the existing description.
  3400. TEST_P(PeerConnectionInterfaceTest,
  3401. MediaSectionCountEnforcedForSubsequentOffer) {
  3402. CreatePeerConnection();
  3403. AddAudioTrack("audio_label");
  3404. AddVideoTrack("video_label");
  3405. std::unique_ptr<SessionDescriptionInterface> offer;
  3406. ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
  3407. EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
  3408. // A remote offer with fewer media sections should be rejected.
  3409. ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
  3410. offer->description()->contents().pop_back();
  3411. offer->description()->contents().pop_back();
  3412. ASSERT_TRUE(offer->description()->contents().empty());
  3413. EXPECT_FALSE(DoSetRemoteDescription(std::move(offer)));
  3414. std::unique_ptr<SessionDescriptionInterface> answer;
  3415. ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
  3416. EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
  3417. // A subsequent local offer with fewer media sections should be rejected.
  3418. ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
  3419. offer->description()->contents().pop_back();
  3420. offer->description()->contents().pop_back();
  3421. ASSERT_TRUE(offer->description()->contents().empty());
  3422. EXPECT_FALSE(DoSetLocalDescription(std::move(offer)));
  3423. }
  3424. TEST_P(PeerConnectionInterfaceTest, ExtmapAllowMixedIsConfigurable) {
  3425. RTCConfiguration config;
  3426. // Default behavior is false.
  3427. CreatePeerConnection(config);
  3428. std::unique_ptr<SessionDescriptionInterface> offer;
  3429. ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
  3430. EXPECT_FALSE(offer->description()->extmap_allow_mixed());
  3431. // Possible to set to true.
  3432. config.offer_extmap_allow_mixed = true;
  3433. CreatePeerConnection(config);
  3434. offer.release();
  3435. ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
  3436. EXPECT_TRUE(offer->description()->extmap_allow_mixed());
  3437. }
  3438. INSTANTIATE_TEST_SUITE_P(PeerConnectionInterfaceTest,
  3439. PeerConnectionInterfaceTest,
  3440. Values(SdpSemantics::kPlanB,
  3441. SdpSemantics::kUnifiedPlan));
  3442. class PeerConnectionMediaConfigTest : public ::testing::Test {
  3443. protected:
  3444. void SetUp() override {
  3445. pcf_ = PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
  3446. pcf_->Initialize();
  3447. }
  3448. const cricket::MediaConfig TestCreatePeerConnection(
  3449. const RTCConfiguration& config) {
  3450. rtc::scoped_refptr<PeerConnectionInterface> pc(
  3451. pcf_->CreatePeerConnection(config, nullptr, nullptr, &observer_));
  3452. EXPECT_TRUE(pc.get());
  3453. observer_.SetPeerConnectionInterface(pc.get());
  3454. return pc->GetConfiguration().media_config;
  3455. }
  3456. rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
  3457. MockPeerConnectionObserver observer_;
  3458. };
  3459. // This sanity check validates the test infrastructure itself.
  3460. TEST_F(PeerConnectionMediaConfigTest, TestCreateAndClose) {
  3461. PeerConnectionInterface::RTCConfiguration config;
  3462. rtc::scoped_refptr<PeerConnectionInterface> pc(
  3463. pcf_->CreatePeerConnection(config, nullptr, nullptr, &observer_));
  3464. EXPECT_TRUE(pc.get());
  3465. observer_.SetPeerConnectionInterface(pc.get()); // Required.
  3466. pc->Close(); // No abort -> ok.
  3467. SUCCEED();
  3468. }
  3469. // This test verifies the default behaviour with no constraints and a
  3470. // default RTCConfiguration.
  3471. TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
  3472. PeerConnectionInterface::RTCConfiguration config;
  3473. const cricket::MediaConfig& media_config = TestCreatePeerConnection(config);
  3474. EXPECT_FALSE(media_config.enable_dscp);
  3475. EXPECT_TRUE(media_config.video.enable_cpu_adaptation);
  3476. EXPECT_TRUE(media_config.video.enable_prerenderer_smoothing);
  3477. EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
  3478. EXPECT_FALSE(media_config.video.experiment_cpu_load_estimator);
  3479. }
  3480. // This test verifies that the enable_prerenderer_smoothing flag is
  3481. // propagated from RTCConfiguration to the PeerConnection.
  3482. TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
  3483. PeerConnectionInterface::RTCConfiguration config;
  3484. config.set_prerenderer_smoothing(false);
  3485. const cricket::MediaConfig& media_config = TestCreatePeerConnection(config);
  3486. EXPECT_FALSE(media_config.video.enable_prerenderer_smoothing);
  3487. }
  3488. // This test verifies that the experiment_cpu_load_estimator flag is
  3489. // propagated from RTCConfiguration to the PeerConnection.
  3490. TEST_F(PeerConnectionMediaConfigTest, TestEnableExperimentCpuLoadEstimator) {
  3491. PeerConnectionInterface::RTCConfiguration config;
  3492. config.set_experiment_cpu_load_estimator(true);
  3493. const cricket::MediaConfig& media_config = TestCreatePeerConnection(config);
  3494. EXPECT_TRUE(media_config.video.experiment_cpu_load_estimator);
  3495. }
  3496. // Tests a few random fields being different.
  3497. TEST(RTCConfigurationTest, ComparisonOperators) {
  3498. PeerConnectionInterface::RTCConfiguration a;
  3499. PeerConnectionInterface::RTCConfiguration b;
  3500. EXPECT_EQ(a, b);
  3501. PeerConnectionInterface::RTCConfiguration c;
  3502. c.servers.push_back(PeerConnectionInterface::IceServer());
  3503. EXPECT_NE(a, c);
  3504. PeerConnectionInterface::RTCConfiguration d;
  3505. d.type = PeerConnectionInterface::kRelay;
  3506. EXPECT_NE(a, d);
  3507. PeerConnectionInterface::RTCConfiguration e;
  3508. e.audio_jitter_buffer_max_packets = 5;
  3509. EXPECT_NE(a, e);
  3510. PeerConnectionInterface::RTCConfiguration f;
  3511. f.ice_connection_receiving_timeout = 1337;
  3512. EXPECT_NE(a, f);
  3513. PeerConnectionInterface::RTCConfiguration g;
  3514. g.disable_ipv6 = true;
  3515. EXPECT_NE(a, g);
  3516. PeerConnectionInterface::RTCConfiguration h(
  3517. PeerConnectionInterface::RTCConfigurationType::kAggressive);
  3518. EXPECT_NE(a, h);
  3519. }
  3520. } // namespace
  3521. } // namespace webrtc