PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/voip/java/android/net/rtp/RtpStream.java

http://github.com/CyanogenMod/android_frameworks_base
Java | 191 lines | 79 code | 23 blank | 89 comment | 10 complexity | 3d6f125ec58f79a6a39fe2c2d97f5df1 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception, CC0-1.0, BitTorrent-1.0, BSD-3-Clause
  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. package android.net.rtp;
  17. import java.net.InetAddress;
  18. import java.net.Inet4Address;
  19. import java.net.Inet6Address;
  20. import java.net.SocketException;
  21. /**
  22. * RtpStream represents the base class of streams which send and receive network
  23. * packets with media payloads over Real-time Transport Protocol (RTP).
  24. *
  25. * <p class="note">Using this class requires
  26. * {@link android.Manifest.permission#INTERNET} permission.</p>
  27. */
  28. public class RtpStream {
  29. /**
  30. * This mode indicates that the stream sends and receives packets at the
  31. * same time. This is the initial mode for new streams.
  32. */
  33. public static final int MODE_NORMAL = 0;
  34. /**
  35. * This mode indicates that the stream only sends packets.
  36. */
  37. public static final int MODE_SEND_ONLY = 1;
  38. /**
  39. * This mode indicates that the stream only receives packets.
  40. */
  41. public static final int MODE_RECEIVE_ONLY = 2;
  42. private static final int MODE_LAST = 2;
  43. private final InetAddress mLocalAddress;
  44. private final int mLocalPort;
  45. private InetAddress mRemoteAddress;
  46. private int mRemotePort = -1;
  47. private int mMode = MODE_NORMAL;
  48. private int mNative;
  49. static {
  50. System.loadLibrary("rtp_jni");
  51. }
  52. /**
  53. * Creates a RtpStream on the given local address. Note that the local
  54. * port is assigned automatically to conform with RFC 3550.
  55. *
  56. * @param address The network address of the local host to bind to.
  57. * @throws SocketException if the address cannot be bound or a problem
  58. * occurs during binding.
  59. */
  60. RtpStream(InetAddress address) throws SocketException {
  61. mLocalPort = create(address.getHostAddress());
  62. mLocalAddress = address;
  63. }
  64. private native int create(String address) throws SocketException;
  65. /**
  66. * Returns the network address of the local host.
  67. */
  68. public InetAddress getLocalAddress() {
  69. return mLocalAddress;
  70. }
  71. /**
  72. * Returns the network port of the local host.
  73. */
  74. public int getLocalPort() {
  75. return mLocalPort;
  76. }
  77. /**
  78. * Returns the network address of the remote host or {@code null} if the
  79. * stream is not associated.
  80. */
  81. public InetAddress getRemoteAddress() {
  82. return mRemoteAddress;
  83. }
  84. /**
  85. * Returns the network port of the remote host or {@code -1} if the stream
  86. * is not associated.
  87. */
  88. public int getRemotePort() {
  89. return mRemotePort;
  90. }
  91. /**
  92. * Returns {@code true} if the stream is busy. In this case most of the
  93. * setter methods are disabled. This method is intended to be overridden
  94. * by subclasses.
  95. */
  96. public boolean isBusy() {
  97. return false;
  98. }
  99. /**
  100. * Returns the current mode.
  101. */
  102. public int getMode() {
  103. return mMode;
  104. }
  105. /**
  106. * Changes the current mode. It must be one of {@link #MODE_NORMAL},
  107. * {@link #MODE_SEND_ONLY}, and {@link #MODE_RECEIVE_ONLY}.
  108. *
  109. * @param mode The mode to change to.
  110. * @throws IllegalArgumentException if the mode is invalid.
  111. * @throws IllegalStateException if the stream is busy.
  112. * @see #isBusy()
  113. */
  114. public void setMode(int mode) {
  115. if (isBusy()) {
  116. throw new IllegalStateException("Busy");
  117. }
  118. if (mode < 0 || mode > MODE_LAST) {
  119. throw new IllegalArgumentException("Invalid mode");
  120. }
  121. mMode = mode;
  122. }
  123. /**
  124. * Associates with a remote host. This defines the destination of the
  125. * outgoing packets.
  126. *
  127. * @param address The network address of the remote host.
  128. * @param port The network port of the remote host.
  129. * @throws IllegalArgumentException if the address is not supported or the
  130. * port is invalid.
  131. * @throws IllegalStateException if the stream is busy.
  132. * @see #isBusy()
  133. */
  134. public void associate(InetAddress address, int port) {
  135. if (isBusy()) {
  136. throw new IllegalStateException("Busy");
  137. }
  138. if (!(address instanceof Inet4Address && mLocalAddress instanceof Inet4Address) &&
  139. !(address instanceof Inet6Address && mLocalAddress instanceof Inet6Address)) {
  140. throw new IllegalArgumentException("Unsupported address");
  141. }
  142. if (port < 0 || port > 65535) {
  143. throw new IllegalArgumentException("Invalid port");
  144. }
  145. mRemoteAddress = address;
  146. mRemotePort = port;
  147. }
  148. synchronized native int dup();
  149. /**
  150. * Releases allocated resources. The stream becomes inoperable after calling
  151. * this method.
  152. *
  153. * @throws IllegalStateException if the stream is busy.
  154. * @see #isBusy()
  155. */
  156. public void release() {
  157. if (isBusy()) {
  158. throw new IllegalStateException("Busy");
  159. }
  160. close();
  161. }
  162. private synchronized native void close();
  163. @Override
  164. protected void finalize() throws Throwable {
  165. close();
  166. super.finalize();
  167. }
  168. }