1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "host/frontend/webrtc/libcommon/peer_connection_utils.h"
18 
19 #include <api/audio_codecs/audio_decoder_factory.h>
20 #include <api/audio_codecs/audio_encoder_factory.h>
21 #include <api/audio_codecs/builtin_audio_decoder_factory.h>
22 #include <api/audio_codecs/builtin_audio_encoder_factory.h>
23 #include <api/create_peerconnection_factory.h>
24 #include <api/peer_connection_interface.h>
25 #include <api/video_codecs/builtin_video_decoder_factory.h>
26 #include <api/video_codecs/builtin_video_encoder_factory.h>
27 #include <api/video_codecs/video_decoder_factory.h>
28 #include <api/video_codecs/video_encoder_factory.h>
29 
30 #include "host/frontend/webrtc/libcommon/audio_device.h"
31 #include "host/frontend/webrtc/libcommon/vp8only_encoder_factory.h"
32 
33 namespace cuttlefish {
34 namespace webrtc_streaming {
35 
CreateAndStartThread(const std::string & name)36 Result<std::unique_ptr<rtc::Thread>> CreateAndStartThread(
37     const std::string& name) {
38   auto thread = rtc::Thread::CreateWithSocketServer();
39   CF_EXPECTF(thread.get(), "Failed to create \"{}\" thread", name);
40   thread->SetName(name, nullptr);
41   CF_EXPECTF(thread->Start(), "Failed to start \"{}\" thread", name);
42   return thread;
43 }
44 
45 Result<rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>>
CreatePeerConnectionFactory(rtc::Thread * network_thread,rtc::Thread * worker_thread,rtc::Thread * signal_thread,rtc::scoped_refptr<webrtc::AudioDeviceModule> audio_device_module)46 CreatePeerConnectionFactory(
47     rtc::Thread* network_thread, rtc::Thread* worker_thread,
48     rtc::Thread* signal_thread,
49     rtc::scoped_refptr<webrtc::AudioDeviceModule> audio_device_module) {
50   auto peer_connection_factory = webrtc::CreatePeerConnectionFactory(
51       network_thread, worker_thread, signal_thread, audio_device_module,
52       webrtc::CreateBuiltinAudioEncoderFactory(),
53       webrtc::CreateBuiltinAudioDecoderFactory(),
54       // Only VP8 is supported
55       std::make_unique<VP8OnlyEncoderFactory>(
56           webrtc::CreateBuiltinVideoEncoderFactory()),
57       webrtc::CreateBuiltinVideoDecoderFactory(), nullptr /* audio_mixer */,
58       nullptr /* audio_processing */);
59   CF_EXPECT(peer_connection_factory.get(),
60             "Failed to create peer connection factory");
61 
62   webrtc::PeerConnectionFactoryInterface::Options options;
63   // By default the loopback network is ignored, but generating candidates for
64   // it is useful when using TCP port forwarding.
65   options.network_ignore_mask = 0;
66   peer_connection_factory->SetOptions(options);
67 
68   return peer_connection_factory;
69 }
70 
71 Result<rtc::scoped_refptr<webrtc::PeerConnectionInterface>>
CreatePeerConnection(rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> peer_connection_factory,webrtc::PeerConnectionDependencies dependencies,uint16_t min_port,uint16_t max_port,const std::vector<webrtc::PeerConnectionInterface::IceServer> & servers)72 CreatePeerConnection(
73     rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
74         peer_connection_factory,
75     webrtc::PeerConnectionDependencies dependencies,
76     uint16_t min_port, uint16_t max_port,
77     const std::vector<webrtc::PeerConnectionInterface::IceServer>& servers) {
78   webrtc::PeerConnectionInterface::RTCConfiguration config;
79   config.sdp_semantics = webrtc::SdpSemantics::kUnifiedPlan;
80   config.servers.insert(config.servers.end(), servers.begin(), servers.end());
81   config.set_min_port(min_port);
82   config.set_max_port(max_port);
83   auto result = peer_connection_factory->CreatePeerConnectionOrError(
84       config, std::move(dependencies));
85 
86   CF_EXPECT(result.ok(),
87             "Failed to create peer connection: " << result.error().message());
88   return result.MoveValue();
89 }
90 
91 }  // namespace webrtc_streaming
92 }  // namespace cuttlefish
93