1 package com.example.imsmediatestingapp;
2 
3 import android.telephony.imsmedia.AmrParams;
4 import android.telephony.imsmedia.AudioConfig;
5 import android.telephony.imsmedia.EvsParams;
6 import android.telephony.imsmedia.VideoConfig;
7 
8 import androidx.annotation.NonNull;
9 
10 import java.io.Serializable;
11 import java.net.InetAddress;
12 import java.util.Arrays;
13 import java.util.HashSet;
14 import java.util.Locale;
15 import java.util.Set;
16 import java.util.StringJoiner;
17 
18 /**
19  * The DeviceInfo class stores the information about a device's connection
20  * details, so it can be
21  * quickly and easily sent through DatagramPackets between devices. Uses the
22  * Builder pattern to
23  * more easily create and change variables.
24  */
25 public class DeviceInfo implements Serializable {
26     private final InetAddress mInetAddress;
27     private final Set<Integer> mAudioCodecs;
28     private final Set<Integer> mAmrModes;
29     private final Set<Integer> mEvsBandwidths;
30     private final Set<Integer> mEvsModes;
31     private final int mVideoCodec;
32     private final int mHandshakePort;
33     private final int mAudioRtpPort;
34     private final int mVideoRtpPort;
35     private final int mTextRtpPort;
36     private final int mVideoResolutionWidth;
37     private final int mVideoResolutionHeight;
38     private final int mVideoCvoValue;
39     private final Set<Integer> mRtcpFbTypes;
40 
DeviceInfo(InetAddress inetSocketAddress, Set<Integer> audioCodecs, Set<Integer> amrModes, Set<Integer> evsBandwidths, Set<Integer> evsModes, int videoCodec, int handshakePort, int audioRtpPort, int videoRtpPort, int textRtpPort, int videoResolutionWidth, int videoResolutionHeight, int videoCvoValue, Set<Integer> rtcpFbTypes)41     private DeviceInfo(InetAddress inetSocketAddress,
42             Set<Integer> audioCodecs, Set<Integer> amrModes, Set<Integer> evsBandwidths,
43             Set<Integer> evsModes, int videoCodec, int handshakePort, int audioRtpPort,
44             int videoRtpPort, int textRtpPort, int videoResolutionWidth, int videoResolutionHeight,
45             int videoCvoValue, Set<Integer> rtcpFbTypes) {
46         this.mInetAddress = inetSocketAddress;
47         this.mAudioCodecs = audioCodecs;
48         this.mAmrModes = amrModes;
49         this.mEvsBandwidths = evsBandwidths;
50         this.mEvsModes = evsModes;
51         this.mVideoCodec = videoCodec;
52         this.mHandshakePort = handshakePort;
53         this.mAudioRtpPort = audioRtpPort;
54         this.mVideoRtpPort = videoRtpPort;
55         this.mTextRtpPort = textRtpPort;
56         this.mVideoResolutionWidth = videoResolutionWidth;
57         this.mVideoResolutionHeight = videoResolutionHeight;
58         this.mVideoCvoValue = videoCvoValue;
59         this.mRtcpFbTypes = rtcpFbTypes;
60     }
61 
62     @NonNull
toString()63     public String toString() {
64         return String.format(Locale.US,
65                 "IP Address: %s\nHandshake Port: %d\nAudioRTP Port: %d\nVideoRTP Port: %d\n"
66                         + "Selected Audio Codecs: %s\nSelected AMR Modes: %s\nSelected "
67                         + "EVS Bandwidths: %s\nSelected EVS Modes: %s\nSelected Video Codec: %d"
68                         + "VideoWidth: %s\n VideoHeight: %s\n Cvo: %d\n rtcpFbTypes: %s\n",
69                 mInetAddress.getHostName(), getHandshakePort(),
70                 getAudioRtpPort(), getVideoRtpPort(),
71                 getAudioCodecsToString(), getAmrModesToString(),
72                 getEvsBandwidthsToString(), getEvsModesToString(),
73                 getVideoCodec(), getVideoResolutionWidth(), getVideoResolutionHeight(),
74                 getVideoCvoValue(), getRtcpFbTypesToString());
75     }
76 
getHandshakePort()77     public int getHandshakePort() {
78         return mHandshakePort;
79     }
80 
getAudioRtpPort()81     public int getAudioRtpPort() {
82         return mAudioRtpPort;
83     }
84 
getVideoRtpPort()85     public int getVideoRtpPort() {
86         return mVideoRtpPort;
87     }
88 
getTextRtpPort()89     public int getTextRtpPort() {
90         return mTextRtpPort;
91     }
92 
getAudioCodecs()93     public Set<Integer> getAudioCodecs() {
94         return mAudioCodecs;
95     }
96 
getEvsBandwidths()97     public Set<Integer> getEvsBandwidths() {
98         return mEvsBandwidths;
99     }
100 
getVideoCodec()101     public int getVideoCodec() {
102         return mVideoCodec;
103     }
104 
getEvsModes()105     public Set<Integer> getEvsModes() {
106         return mEvsModes;
107     }
108 
getAmrModes()109     public Set<Integer> getAmrModes() {
110         return mAmrModes;
111     }
112 
getInetAddress()113     public InetAddress getInetAddress() {
114         return mInetAddress;
115     }
116 
getAudioCodecsToString()117     private String getAudioCodecsToString() {
118         StringJoiner joiner = new StringJoiner(",");
119         mAudioCodecs.forEach(item -> joiner.add(item.toString()));
120         return joiner.toString();
121     }
122 
getAmrModesToString()123     private String getAmrModesToString() {
124         StringJoiner joiner = new StringJoiner(",");
125         mAmrModes.forEach(item -> joiner.add(item.toString()));
126         return joiner.toString();
127     }
128 
getEvsBandwidthsToString()129     private String getEvsBandwidthsToString() {
130         StringJoiner joiner = new StringJoiner(",");
131         mEvsBandwidths.forEach(item -> joiner.add(item.toString()));
132         return joiner.toString();
133     }
134 
getEvsModesToString()135     private String getEvsModesToString() {
136         StringJoiner joiner = new StringJoiner(",");
137         mEvsModes.forEach(item -> joiner.add(item.toString()));
138         return joiner.toString();
139     }
140 
getVideoResolutionWidth()141     public int getVideoResolutionWidth() {
142         return mVideoResolutionWidth;
143     }
144 
getVideoResolutionHeight()145     public int getVideoResolutionHeight() {
146         return mVideoResolutionHeight;
147     }
148 
getVideoCvoValue()149     public int getVideoCvoValue() {
150         return mVideoCvoValue;
151     }
152 
getRtcpFbTypes()153     public Set<Integer> getRtcpFbTypes() {
154         return mRtcpFbTypes;
155     }
156 
getRtcpFbTypesToString()157     private String getRtcpFbTypesToString() {
158         StringJoiner joiner = new StringJoiner(",");
159         mRtcpFbTypes.forEach(item -> joiner.add(item.toString()));
160         return joiner.toString();
161     }
162 
163     public static final class Builder {
164         private InetAddress mInetAddress;
165         private Set<Integer> mAudioCodecs = new HashSet<>(Arrays.asList(AudioConfig.CODEC_AMR,
166                 AudioConfig.CODEC_AMR_WB, AudioConfig.CODEC_EVS, AudioConfig.CODEC_PCMA,
167                 AudioConfig.CODEC_PCMU));
168         private Set<Integer> mAmrModes = new HashSet<>(Arrays.asList(AmrParams.AMR_MODE_0,
169                 AmrParams.AMR_MODE_1, AmrParams.AMR_MODE_2, AmrParams.AMR_MODE_3,
170                 AmrParams.AMR_MODE_4, AmrParams.AMR_MODE_5, AmrParams.AMR_MODE_6,
171                 AmrParams.AMR_MODE_7, AmrParams.AMR_MODE_8));
172         private Set<Integer> mEvsBandwidths = new HashSet<>(Arrays.asList(EvsParams.EVS_BAND_NONE,
173                 EvsParams.EVS_NARROW_BAND, EvsParams.EVS_WIDE_BAND, EvsParams.EVS_SUPER_WIDE_BAND,
174                 EvsParams.EVS_FULL_BAND));
175         private Set<Integer> mEvsModes = new HashSet<>(Arrays.asList(EvsParams.EVS_MODE_0,
176                 EvsParams.EVS_MODE_1, EvsParams.EVS_MODE_2, EvsParams.EVS_MODE_3,
177                 EvsParams.EVS_MODE_4, EvsParams.EVS_MODE_5, EvsParams.EVS_MODE_6,
178                 EvsParams.EVS_MODE_7, EvsParams.EVS_MODE_8, EvsParams.EVS_MODE_9,
179                 EvsParams.EVS_MODE_10, EvsParams.EVS_MODE_11, EvsParams.EVS_MODE_12,
180                 EvsParams.EVS_MODE_13, EvsParams.EVS_MODE_14, EvsParams.EVS_MODE_15,
181                 EvsParams.EVS_MODE_16, EvsParams.EVS_MODE_17, EvsParams.EVS_MODE_18,
182                 EvsParams.EVS_MODE_19, EvsParams.EVS_MODE_20));
183         private int mVideoCodec;
184         private int mHandshakePort;
185         private int mAudioRtpPort;
186         private int mVideoRtpPort;
187         private int mTextRtpPort;
188         private int mVideoResolutionWidth;
189         private int mVideoResolutionHeight;
190         private int mVideoCvoValue;
191         private Set<Integer> mRtcpFbTypes = new HashSet<>(Arrays.asList(VideoConfig.RTP_FB_NONE,
192                 VideoConfig.RTPFB_NACK, VideoConfig.RTPFB_TMMBR, VideoConfig.RTPFB_TMMBN,
193                 VideoConfig.PSFB_PLI, VideoConfig.PSFB_FIR));
194 
Builder()195         public Builder() {
196         }
197 
198         @NonNull
setInetAddress(InetAddress inetAddress)199         public DeviceInfo.Builder setInetAddress(InetAddress inetAddress) {
200             this.mInetAddress = inetAddress;
201             return this;
202         }
203 
204         @NonNull
setAudioCodecs(Set<Integer> audioCodecs)205         public DeviceInfo.Builder setAudioCodecs(Set<Integer> audioCodecs) {
206             if (!audioCodecs.isEmpty()) {
207                 this.mAudioCodecs = audioCodecs;
208             }
209             return this;
210         }
211 
setAmrModes(Set<Integer> amrModes)212         public DeviceInfo.Builder setAmrModes(Set<Integer> amrModes) {
213             if (!amrModes.isEmpty()) {
214                 this.mAmrModes = amrModes;
215             }
216             return this;
217         }
218 
setEvsBandwidths(Set<Integer> evsBandwidths)219         public DeviceInfo.Builder setEvsBandwidths(Set<Integer> evsBandwidths) {
220             if (!evsBandwidths.isEmpty()) {
221                 this.mEvsBandwidths = evsBandwidths;
222             }
223             return this;
224         }
225 
setEvsModes(Set<Integer> evsModes)226         public DeviceInfo.Builder setEvsModes(Set<Integer> evsModes) {
227             if (!evsModes.isEmpty()) {
228                 this.mEvsModes = evsModes;
229             }
230             return this;
231         }
232 
setVideoCodec(int videoCodec)233         public DeviceInfo.Builder setVideoCodec(int videoCodec) {
234             this.mVideoCodec = videoCodec;
235             return this;
236         }
237 
238         @NonNull
setHandshakePort(int handshakePort)239         public DeviceInfo.Builder setHandshakePort(int handshakePort) {
240             this.mHandshakePort = handshakePort;
241             return this;
242         }
243 
244         @NonNull
setAudioRtpPort(int audioRtpPort)245         public DeviceInfo.Builder setAudioRtpPort(int audioRtpPort) {
246             this.mAudioRtpPort = audioRtpPort;
247             return this;
248         }
249 
250         @NonNull
setVideoRtpPort(int videoRtpPort)251         public DeviceInfo.Builder setVideoRtpPort(int videoRtpPort) {
252             this.mVideoRtpPort = videoRtpPort;
253             return this;
254         }
255 
256         @NonNull
setTextRtpPort(int textRtpPort)257         public DeviceInfo.Builder setTextRtpPort(int textRtpPort) {
258             this.mTextRtpPort = textRtpPort;
259             return this;
260         }
261 
setVideoResolutionWidth(int width)262         public DeviceInfo.Builder setVideoResolutionWidth(int width) {
263             this.mVideoResolutionWidth = width;
264             return this;
265         }
266 
setVideoResolutionHeight(int height)267         public DeviceInfo.Builder setVideoResolutionHeight(int height) {
268             this.mVideoResolutionHeight = height;
269             return this;
270         }
271 
setVideoCvoValue(int cvo)272         public DeviceInfo.Builder setVideoCvoValue(int cvo) {
273             this.mVideoCvoValue = cvo;
274             return this;
275         }
276 
setRtcpFbTypes(Set<Integer> rtcpFb)277         public DeviceInfo.Builder setRtcpFbTypes(Set<Integer> rtcpFb) {
278             if (!rtcpFb.isEmpty()) {
279                 this.mRtcpFbTypes = rtcpFb;
280             }
281             return this;
282         }
283 
284         @NonNull
build()285         public DeviceInfo build() {
286             return new DeviceInfo(mInetAddress, mAudioCodecs, mAmrModes, mEvsBandwidths, mEvsModes,
287                     mVideoCodec, mHandshakePort, mAudioRtpPort, mVideoRtpPort, mTextRtpPort,
288                     mVideoResolutionWidth, mVideoResolutionHeight, mVideoCvoValue, mRtcpFbTypes);
289         }
290     }
291 }
292