Skip to content
This repository was archived by the owner on Oct 25, 2024. It is now read-only.

Commit 5ebc7db

Browse files
committed
Remove SDP munging for P2P mode.
This change replaces SDP munging with setCodecPreference for P2P mode. Changes of OWT APIs are needed to fully support setCodecPreference. For example, OWT APIs allow different bitrate settings for different codecs, which is not supported by WebRTC, so only the first codec's bitrate setting takes effect at this time.
1 parent 204db47 commit 5ebc7db

File tree

6 files changed

+168
-38
lines changed

6 files changed

+168
-38
lines changed

talk/owt/sdk/base/peerconnectionchannel.cc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// SPDX-License-Identifier: Apache-2.0
44
#include "talk/owt/sdk/base/peerconnectionchannel.h"
55
#include <vector>
6-
#include "talk/owt/sdk/base/sdputils.h"
76
#include "webrtc/api/peer_connection_interface.h"
87
#include "webrtc/rtc_base/logging.h"
98
#include "webrtc/rtc_base/thread.h"
@@ -83,16 +82,17 @@ void PeerConnectionChannel::ApplyBitrateSettings() {
8382
return;
8483
}
8584

86-
void PeerConnectionChannel::AddTransceiver(
85+
webrtc::RTCErrorOr<rtc::scoped_refptr<webrtc::RtpTransceiverInterface>>
86+
PeerConnectionChannel::AddTransceiver(
8787
rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> track,
8888
const webrtc::RtpTransceiverInit& init) {
89-
peer_connection_->AddTransceiver(track, init);
89+
return peer_connection_->AddTransceiver(track, init);
9090
}
9191

92-
void PeerConnectionChannel::AddTransceiver(
93-
cricket::MediaType media_type,
94-
const webrtc::RtpTransceiverInit& init) {
95-
peer_connection_->AddTransceiver(media_type, init);
92+
webrtc::RTCErrorOr<rtc::scoped_refptr<webrtc::RtpTransceiverInterface>>
93+
PeerConnectionChannel::AddTransceiver(cricket::MediaType media_type,
94+
const webrtc::RtpTransceiverInit& init) {
95+
return peer_connection_->AddTransceiver(media_type, init);
9696
}
9797

9898
const webrtc::SessionDescriptionInterface*

talk/owt/sdk/base/peerconnectionchannel.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,14 @@ class PeerConnectionChannel : public webrtc::PeerConnectionObserver,
4141
// message to PeerConnectionChannel.
4242
virtual void CreateOffer() = 0;
4343
virtual void CreateAnswer() = 0;
44-
virtual void AddTransceiver(
45-
rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> track,
46-
const webrtc::RtpTransceiverInit& init);
47-
virtual void AddTransceiver(cricket::MediaType media_type,
48-
const webrtc::RtpTransceiverInit& init);
44+
virtual webrtc::RTCErrorOr<
45+
rtc::scoped_refptr<webrtc::RtpTransceiverInterface>>
46+
AddTransceiver(rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> track,
47+
const webrtc::RtpTransceiverInit& init);
48+
virtual webrtc::RTCErrorOr<
49+
rtc::scoped_refptr<webrtc::RtpTransceiverInterface>>
50+
AddTransceiver(cricket::MediaType media_type,
51+
const webrtc::RtpTransceiverInit& init);
4952
// PeerConnectionObserver
5053
virtual void OnStateChange(webrtc::StatsReport::StatsType state_changed) {}
5154
virtual void OnSignalingChange(

talk/owt/sdk/base/peerconnectiondependencyfactory.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,5 +369,22 @@ scoped_refptr<webrtc::AudioDeviceModule> PeerConnectionDependencyFactory::
369369
GlobalConfiguration::GetAudioFrameGenerator());
370370
}
371371

372+
std::unique_ptr<webrtc::RtpCapabilities>
373+
PeerConnectionDependencyFactory::GetSenderCapabilities(
374+
const std::string& kind) {
375+
if (kind == "audio") {
376+
return pc_thread_->BlockingCall([this] {
377+
return std::make_unique<webrtc::RtpCapabilities>(
378+
pc_factory_->GetRtpSenderCapabilities(cricket::MEDIA_TYPE_AUDIO));
379+
});
380+
} else if (kind == "video") {
381+
return pc_thread_->BlockingCall([this] {
382+
return std::make_unique<webrtc::RtpCapabilities>(
383+
pc_factory_->GetRtpSenderCapabilities(cricket::MEDIA_TYPE_VIDEO));
384+
});
385+
}
386+
return nullptr;
387+
}
388+
372389
} // namespace base
373390
} // namespace owt

talk/owt/sdk/base/peerconnectiondependencyfactory.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class PeerConnectionThread : public rtc::Thread {
3535
// Object factory for WebRTC PeerConnections.
3636
class PeerConnectionDependencyFactory : public rtc::RefCountInterface {
3737
public:
38+
~PeerConnectionDependencyFactory() override;
3839
// Get a PeerConnectionDependencyFactory instance. It doesn't create a new
3940
// instance. It always return the same instance.
4041
static PeerConnectionDependencyFactory* Get();
@@ -57,9 +58,12 @@ class PeerConnectionDependencyFactory : public rtc::RefCountInterface {
5758
// Returns current |pc_factory_|.
5859
rtc::scoped_refptr<PeerConnectionFactoryInterface> PeerConnectionFactory()
5960
const;
61+
std::unique_ptr<webrtc::RtpCapabilities> GetSenderCapabilities(
62+
const std::string& kind);
63+
6064
// Returns |signaling_thread_| for testing.
6165
rtc::Thread* SignalingThreadForTesting();
62-
~PeerConnectionDependencyFactory() override;
66+
6367
protected:
6468
explicit PeerConnectionDependencyFactory();
6569
virtual const rtc::scoped_refptr<PeerConnectionFactoryInterface>&

talk/owt/sdk/include/cpp/owt/base/commontypes.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ struct OWT_EXPORT RtpEncodingParameters {
129129
NetworkPriority priority = NetworkPriority::kDefault;
130130
};
131131

132-
/// Audio encoding parameters.
132+
/// Audio encoding parameters. This API will be changed in the future to align
133+
/// with WebRTC APIs.
133134
struct OWT_EXPORT AudioEncodingParameters {
134135
explicit AudioEncodingParameters();
135136
AudioEncodingParameters(const AudioCodecParameters& codec_param,
@@ -148,11 +149,13 @@ struct OWT_EXPORT VideoCodecParameters {
148149
VideoCodecParameters(const VideoCodec& codec, const std::string& profile);
149150
virtual ~VideoCodecParameters();
150151
VideoCodec name;
152+
// profile is ignore at this time.
151153
std::string profile;
152154
};
153155

154156
/// Video encoding parameters. Used to specify the video encoding settings when
155-
/// publishing the video.
157+
/// publishing the video. This API will be changed in the future to align with
158+
/// WebRTC APIs.
156159
struct OWT_EXPORT VideoEncodingParameters {
157160
explicit VideoEncodingParameters();
158161
/// Construct an instance of VideoEncodingParameters

talk/owt/sdk/p2p/p2ppeerconnectionchannel.cc

Lines changed: 126 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include <vector>
66
#include "talk/owt/sdk/base/eventtrigger.h"
77
#include "talk/owt/sdk/base/functionalobserver.h"
8-
#include "talk/owt/sdk/base/sdputils.h"
98
#include "talk/owt/sdk/base/sysinfo.h"
109
#include "talk/owt/sdk/p2p/p2ppeerconnectionchannel.h"
1110
#include "webrtc/rtc_base/logging.h"
@@ -14,6 +13,23 @@
1413
using namespace rtc;
1514
namespace owt {
1615
namespace p2p {
16+
17+
static const std::
18+
unordered_map<owt::base::AudioCodec, const std::string, EnumClassHash>
19+
audio_codec_names = {{owt::base::AudioCodec::kOpus, "OPUS"},
20+
{owt::base::AudioCodec::kIsac, "ISAC"},
21+
{owt::base::AudioCodec::kG722, "G722"},
22+
{owt::base::AudioCodec::kPcmu, "PCMU"},
23+
{owt::base::AudioCodec::kIlbc, "ILBC"},
24+
{owt::base::AudioCodec::kPcma, "PCMA"}};
25+
static const std::
26+
unordered_map<owt::base::VideoCodec, const std::string, EnumClassHash>
27+
video_codec_names = {{owt::base::VideoCodec::kVp8, "VP8"},
28+
{owt::base::VideoCodec::kVp9, "VP9"},
29+
{owt::base::VideoCodec::kH264, "H264"},
30+
{owt::base::VideoCodec::kH265, "H265"},
31+
{owt::base::VideoCodec::kAv1, "AV1"}};
32+
1733
using std::string;
1834
enum P2PPeerConnectionChannel::SessionState : int {
1935
kSessionStateReady = 1, // Indicate the channel is ready. This is the initial state.
@@ -636,16 +652,6 @@ void P2PPeerConnectionChannel::OnSignalingChange(
636652
RTC_LOG(LS_ERROR) << "Error parsing local description.";
637653
RTC_DCHECK(false);
638654
}
639-
std::vector<AudioCodec> audio_codecs;
640-
for (auto& audio_enc_param : configuration_.audio) {
641-
audio_codecs.push_back(audio_enc_param.codec.name);
642-
}
643-
sdp_string = SdpUtils::SetPreferAudioCodecs(sdp_string, audio_codecs);
644-
std::vector<VideoCodec> video_codecs;
645-
for (auto& video_enc_param : configuration_.video) {
646-
video_codecs.push_back(video_enc_param.codec.name);
647-
}
648-
sdp_string = SdpUtils::SetPreferVideoCodecs(sdp_string, video_codecs);
649655
std::unique_ptr<webrtc::SessionDescriptionInterface> new_desc(
650656
webrtc::CreateSessionDescription(pending_remote_sdp_->type(),
651657
sdp_string, nullptr));
@@ -827,16 +833,6 @@ void P2PPeerConnectionChannel::OnCreateSessionDescriptionSuccess(
827833
RTC_LOG(LS_ERROR) << "Error parsing local description.";
828834
RTC_DCHECK(false);
829835
}
830-
std::vector<AudioCodec> audio_codecs;
831-
for (auto& audio_enc_param : configuration_.audio) {
832-
audio_codecs.push_back(audio_enc_param.codec.name);
833-
}
834-
sdp_string = SdpUtils::SetPreferAudioCodecs(sdp_string, audio_codecs);
835-
std::vector<VideoCodec> video_codecs;
836-
for (auto& video_enc_param : configuration_.video) {
837-
video_codecs.push_back(video_enc_param.codec.name);
838-
}
839-
sdp_string = SdpUtils::SetPreferVideoCodecs(sdp_string, video_codecs);
840836
webrtc::SessionDescriptionInterface* new_desc(
841837
webrtc::CreateSessionDescription(desc->type(), sdp_string, nullptr));
842838
peer_connection_->SetLocalDescription(observer.get(), new_desc);
@@ -1173,16 +1169,123 @@ void P2PPeerConnectionChannel::DrainPendingStreams() {
11731169
track_info[kTrackIdKey] = track->id();
11741170
track_info[kTrackSourceKey] = audio_track_source;
11751171
track_sources.append(track_info);
1176-
peer_connection_->AddTrack(track, {media_stream->id()});
1172+
1173+
webrtc::RtpTransceiverInit init;
1174+
init.direction = webrtc::RtpTransceiverDirection::kSendRecv;
1175+
init.stream_ids.push_back(media_stream->id());
1176+
if (configuration_.audio.size() > 0) {
1177+
// OWT APIs allow different bitrate settings for different codecs.
1178+
// However, this is not supported by WebRTC. We take the first
1179+
// codec's setting here. Consider to change OWT API in the future.
1180+
const auto& audio_encoding_paramters =
1181+
configuration_.audio[0].rtp_encoding_parameters;
1182+
std::vector<webrtc::RtpEncodingParameters>
1183+
rtp_encoding_parameters_list;
1184+
rtp_encoding_parameters_list.resize(audio_encoding_paramters.size());
1185+
std::transform(
1186+
audio_encoding_paramters.begin(), audio_encoding_paramters.end(),
1187+
rtp_encoding_parameters_list.begin(),
1188+
[](const RtpEncodingParameters& p) {
1189+
webrtc::RtpEncodingParameters encoding_paramters;
1190+
encoding_paramters.active = p.active;
1191+
if (p.max_bitrate_bps != 0) {
1192+
encoding_paramters.max_bitrate_bps = p.max_bitrate_bps;
1193+
}
1194+
if (p.max_framerate != 0) {
1195+
encoding_paramters.max_framerate = p.max_framerate;
1196+
}
1197+
if (p.rid != "") {
1198+
encoding_paramters.rid = p.rid;
1199+
}
1200+
return encoding_paramters;
1201+
});
1202+
init.send_encodings = rtp_encoding_parameters_list;
1203+
}
1204+
auto transceiver = AddTransceiver(track, init);
1205+
if (!configuration_.audio.empty() && transceiver.ok()) {
1206+
std::vector<webrtc::RtpCodecCapability> codecs;
1207+
auto capabilities =
1208+
PeerConnectionDependencyFactory::Get()->GetSenderCapabilities(
1209+
"audio");
1210+
for (const auto& audio : configuration_.audio) {
1211+
for (auto& c : capabilities->codecs) {
1212+
if (c.name != audio_codec_names.at(audio.codec.name)) {
1213+
continue;
1214+
}
1215+
if (audio.codec.channel_count != 0 &&
1216+
c.num_channels !=
1217+
static_cast<int>(audio.codec.channel_count)) {
1218+
continue;
1219+
}
1220+
codecs.push_back(c);
1221+
}
1222+
}
1223+
transceiver.value()->SetCodecPreferences(codecs);
1224+
}
11771225
}
11781226
for (const auto& track : media_stream->GetVideoTracks()) {
1227+
RTC_LOG(LS_INFO)<<"GetVideoTracks(), config empty: "<<configuration_.video.empty();
11791228
// Signaling.
11801229
stream_tracks.append(track->id());
11811230
stream_sources[kStreamVideoSourceKey] = video_track_source;
11821231
track_info[kTrackIdKey] = track->id();
11831232
track_info[kTrackSourceKey] = video_track_source;
11841233
track_sources.append(track_info);
1185-
peer_connection_->AddTrack(track, {media_stream->id()});
1234+
1235+
webrtc::RtpTransceiverInit init;
1236+
init.direction = webrtc::RtpTransceiverDirection::kSendRecv;
1237+
init.stream_ids.push_back(media_stream->id());
1238+
if (!configuration_.video.empty()) {
1239+
// OWT APIs allow different bitrate settings for different codecs.
1240+
// However, this is not supported by WebRTC. We take the first
1241+
// codec's setting here. Consider to change OWT API in the future.
1242+
const auto& video_encoding_paramters =
1243+
configuration_.video[0].rtp_encoding_parameters;
1244+
std::vector<webrtc::RtpEncodingParameters>
1245+
rtp_encoding_parameters_list;
1246+
rtp_encoding_parameters_list.resize(video_encoding_paramters.size());
1247+
std::transform(
1248+
video_encoding_paramters.begin(), video_encoding_paramters.end(),
1249+
rtp_encoding_parameters_list.begin(),
1250+
[](const RtpEncodingParameters& p) {
1251+
webrtc::RtpEncodingParameters encoding_paramters;
1252+
encoding_paramters.active = p.active;
1253+
if (p.max_bitrate_bps != 0) {
1254+
encoding_paramters.max_bitrate_bps = p.max_bitrate_bps;
1255+
}
1256+
if (p.max_framerate != 0) {
1257+
encoding_paramters.max_framerate = p.max_framerate;
1258+
}
1259+
if (p.rid != "") {
1260+
encoding_paramters.rid = p.rid;
1261+
}
1262+
if (p.num_temporal_layers != 0) {
1263+
encoding_paramters.num_temporal_layers =
1264+
p.num_temporal_layers;
1265+
}
1266+
if (p.scale_resolution_down_by != 0) {
1267+
encoding_paramters.scale_resolution_down_by =
1268+
p.scale_resolution_down_by;
1269+
}
1270+
return encoding_paramters;
1271+
});
1272+
init.send_encodings = rtp_encoding_parameters_list;
1273+
}
1274+
auto transceiver = AddTransceiver(track, init);
1275+
if (!configuration_.video.empty() && transceiver.ok()) {
1276+
std::vector<webrtc::RtpCodecCapability> codecs;
1277+
auto capabilities =
1278+
PeerConnectionDependencyFactory::Get()->GetSenderCapabilities(
1279+
"video");
1280+
for (const auto& video : configuration_.video) {
1281+
for (auto& c : capabilities->codecs) {
1282+
if (c.name == video_codec_names.at(video.codec.name)) {
1283+
codecs.push_back(c);
1284+
}
1285+
}
1286+
}
1287+
transceiver.value()->SetCodecPreferences(codecs);
1288+
}
11861289
}
11871290
// The second signaling message of track sources to remote peer.
11881291
Json::Value json_track_sources;

0 commit comments

Comments
 (0)