blob: 2d674aad4d551342cc39c10016c70699ea2484a0 [file] [log] [blame]
Harald Alvestrand1090e442020-10-05 07:01:09 +00001/*
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"
16#include "api/media_stream_interface.h"
17#include "api/peer_connection_interface.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000018#include "api/scoped_refptr.h"
Artem Titovd15a5752021-02-10 14:31:24 +010019#include "api/sequence_checker.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000020#include "api/stats_types.h"
Danil Chapovalov5d37ba22022-08-17 14:58:40 +020021#include "api/task_queue/pending_task_safety_flag.h"
Henrik Boströmf7859892022-07-04 14:36:37 +020022#include "pc/legacy_stats_collector_interface.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000023#include "rtc_base/checks.h"
Harald Alvestrand1090e442020-10-05 07:01:09 +000024
25namespace webrtc {
Harald Alvestrand1090e442020-10-05 07:01:09 +000026namespace {
27
Danil Chapovalov5d37ba22022-08-17 14:58:40 +020028template <typename T>
29rtc::scoped_refptr<T> WrapScoped(T* ptr) {
30 return rtc::scoped_refptr<T>(ptr);
31}
Harald Alvestrand1090e442020-10-05 07:01:09 +000032
33} // namespace
34
Harald Alvestrand1090e442020-10-05 07:01:09 +000035void PeerConnectionMessageHandler::PostSetSessionDescriptionSuccess(
36 SetSessionDescriptionObserver* observer) {
Danil Chapovalov5d37ba22022-08-17 14:58:40 +020037 signaling_thread_->PostTask(
38 SafeTask(safety_.flag(),
39 [observer = WrapScoped(observer)] { observer->OnSuccess(); }));
Harald Alvestrand1090e442020-10-05 07:01:09 +000040}
41
42void PeerConnectionMessageHandler::PostSetSessionDescriptionFailure(
43 SetSessionDescriptionObserver* observer,
44 RTCError&& error) {
45 RTC_DCHECK(!error.ok());
Danil Chapovalov5d37ba22022-08-17 14:58:40 +020046 signaling_thread_->PostTask(SafeTask(
47 safety_.flag(),
48 [observer = WrapScoped(observer), error = std::move(error)]() mutable {
49 observer->OnFailure(std::move(error));
50 }));
Harald Alvestrand1090e442020-10-05 07:01:09 +000051}
52
53void PeerConnectionMessageHandler::PostCreateSessionDescriptionFailure(
54 CreateSessionDescriptionObserver* observer,
55 RTCError error) {
56 RTC_DCHECK(!error.ok());
Danil Chapovalov5d37ba22022-08-17 14:58:40 +020057 // 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 Alvestrand1090e442020-10-05 07:01:09 +000063}
64
65void PeerConnectionMessageHandler::PostGetStats(
66 StatsObserver* observer,
Henrik Boströmf7859892022-07-04 14:36:37 +020067 LegacyStatsCollectorInterface* legacy_stats,
Harald Alvestrand1090e442020-10-05 07:01:09 +000068 MediaStreamTrackInterface* track) {
Danil Chapovalov5d37ba22022-08-17 14:58:40 +020069 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 Alvestrand1090e442020-10-05 07:01:09 +000076}
77
78void PeerConnectionMessageHandler::RequestUsagePatternReport(
79 std::function<void()> func,
80 int delay_ms) {
Danil Chapovalov5d37ba22022-08-17 14:58:40 +020081 signaling_thread_->PostDelayedTask(SafeTask(safety_.flag(), std::move(func)),
82 TimeDelta::Millis(delay_ms));
Harald Alvestrand1090e442020-10-05 07:01:09 +000083}
84
85} // namespace webrtc