• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // VoipBase related operations with rtc::Thread (voip_thread_), this
40 // is to comply with consistent thread usage requirement with
41 // ProcessThread used within VoipEngine. AndroidVoipClient is meant
42 // 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 
55   ~AndroidVoipClient() override;
56 
57   // Returns a Java List of Strings containing names of the built-in
58   // supported codecs.
59   webrtc::ScopedJavaLocalRef<jobject> GetSupportedCodecs(JNIEnv* env);
60 
61   // Returns a Java String of the default local IPv4 address. If IPv4
62   // address is not found, returns the default local IPv6 address. If
63   // IPv6 address is not found, returns an empty string.
64   webrtc::ScopedJavaLocalRef<jstring> GetLocalIPAddress(JNIEnv* env);
65 
66   // Sets the encoder used by the VoIP API.
67   void SetEncoder(JNIEnv* env,
68                   const webrtc::JavaRef<jstring>& j_encoder_string);
69 
70   // Sets the decoders used by the VoIP API.
71   void SetDecoders(JNIEnv* env,
72                    const webrtc::JavaParamRef<jobject>& j_decoder_strings);
73 
74   // Sets two local/remote addresses, one for RTP packets, and another for
75   // RTCP packets. The RTP address will have IP address j_ip_address_string
76   // and port number j_port_number_int, the RTCP address will have IP address
77   // j_ip_address_string and port number j_port_number_int+1.
78   void SetLocalAddress(JNIEnv* env,
79                        const webrtc::JavaRef<jstring>& j_ip_address_string,
80                        jint j_port_number_int);
81   void SetRemoteAddress(JNIEnv* env,
82                         const webrtc::JavaRef<jstring>& j_ip_address_string,
83                         jint j_port_number_int);
84 
85   // Starts a VoIP session. The VoIP operations below can only be
86   // used after a session has already started. Returns true if session
87   // started successfully and false otherwise.
88   jboolean StartSession(JNIEnv* env);
89 
90   // Stops the current session. Returns true if session stopped
91   // successfully and false otherwise.
92   jboolean StopSession(JNIEnv* env);
93 
94   // Starts sending RTP/RTCP packets to the remote endpoint. Returns
95   // the return value of StartSend in api/voip/voip_base.h.
96   jboolean StartSend(JNIEnv* env);
97 
98   // Stops sending RTP/RTCP packets to the remote endpoint. Returns
99   // the return value of StopSend in api/voip/voip_base.h.
100   jboolean StopSend(JNIEnv* env);
101 
102   // Starts playing out the voice data received from the remote endpoint.
103   // Returns the return value of StartPlayout in api/voip/voip_base.h.
104   jboolean StartPlayout(JNIEnv* env);
105 
106   // Stops playing out the voice data received from the remote endpoint.
107   // Returns the return value of StopPlayout in api/voip/voip_base.h.
108   jboolean StopPlayout(JNIEnv* env);
109 
110   // Deletes this object. Used by client when they are done.
111   void Delete(JNIEnv* env);
112 
113   // Implementation for Transport.
114   bool SendRtp(const uint8_t* packet,
115                size_t length,
116                const webrtc::PacketOptions& options) override;
117   bool SendRtcp(const uint8_t* packet, size_t length) override;
118 
119   // Slots for sockets to connect to.
120   void OnSignalReadRTPPacket(rtc::AsyncPacketSocket* socket,
121                              const char* rtp_packet,
122                              size_t size,
123                              const rtc::SocketAddress& addr,
124                              const int64_t& timestamp);
125   void OnSignalReadRTCPPacket(rtc::AsyncPacketSocket* socket,
126                               const char* rtcp_packet,
127                               size_t size,
128                               const rtc::SocketAddress& addr,
129                               const int64_t& timestamp);
130 
131  private:
132   AndroidVoipClient(JNIEnv* env,
133                     const webrtc::JavaParamRef<jobject>& application_context);
134 
135   // Used to invoke VoipBase operations and send/receive
136   // RTP/RTCP packets.
137   std::unique_ptr<rtc::Thread> voip_thread_;
138   // A list of AudioCodecSpec supported by the built-in
139   // encoder/decoder factories.
140   std::vector<webrtc::AudioCodecSpec> supported_codecs_;
141   // The entry point to all VoIP APIs.
142   std::unique_ptr<webrtc::VoipEngine> voip_engine_;
143   // Used by the VoIP API to facilitate a VoIP session.
144   absl::optional<webrtc::ChannelId> channel_;
145   // Members below are used for network related operations.
146   std::unique_ptr<rtc::AsyncUDPSocket> rtp_socket_;
147   std::unique_ptr<rtc::AsyncUDPSocket> rtcp_socket_;
148   rtc::SocketAddress rtp_local_address_;
149   rtc::SocketAddress rtcp_local_address_;
150   rtc::SocketAddress rtp_remote_address_;
151   rtc::SocketAddress rtcp_remote_address_;
152 };
153 
154 }  // namespace webrtc_examples
155 
156 #endif  // EXAMPLES_ANDROIDVOIP_JNI_ANDROID_VOIP_CLIENT_H_
157