RtpStream.cpp revision 4c5d28cee0537c83ff0e5bc0daaae78f68dfc7c8
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#include <stdio.h> 18#include <stdint.h> 19#include <string.h> 20#include <errno.h> 21#include <sys/types.h> 22#include <sys/socket.h> 23#include <arpa/inet.h> 24#include <netinet/in.h> 25 26#define LOG_TAG "RtpStream" 27#include <utils/Log.h> 28 29#include "jni.h" 30#include "JNIHelp.h" 31 32extern int parse(JNIEnv *env, jstring jAddress, int port, sockaddr_storage *ss); 33 34namespace { 35 36jfieldID gNative; 37 38jint create(JNIEnv *env, jobject thiz, jstring jAddress) 39{ 40 env->SetIntField(thiz, gNative, -1); 41 42 sockaddr_storage ss; 43 if (parse(env, jAddress, 0, &ss) < 0) { 44 // Exception already thrown. 45 return -1; 46 } 47 48 int socket = ::socket(ss.ss_family, SOCK_DGRAM, 0); 49 socklen_t len = sizeof(ss); 50 if (socket == -1 || bind(socket, (sockaddr *)&ss, sizeof(ss)) != 0 || 51 getsockname(socket, (sockaddr *)&ss, &len) != 0) { 52 jniThrowException(env, "java/net/SocketException", strerror(errno)); 53 ::close(socket); 54 return -1; 55 } 56 57 uint16_t *p = (ss.ss_family == AF_INET) ? 58 &((sockaddr_in *)&ss)->sin_port : &((sockaddr_in6 *)&ss)->sin6_port; 59 uint16_t port = ntohs(*p); 60 if ((port & 1) == 0) { 61 env->SetIntField(thiz, gNative, socket); 62 return port; 63 } 64 ::close(socket); 65 66 socket = ::socket(ss.ss_family, SOCK_DGRAM, 0); 67 if (socket != -1) { 68 uint16_t delta = port << 1; 69 ++port; 70 71 for (int i = 0; i < 1000; ++i) { 72 do { 73 port += delta; 74 } while (port < 1024); 75 *p = htons(port); 76 77 if (bind(socket, (sockaddr *)&ss, sizeof(ss)) == 0) { 78 env->SetIntField(thiz, gNative, socket); 79 return port; 80 } 81 } 82 } 83 84 jniThrowException(env, "java/net/SocketException", strerror(errno)); 85 ::close(socket); 86 return -1; 87} 88 89jint dup(JNIEnv *env, jobject thiz) 90{ 91 int socket1 = env->GetIntField(thiz, gNative); 92 int socket2 = ::dup(socket1); 93 if (socket2 == -1) { 94 jniThrowException(env, "java/lang/IllegalStateException", strerror(errno)); 95 } 96 LOGD("dup %d to %d", socket1, socket2); 97 return socket2; 98} 99 100void close(JNIEnv *env, jobject thiz) 101{ 102 int socket = env->GetIntField(thiz, gNative); 103 ::close(socket); 104 env->SetIntField(thiz, gNative, -1); 105 LOGD("close %d", socket); 106} 107 108JNINativeMethod gMethods[] = { 109 {"create", "(Ljava/lang/String;)I", (void *)create}, 110 {"dup", "()I", (void *)dup}, 111 {"close", "()V", (void *)close}, 112}; 113 114} // namespace 115 116int registerRtpStream(JNIEnv *env) 117{ 118 jclass clazz; 119 if ((clazz = env->FindClass("android/net/rtp/RtpStream")) == NULL || 120 (gNative = env->GetFieldID(clazz, "mNative", "I")) == NULL || 121 env->RegisterNatives(clazz, gMethods, NELEM(gMethods)) < 0) { 122 LOGE("JNI registration failed"); 123 return -1; 124 } 125 return 0; 126} 127