Harald Alvestrand | 1090e44 | 2020-10-05 07:01:09 +0000 | [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 | |
Harald Alvestrand | c24a218 | 2022-02-23 13:44:59 +0000 | [diff] [blame] | 13 | #include <list> |
Harald Alvestrand | 1090e44 | 2020-10-05 07:01:09 +0000 | [diff] [blame] | 14 | #include <utility> |
| 15 | |
| 16 | #include "api/jsep.h" |
| 17 | #include "api/media_stream_interface.h" |
| 18 | #include "api/peer_connection_interface.h" |
Harald Alvestrand | 5761e7b | 2021-01-29 14:45:08 +0000 | [diff] [blame] | 19 | #include "api/scoped_refptr.h" |
Artem Titov | d15a575 | 2021-02-10 14:31:24 +0100 | [diff] [blame] | 20 | #include "api/sequence_checker.h" |
Harald Alvestrand | 5761e7b | 2021-01-29 14:45:08 +0000 | [diff] [blame] | 21 | #include "api/stats_types.h" |
Harald Alvestrand | 1090e44 | 2020-10-05 07:01:09 +0000 | [diff] [blame] | 22 | #include "pc/stats_collector_interface.h" |
Harald Alvestrand | 5761e7b | 2021-01-29 14:45:08 +0000 | [diff] [blame] | 23 | #include "rtc_base/checks.h" |
| 24 | #include "rtc_base/location.h" |
Harald Alvestrand | 1090e44 | 2020-10-05 07:01:09 +0000 | [diff] [blame] | 25 | |
| 26 | namespace webrtc { |
| 27 | |
| 28 | namespace { |
| 29 | |
| 30 | enum { |
| 31 | MSG_SET_SESSIONDESCRIPTION_SUCCESS = 0, |
| 32 | MSG_SET_SESSIONDESCRIPTION_FAILED, |
| 33 | MSG_CREATE_SESSIONDESCRIPTION_FAILED, |
| 34 | MSG_GETSTATS, |
| 35 | MSG_REPORT_USAGE_PATTERN, |
| 36 | }; |
| 37 | |
| 38 | struct SetSessionDescriptionMsg : public rtc::MessageData { |
| 39 | explicit SetSessionDescriptionMsg( |
| 40 | webrtc::SetSessionDescriptionObserver* observer) |
| 41 | : observer(observer) {} |
| 42 | |
| 43 | rtc::scoped_refptr<webrtc::SetSessionDescriptionObserver> observer; |
| 44 | RTCError error; |
| 45 | }; |
| 46 | |
| 47 | struct CreateSessionDescriptionMsg : public rtc::MessageData { |
| 48 | explicit CreateSessionDescriptionMsg( |
| 49 | webrtc::CreateSessionDescriptionObserver* observer) |
| 50 | : observer(observer) {} |
| 51 | |
| 52 | rtc::scoped_refptr<webrtc::CreateSessionDescriptionObserver> observer; |
| 53 | RTCError error; |
| 54 | }; |
| 55 | |
| 56 | struct GetStatsMsg : public rtc::MessageData { |
| 57 | GetStatsMsg(webrtc::StatsObserver* observer, |
| 58 | StatsCollectorInterface* stats, |
| 59 | webrtc::MediaStreamTrackInterface* track) |
| 60 | : observer(observer), stats(stats), track(track) {} |
| 61 | rtc::scoped_refptr<webrtc::StatsObserver> observer; |
| 62 | StatsCollectorInterface* stats; |
| 63 | rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> track; |
| 64 | }; |
| 65 | |
| 66 | struct RequestUsagePatternMsg : public rtc::MessageData { |
| 67 | explicit RequestUsagePatternMsg(std::function<void()> func) |
| 68 | : function(func) {} |
| 69 | std::function<void()> function; |
| 70 | }; |
| 71 | |
| 72 | } // namespace |
| 73 | |
| 74 | PeerConnectionMessageHandler::~PeerConnectionMessageHandler() { |
| 75 | // Process all pending notifications in the message queue. If we don't do |
| 76 | // this, requests will linger and not know they succeeded or failed. |
| 77 | rtc::MessageList list; |
| 78 | signaling_thread()->Clear(this, rtc::MQID_ANY, &list); |
| 79 | for (auto& msg : list) { |
| 80 | if (msg.message_id == MSG_CREATE_SESSIONDESCRIPTION_FAILED) { |
| 81 | // Processing CreateOffer() and CreateAnswer() messages ensures their |
| 82 | // observers are invoked even if the PeerConnection is destroyed early. |
| 83 | OnMessage(&msg); |
| 84 | } else { |
| 85 | // TODO(hbos): Consider processing all pending messages. This would mean |
| 86 | // that SetLocalDescription() and SetRemoteDescription() observers are |
| 87 | // informed of successes and failures; this is currently NOT the case. |
| 88 | delete msg.pdata; |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | void PeerConnectionMessageHandler::OnMessage(rtc::Message* msg) { |
| 94 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 95 | switch (msg->message_id) { |
| 96 | case MSG_SET_SESSIONDESCRIPTION_SUCCESS: { |
| 97 | SetSessionDescriptionMsg* param = |
| 98 | static_cast<SetSessionDescriptionMsg*>(msg->pdata); |
| 99 | param->observer->OnSuccess(); |
| 100 | delete param; |
| 101 | break; |
| 102 | } |
| 103 | case MSG_SET_SESSIONDESCRIPTION_FAILED: { |
| 104 | SetSessionDescriptionMsg* param = |
| 105 | static_cast<SetSessionDescriptionMsg*>(msg->pdata); |
| 106 | param->observer->OnFailure(std::move(param->error)); |
| 107 | delete param; |
| 108 | break; |
| 109 | } |
| 110 | case MSG_CREATE_SESSIONDESCRIPTION_FAILED: { |
| 111 | CreateSessionDescriptionMsg* param = |
| 112 | static_cast<CreateSessionDescriptionMsg*>(msg->pdata); |
| 113 | param->observer->OnFailure(std::move(param->error)); |
| 114 | delete param; |
| 115 | break; |
| 116 | } |
| 117 | case MSG_GETSTATS: { |
| 118 | GetStatsMsg* param = static_cast<GetStatsMsg*>(msg->pdata); |
| 119 | StatsReports reports; |
Niels Möller | afb246b | 2022-04-20 14:26:50 +0200 | [diff] [blame^] | 120 | param->stats->GetStats(param->track.get(), &reports); |
Harald Alvestrand | 1090e44 | 2020-10-05 07:01:09 +0000 | [diff] [blame] | 121 | param->observer->OnComplete(reports); |
| 122 | delete param; |
| 123 | break; |
| 124 | } |
| 125 | case MSG_REPORT_USAGE_PATTERN: { |
| 126 | RequestUsagePatternMsg* param = |
| 127 | static_cast<RequestUsagePatternMsg*>(msg->pdata); |
| 128 | param->function(); |
| 129 | delete param; |
| 130 | break; |
| 131 | } |
| 132 | default: |
Artem Titov | d325196 | 2021-11-15 16:57:07 +0100 | [diff] [blame] | 133 | RTC_DCHECK_NOTREACHED() << "Not implemented"; |
Harald Alvestrand | 1090e44 | 2020-10-05 07:01:09 +0000 | [diff] [blame] | 134 | break; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | void PeerConnectionMessageHandler::PostSetSessionDescriptionSuccess( |
| 139 | SetSessionDescriptionObserver* observer) { |
| 140 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
| 141 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 142 | MSG_SET_SESSIONDESCRIPTION_SUCCESS, msg); |
| 143 | } |
| 144 | |
| 145 | void PeerConnectionMessageHandler::PostSetSessionDescriptionFailure( |
| 146 | SetSessionDescriptionObserver* observer, |
| 147 | RTCError&& error) { |
| 148 | RTC_DCHECK(!error.ok()); |
| 149 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
| 150 | msg->error = std::move(error); |
| 151 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 152 | MSG_SET_SESSIONDESCRIPTION_FAILED, msg); |
| 153 | } |
| 154 | |
| 155 | void PeerConnectionMessageHandler::PostCreateSessionDescriptionFailure( |
| 156 | CreateSessionDescriptionObserver* observer, |
| 157 | RTCError error) { |
| 158 | RTC_DCHECK(!error.ok()); |
| 159 | CreateSessionDescriptionMsg* msg = new CreateSessionDescriptionMsg(observer); |
| 160 | msg->error = std::move(error); |
| 161 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 162 | MSG_CREATE_SESSIONDESCRIPTION_FAILED, msg); |
| 163 | } |
| 164 | |
| 165 | void PeerConnectionMessageHandler::PostGetStats( |
| 166 | StatsObserver* observer, |
| 167 | StatsCollectorInterface* stats, |
| 168 | MediaStreamTrackInterface* track) { |
| 169 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_GETSTATS, |
| 170 | new GetStatsMsg(observer, stats, track)); |
| 171 | } |
| 172 | |
| 173 | void PeerConnectionMessageHandler::RequestUsagePatternReport( |
| 174 | std::function<void()> func, |
| 175 | int delay_ms) { |
| 176 | signaling_thread()->PostDelayed(RTC_FROM_HERE, delay_ms, this, |
| 177 | MSG_REPORT_USAGE_PATTERN, |
| 178 | new RequestUsagePatternMsg(func)); |
| 179 | } |
| 180 | |
| 181 | } // namespace webrtc |