• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.net.rtp;
18 
19 import java.net.InetAddress;
20 import java.net.Inet4Address;
21 import java.net.Inet6Address;
22 import java.net.SocketException;
23 
24 /**
25  * RtpStream represents the base class of streams which send and receive network
26  * packets with media payloads over Real-time Transport Protocol (RTP).
27  *
28  * <p class="note">Using this class requires
29  * {@link android.Manifest.permission#INTERNET} permission.</p>
30  * @deprecated {@link android.net.sip.SipManager} and associated classes are no longer supported and
31  * should not be used as the basis of future VOIP apps.
32  */
33 public class RtpStream {
34     /**
35      * This mode indicates that the stream sends and receives packets at the
36      * same time. This is the initial mode for new streams.
37      */
38     public static final int MODE_NORMAL = 0;
39 
40     /**
41      * This mode indicates that the stream only sends packets.
42      */
43     public static final int MODE_SEND_ONLY = 1;
44 
45     /**
46      * This mode indicates that the stream only receives packets.
47      */
48     public static final int MODE_RECEIVE_ONLY = 2;
49 
50     private static final int MODE_LAST = 2;
51 
52     private final InetAddress mLocalAddress;
53     private final int mLocalPort;
54 
55     private InetAddress mRemoteAddress;
56     private int mRemotePort = -1;
57     private int mMode = MODE_NORMAL;
58 
59     private int mSocket = -1;
60     static {
61         System.loadLibrary("rtp_jni");
62     }
63 
64     /**
65      * Creates a RtpStream on the given local address. Note that the local
66      * port is assigned automatically to conform with RFC 3550.
67      *
68      * @param address The network address of the local host to bind to.
69      * @throws SocketException if the address cannot be bound or a problem
70      *     occurs during binding.
71      */
RtpStream(InetAddress address)72     RtpStream(InetAddress address) throws SocketException {
73         mLocalPort = create(address.getHostAddress());
74         mLocalAddress = address;
75     }
76 
create(String address)77     private native int create(String address) throws SocketException;
78 
79     /**
80      * Returns the network address of the local host.
81      */
getLocalAddress()82     public InetAddress getLocalAddress() {
83         return mLocalAddress;
84     }
85 
86     /**
87      * Returns the network port of the local host.
88      */
getLocalPort()89     public int getLocalPort() {
90         return mLocalPort;
91     }
92 
93     /**
94      * Returns the network address of the remote host or {@code null} if the
95      * stream is not associated.
96      */
getRemoteAddress()97     public InetAddress getRemoteAddress() {
98         return mRemoteAddress;
99     }
100 
101     /**
102      * Returns the network port of the remote host or {@code -1} if the stream
103      * is not associated.
104      */
getRemotePort()105     public int getRemotePort() {
106         return mRemotePort;
107     }
108 
109     /**
110      * Returns {@code true} if the stream is busy. In this case most of the
111      * setter methods are disabled. This method is intended to be overridden
112      * by subclasses.
113      */
isBusy()114     public boolean isBusy() {
115         return false;
116     }
117 
118     /**
119      * Returns the current mode.
120      */
getMode()121     public int getMode() {
122         return mMode;
123     }
124 
125     /**
126      * Changes the current mode. It must be one of {@link #MODE_NORMAL},
127      * {@link #MODE_SEND_ONLY}, and {@link #MODE_RECEIVE_ONLY}.
128      *
129      * @param mode The mode to change to.
130      * @throws IllegalArgumentException if the mode is invalid.
131      * @throws IllegalStateException if the stream is busy.
132      * @see #isBusy()
133      */
setMode(int mode)134     public void setMode(int mode) {
135         if (isBusy()) {
136             throw new IllegalStateException("Busy");
137         }
138         if (mode < 0 || mode > MODE_LAST) {
139             throw new IllegalArgumentException("Invalid mode");
140         }
141         mMode = mode;
142     }
143 
144     /**
145      * Associates with a remote host. This defines the destination of the
146      * outgoing packets.
147      *
148      * @param address The network address of the remote host.
149      * @param port The network port of the remote host.
150      * @throws IllegalArgumentException if the address is not supported or the
151      *     port is invalid.
152      * @throws IllegalStateException if the stream is busy.
153      * @see #isBusy()
154      */
associate(InetAddress address, int port)155     public void associate(InetAddress address, int port) {
156         if (isBusy()) {
157             throw new IllegalStateException("Busy");
158         }
159         if (!(address instanceof Inet4Address && mLocalAddress instanceof Inet4Address) &&
160                 !(address instanceof Inet6Address && mLocalAddress instanceof Inet6Address)) {
161             throw new IllegalArgumentException("Unsupported address");
162         }
163         if (port < 0 || port > 65535) {
164             throw new IllegalArgumentException("Invalid port");
165         }
166         mRemoteAddress = address;
167         mRemotePort = port;
168     }
169 
getSocket()170     int getSocket() {
171         return mSocket;
172     }
173 
174     /**
175      * Releases allocated resources. The stream becomes inoperable after calling
176      * this method.
177      *
178      * @throws IllegalStateException if the stream is busy.
179      * @see #isBusy()
180      */
release()181     public void release() {
182         synchronized (this) {
183             if (isBusy()) {
184                 throw new IllegalStateException("Busy");
185             }
186             close();
187         }
188     }
189 
close()190     private native void close();
191 
192     @Override
finalize()193     protected void finalize() throws Throwable {
194         close();
195         super.finalize();
196     }
197 }
198