android_voip_client.h 8.2 KB

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