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