|
23 | 23 | import java.util.ArrayList; |
24 | 24 | import java.util.Collections; |
25 | 25 | import java.util.HashMap; |
| 26 | +import java.util.EnumSet; |
| 27 | +import java.util.HashSet; |
26 | 28 | import java.util.List; |
27 | 29 | import java.util.Map; |
| 30 | +import java.util.Set; |
28 | 31 | import java.util.UUID; |
29 | 32 | import java.util.concurrent.CompletableFuture; |
30 | 33 | import java.util.concurrent.CompletionException; |
@@ -167,6 +170,13 @@ final class PeerImpl implements Peer { |
167 | 170 |
|
168 | 171 | private final NegotiatorImpl negotiator = new NegotiatorImpl(); |
169 | 172 |
|
| 173 | + /** |
| 174 | + * The kinds of media whose tracks this peer drops, those it declined when |
| 175 | + * it answered an offer that replaced its own. Only on the control |
| 176 | + * executor. |
| 177 | + */ |
| 178 | + private final Set<MediaKind> declinedKinds = EnumSet.noneOf(MediaKind.class); |
| 179 | + |
170 | 180 |
|
171 | 181 | PeerImpl(WebRtc rtc, PeerOptions options, String name) { |
172 | 182 | this.rtc = rtc; |
@@ -423,21 +433,106 @@ public Negotiator negotiator() { |
423 | 433 | * Takes this peer's own offer back and answers the given one instead, as |
424 | 434 | * a WHEP player does when the server counters its offer. Unlike {@link |
425 | 435 | * Negotiator#answer}, which fails while an offer of this peer is out. |
| 436 | + * <p> |
| 437 | + * The answer receives only the kinds of media the peer was built to |
| 438 | + * receive: the audio or video m-lines of other kinds are rejected, with |
| 439 | + * port 0. An offer without any m-line of those kinds fails. |
426 | 440 | * |
427 | 441 | * @param offer the offer that replaces this peer's own. |
428 | 442 | * @param gathering when the answer is handed over. |
429 | 443 | * |
430 | 444 | * @return a future with the answer, applied as the local description. |
431 | 445 | */ |
432 | 446 | CompletableFuture<RTCSessionDescription> answerInstead(RTCSessionDescription offer, Gathering gathering) { |
433 | | - return negotiator.run(() -> setLocal(new RTCSessionDescription(RTCSdpType.ROLLBACK, "")) |
434 | | - .thenCompose(ignored -> setRemote(offer)) |
435 | | - .thenCompose(ignored -> { |
436 | | - releaseHeldCandidates(); |
437 | | - return createAnswer(); |
438 | | - }) |
439 | | - .thenCompose(this::setLocal) |
440 | | - .thenCompose(ignored -> negotiator.gathered(gathering))); |
| 447 | + Set<String> rejected; |
| 448 | + |
| 449 | + try { |
| 450 | + rejected = midsNotReceived(offer.sdp); |
| 451 | + } |
| 452 | + catch (NegotiationException e) { |
| 453 | + return toPublic(CompletableFuture.failedFuture(e)); |
| 454 | + } |
| 455 | + |
| 456 | + return negotiator.run(() -> { |
| 457 | + // Before the offer is applied, which reports the tracks of the |
| 458 | + // m-lines that are then rejected. |
| 459 | + for (MediaKind kind : MediaKind.values()) { |
| 460 | + if (!options.receive.contains(kind)) { |
| 461 | + declinedKinds.add(kind); |
| 462 | + } |
| 463 | + } |
| 464 | + |
| 465 | + return setLocal(new RTCSessionDescription(RTCSdpType.ROLLBACK, "")) |
| 466 | + .thenCompose(ignored -> setRemote(offer)) |
| 467 | + .thenCompose(ignored -> { |
| 468 | + stopTransceivers(rejected); |
| 469 | + releaseHeldCandidates(); |
| 470 | + return createAnswer(); |
| 471 | + }) |
| 472 | + .thenCompose(this::setLocal) |
| 473 | + .thenCompose(ignored -> negotiator.gathered(gathering)); |
| 474 | + }); |
| 475 | + } |
| 476 | + |
| 477 | + /** |
| 478 | + * Finds the audio and video m-lines of an offer whose kind this peer does |
| 479 | + * not receive. |
| 480 | + * |
| 481 | + * @return their mids. |
| 482 | + * |
| 483 | + * @throws NegotiationException if the offer has no open m-line of a kind |
| 484 | + * this peer receives. |
| 485 | + */ |
| 486 | + private Set<String> midsNotReceived(String sdp) { |
| 487 | + Set<String> rejected = new HashSet<>(); |
| 488 | + boolean received = false; |
| 489 | + MediaKind kind = null; |
| 490 | + |
| 491 | + for (String line : sdp.split("\r?\n")) { |
| 492 | + if (line.startsWith("m=")) { |
| 493 | + String[] fields = line.substring(2).split(" "); |
| 494 | + boolean open = fields.length > 1 && !fields[1].equals("0"); |
| 495 | + |
| 496 | + kind = !open ? null |
| 497 | + : fields[0].equals("audio") ? MediaKind.AUDIO |
| 498 | + : fields[0].equals("video") ? MediaKind.VIDEO |
| 499 | + : null; |
| 500 | + received |= kind != null && options.receive.contains(kind); |
| 501 | + } |
| 502 | + else if (line.startsWith("a=mid:") && kind != null && !options.receive.contains(kind)) { |
| 503 | + rejected.add(line.substring("a=mid:".length()).trim()); |
| 504 | + } |
| 505 | + } |
| 506 | + |
| 507 | + if (!received) { |
| 508 | + throw new NegotiationException("The offer of the remote peer of " + name |
| 509 | + + " holds none of the media it receives, " + options.receive); |
| 510 | + } |
| 511 | + |
| 512 | + return rejected; |
| 513 | + } |
| 514 | + |
| 515 | + /** |
| 516 | + * Stops the transceivers of the given mids, whose m-lines the next |
| 517 | + * answer then rejects. |
| 518 | + */ |
| 519 | + private void stopTransceivers(Set<String> mids) { |
| 520 | + if (mids.isEmpty()) { |
| 521 | + return; |
| 522 | + } |
| 523 | + |
| 524 | + for (RTCRtpTransceiver transceiver : connection.getTransceivers()) { |
| 525 | + try { |
| 526 | + if (mids.contains(transceiver.getMid()) && !transceiver.stopped()) { |
| 527 | + // At once: an answer rejects only the m-lines of stopped |
| 528 | + // transceivers, and keeps those still stopping, inactive. |
| 529 | + transceiver.stopImmediately(); |
| 530 | + } |
| 531 | + } |
| 532 | + finally { |
| 533 | + transceiver.dispose(); |
| 534 | + } |
| 535 | + } |
441 | 536 | } |
442 | 537 |
|
443 | 538 | @Override |
@@ -1132,6 +1227,13 @@ private void trackAdded(RTCRtpReceiver receiver, MediaStream[] streams) { |
1132 | 1227 | MediaStreamTrack track = receiver.getTrack(); |
1133 | 1228 | String id = track.getId(); |
1134 | 1229 |
|
| 1230 | + if (declinedKinds.contains(track instanceof VideoTrack ? MediaKind.VIDEO : MediaKind.AUDIO)) { |
| 1231 | + // Of an m-line this peer rejected, when it answered an offer that |
| 1232 | + // replaced its own. |
| 1233 | + track.dispose(); |
| 1234 | + receiver.dispose(); |
| 1235 | + return; |
| 1236 | + } |
1135 | 1237 | if (remoteTrackById.containsKey(id)) { |
1136 | 1238 | // Renegotiation reported a track that is already known. |
1137 | 1239 | track.dispose(); |
|
0 commit comments