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 #ifndef EXAMPLES_ANDROIDVOIP_JNI_ANDROID_VOIP_CLIENT_H_ 12 #define EXAMPLES_ANDROIDVOIP_JNI_ANDROID_VOIP_CLIENT_H_ 13 14 #include <jni.h> 15 16 #include <memory> 17 #include <string> 18 #include <vector> 19 20 #include "api/audio_codecs/audio_format.h" 21 #include "api/call/transport.h" 22 #include "api/voip/voip_base.h" 23 #include "api/voip/voip_engine.h" 24 #include "rtc_base/async_packet_socket.h" 25 #include "rtc_base/async_udp_socket.h" 26 #include "rtc_base/socket_address.h" 27 #include "rtc_base/third_party/sigslot/sigslot.h" 28 #include "rtc_base/thread.h" 29 #include "sdk/android/native_api/jni/scoped_java_ref.h" 30 31 namespace webrtc_examples { 32 33 // AndroidVoipClient facilitates the use of the VoIP API defined in 34 // api/voip/voip_engine.h. One instance of AndroidVoipClient should 35 // suffice for most VoIP applications. AndroidVoipClient implements 36 // webrtc::Transport to send RTP/RTCP packets to the remote endpoint. 37 // It also creates methods (slots) for sockets to connect to in 38 // order to receive RTP/RTCP packets. AndroidVoipClient does all 39 // operations with rtc::Thread (voip_thread_), this is to comply 40 // with consistent thread usage requirement with ProcessThread used 41 // within VoipEngine, as well as providing asynchronicity to the 42 // caller. AndroidVoipClient is meant to be used by Java through JNI. 43 class AndroidVoipClient : public webrtc::Transport, 44 public sigslot::has_slots<> { 45 public: 46 // Returns a pointer to an AndroidVoipClient object. Clients should 47 // use this factory method to create AndroidVoipClient objects. The 48 // method will return a nullptr in case of initialization errors. 49 // It is the client's responsibility to delete the pointer when 50 // they are done with it (this class provides a Delete() method). 51 static AndroidVoipClient* Create( 52 JNIEnv* env, 53 const webrtc::JavaParamRef<jobject>& application_context, 54 const webrtc::JavaParamRef<jobject>& j_voip_client); 55 56 ~AndroidVoipClient() override; 57 58 // Provides client with a Java List of Strings containing names of 59 // the built-in supported codecs through callback. 60 void GetSupportedCodecs(JNIEnv* env); 61 62 // Provides client with a Java String of the default local IPv4 address 63 // through callback. If IPv4 address is not found, provide the default 64 // local IPv6 address. If IPv6 address is not found, provide an empty 65 // string. 66 void GetLocalIPAddress(JNIEnv* env); 67 68 // Sets the encoder used by the VoIP API. 69 void SetEncoder(JNIEnv* env, 70 const webrtc::JavaParamRef<jstring>& j_encoder_string); 71 72 // Sets the decoders used by the VoIP API. 73 void SetDecoders(JNIEnv* env, 74 const webrtc::JavaParamRef<jobject>& j_decoder_strings); 75 76 // Sets two local/remote addresses, one for RTP packets, and another for 77 // RTCP packets. The RTP address will have IP address j_ip_address_string 78 // and port number j_port_number_int, the RTCP address will have IP address 79 // j_ip_address_string and port number j_port_number_int+1. 80 void SetLocalAddress(JNIEnv* env, 81 const webrtc::JavaParamRef<jstring>& j_ip_address_string, 82 jint j_port_number_int); 83 void SetRemoteAddress( 84 JNIEnv* env, 85 const webrtc::JavaParamRef<jstring>& j_ip_address_string, 86 jint j_port_number_int); 87 88 // Starts a VoIP session, then calls a callback method with a boolean 89 // value indicating if the session has started successfully. The VoIP 90 // operations below can only be used after a session has already started. 91 void StartSession(JNIEnv* env); 92 93 // Stops the current session, then calls a callback method with a 94 // boolean value indicating if the session has stopped successfully. 95 void StopSession(JNIEnv* env); 96 97 // Starts sending RTP/RTCP packets to the remote endpoint, then calls 98 // a callback method with a boolean value indicating if sending 99 // has started successfully. 100 void StartSend(JNIEnv* env); 101 102 // Stops sending RTP/RTCP packets to the remote endpoint, then calls 103 // a callback method with a boolean value indicating if sending 104 // has stopped successfully. 105 void StopSend(JNIEnv* env); 106 107 // Starts playing out the voice data received from the remote endpoint, 108 // then calls a callback method with a boolean value indicating if 109 // playout has started successfully. 110 void StartPlayout(JNIEnv* env); 111 112 // Stops playing out the voice data received from the remote endpoint, 113 // then calls a callback method with a boolean value indicating if 114 // playout has stopped successfully. 115 void StopPlayout(JNIEnv* env); 116 117 // Deletes this object. Used by client when they are done. 118 void Delete(JNIEnv* env); 119 120 // Implementation for Transport. 121 bool SendRtp(const uint8_t* packet, 122 size_t length, 123 const webrtc::PacketOptions& options) override; 124 bool SendRtcp(const uint8_t* packet, size_t length) override; 125 126 // Slots for sockets to connect to. 127 void OnSignalReadRTPPacket(rtc::AsyncPacketSocket* socket, 128 const char* rtp_packet, 129 size_t size, 130 const rtc::SocketAddress& addr, 131 const int64_t& timestamp); 132 void OnSignalReadRTCPPacket(rtc::AsyncPacketSocket* socket, 133 const char* rtcp_packet, 134 size_t size, 135 const rtc::SocketAddress& addr, 136 const int64_t& timestamp); 137 138 private: AndroidVoipClient(JNIEnv * env,const webrtc::JavaParamRef<jobject> & j_voip_client)139 AndroidVoipClient(JNIEnv* env, 140 const webrtc::JavaParamRef<jobject>& j_voip_client) 141 : voip_thread_(rtc::Thread::CreateWithSocketServer()), 142 j_voip_client_(env, j_voip_client) {} 143 144 void Init(JNIEnv* env, 145 const webrtc::JavaParamRef<jobject>& application_context); 146 147 // Overloaded methods having native C++ variables as arguments. 148 void SetEncoder(const std::string& encoder); 149 void SetDecoders(const std::vector<std::string>& decoders); 150 void SetLocalAddress(const std::string& ip_address, int port_number); 151 void SetRemoteAddress(const std::string& ip_address, int port_number); 152 153 // Methods to send and receive RTP/RTCP packets. Takes in a 154 // copy of a packet as a vector to prolong the lifetime of 155 // the packet as these methods will be called asynchronously. 156 void SendRtpPacket(const std::vector<uint8_t>& packet_copy); 157 void SendRtcpPacket(const std::vector<uint8_t>& packet_copy); 158 void ReadRTPPacket(const std::vector<uint8_t>& packet_copy); 159 void ReadRTCPPacket(const std::vector<uint8_t>& packet_copy); 160 161 // Used to invoke operations and send/receive RTP/RTCP packets. 162 std::unique_ptr<rtc::Thread> voip_thread_; 163 // Reference to the VoipClient java instance used to 164 // invoke callbacks when operations are finished. 165 webrtc::ScopedJavaGlobalRef<jobject> j_voip_client_ 166 RTC_GUARDED_BY(voip_thread_); 167 // A list of AudioCodecSpec supported by the built-in 168 // encoder/decoder factories. 169 std::vector<webrtc::AudioCodecSpec> supported_codecs_ 170 RTC_GUARDED_BY(voip_thread_); 171 // A JNI context used by the voip_thread_. 172 JNIEnv* env_ RTC_GUARDED_BY(voip_thread_); 173 // The entry point to all VoIP APIs. 174 std::unique_ptr<webrtc::VoipEngine> voip_engine_ RTC_GUARDED_BY(voip_thread_); 175 // Used by the VoIP API to facilitate a VoIP session. 176 absl::optional<webrtc::ChannelId> channel_ RTC_GUARDED_BY(voip_thread_); 177 // Members below are used for network related operations. 178 std::unique_ptr<rtc::AsyncUDPSocket> rtp_socket_ RTC_GUARDED_BY(voip_thread_); 179 std::unique_ptr<rtc::AsyncUDPSocket> rtcp_socket_ 180 RTC_GUARDED_BY(voip_thread_); 181 rtc::SocketAddress rtp_local_address_ RTC_GUARDED_BY(voip_thread_); 182 rtc::SocketAddress rtcp_local_address_ RTC_GUARDED_BY(voip_thread_); 183 rtc::SocketAddress rtp_remote_address_ RTC_GUARDED_BY(voip_thread_); 184 rtc::SocketAddress rtcp_remote_address_ RTC_GUARDED_BY(voip_thread_); 185 }; 186 187 } // namespace webrtc_examples 188 189 #endif // EXAMPLES_ANDROIDVOIP_JNI_ANDROID_VOIP_CLIENT_H_ 190