Harald Alvestrand | 1090e44 | 2020-10-05 07:01:09 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include "pc/peer_connection_message_handler.h" |
| 12 | |
| 13 | #include <utility> |
| 14 | |
| 15 | #include "api/jsep.h" |
Henrik Boström | 3e6931b | 2022-11-11 09:07:34 | [diff] [blame] | 16 | #include "api/legacy_stats_types.h" |
Harald Alvestrand | 1090e44 | 2020-10-05 07:01:09 | [diff] [blame] | 17 | #include "api/media_stream_interface.h" |
| 18 | #include "api/peer_connection_interface.h" |
Harald Alvestrand | 5761e7b | 2021-01-29 14:45:08 | [diff] [blame] | 19 | #include "api/scoped_refptr.h" |
Artem Titov | d15a575 | 2021-02-10 13:31:24 | [diff] [blame] | 20 | #include "api/sequence_checker.h" |
Danil Chapovalov | 5d37ba2 | 2022-08-17 12:58:40 | [diff] [blame] | 21 | #include "api/task_queue/pending_task_safety_flag.h" |
Henrik Boström | f785989 | 2022-07-04 12:36:37 | [diff] [blame] | 22 | #include "pc/legacy_stats_collector_interface.h" |
Harald Alvestrand | 5761e7b | 2021-01-29 14:45:08 | [diff] [blame] | 23 | #include "rtc_base/checks.h" |
Harald Alvestrand | 1090e44 | 2020-10-05 07:01:09 | [diff] [blame] | 24 | |
| 25 | namespace webrtc { |
Harald Alvestrand | 1090e44 | 2020-10-05 07:01:09 | [diff] [blame] | 26 | namespace { |
| 27 | |
Danil Chapovalov | 5d37ba2 | 2022-08-17 12:58:40 | [diff] [blame] | 28 | template <typename T> |
| 29 | rtc::scoped_refptr<T> WrapScoped(T* ptr) { |
| 30 | return rtc::scoped_refptr<T>(ptr); |
| 31 | } |
Harald Alvestrand | 1090e44 | 2020-10-05 07:01:09 | [diff] [blame] | 32 | |
| 33 | } // namespace |
| 34 | |
Harald Alvestrand | 1090e44 | 2020-10-05 07:01:09 | [diff] [blame] | 35 | void PeerConnectionMessageHandler::PostSetSessionDescriptionSuccess( |
| 36 | SetSessionDescriptionObserver* observer) { |
Danil Chapovalov | 5d37ba2 | 2022-08-17 12:58:40 | [diff] [blame] | 37 | signaling_thread_->PostTask( |
| 38 | SafeTask(safety_.flag(), |
| 39 | [observer = WrapScoped(observer)] { observer->OnSuccess(); })); |
Harald Alvestrand | 1090e44 | 2020-10-05 07:01:09 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | void PeerConnectionMessageHandler::PostSetSessionDescriptionFailure( |
| 43 | SetSessionDescriptionObserver* observer, |
| 44 | RTCError&& error) { |
| 45 | RTC_DCHECK(!error.ok()); |
Danil Chapovalov | 5d37ba2 | 2022-08-17 12:58:40 | [diff] [blame] | 46 | signaling_thread_->PostTask(SafeTask( |
| 47 | safety_.flag(), |
| 48 | [observer = WrapScoped(observer), error = std::move(error)]() mutable { |
| 49 | observer->OnFailure(std::move(error)); |
| 50 | })); |
Harald Alvestrand | 1090e44 | 2020-10-05 07:01:09 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | void PeerConnectionMessageHandler::PostCreateSessionDescriptionFailure( |
| 54 | CreateSessionDescriptionObserver* observer, |
| 55 | RTCError error) { |
| 56 | RTC_DCHECK(!error.ok()); |
Danil Chapovalov | 5d37ba2 | 2022-08-17 12:58:40 | [diff] [blame] | 57 | // Do not protect this task with the safety_.flag() to ensure |
| 58 | // observer is invoked even if the PeerConnection is destroyed early. |
| 59 | signaling_thread_->PostTask( |
| 60 | [observer = WrapScoped(observer), error = std::move(error)]() mutable { |
| 61 | observer->OnFailure(std::move(error)); |
| 62 | }); |
Harald Alvestrand | 1090e44 | 2020-10-05 07:01:09 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | void PeerConnectionMessageHandler::PostGetStats( |
| 66 | StatsObserver* observer, |
Henrik Boström | f785989 | 2022-07-04 12:36:37 | [diff] [blame] | 67 | LegacyStatsCollectorInterface* legacy_stats, |
Harald Alvestrand | 1090e44 | 2020-10-05 07:01:09 | [diff] [blame] | 68 | MediaStreamTrackInterface* track) { |
Danil Chapovalov | 5d37ba2 | 2022-08-17 12:58:40 | [diff] [blame] | 69 | signaling_thread_->PostTask( |
| 70 | SafeTask(safety_.flag(), [observer = WrapScoped(observer), legacy_stats, |
| 71 | track = WrapScoped(track)] { |
| 72 | StatsReports reports; |
| 73 | legacy_stats->GetStats(track.get(), &reports); |
| 74 | observer->OnComplete(reports); |
| 75 | })); |
Harald Alvestrand | 1090e44 | 2020-10-05 07:01:09 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | void PeerConnectionMessageHandler::RequestUsagePatternReport( |
| 79 | std::function<void()> func, |
| 80 | int delay_ms) { |
Danil Chapovalov | 5d37ba2 | 2022-08-17 12:58:40 | [diff] [blame] | 81 | signaling_thread_->PostDelayedTask(SafeTask(safety_.flag(), std::move(func)), |
| 82 | TimeDelta::Millis(delay_ms)); |
Harald Alvestrand | 1090e44 | 2020-10-05 07:01:09 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | } // namespace webrtc |