Home | History | Annotate | Download | only in rtp
      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 
     32 extern int parse(JNIEnv *env, jstring jAddress, int port, sockaddr_storage *ss);
     33 
     34 namespace {
     35 
     36 jfieldID gNative;
     37 
     38 jint 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 
     89 jint dup(JNIEnv *env, jobject thiz)
     90 {
     91     int socket = ::dup(env->GetIntField(thiz, gNative));
     92     if (socket == -1) {
     93         jniThrowException(env, "java/lang/IllegalStateException", strerror(errno));
     94     }
     95     return socket;
     96 }
     97 
     98 void close(JNIEnv *env, jobject thiz)
     99 {
    100     int socket = env->GetIntField(thiz, gNative);
    101     ::close(socket);
    102     env->SetIntField(thiz, gNative, -1);
    103 }
    104 
    105 JNINativeMethod gMethods[] = {
    106     {"create", "(Ljava/lang/String;)I", (void *)create},
    107     {"dup", "()I", (void *)dup},
    108     {"close", "()V", (void *)close},
    109 };
    110 
    111 } // namespace
    112 
    113 int registerRtpStream(JNIEnv *env)
    114 {
    115     jclass clazz;
    116     if ((clazz = env->FindClass("android/net/rtp/RtpStream")) == NULL ||
    117         (gNative = env->GetFieldID(clazz, "mNative", "I")) == NULL ||
    118         env->RegisterNatives(clazz, gMethods, NELEM(gMethods)) < 0) {
    119         LOGE("JNI registration failed");
    120         return -1;
    121     }
    122     return 0;
    123 }
    124