blob: bfe8e24b4fd3c0f28ec54047daf1e5c074891af8 [file] [log] [blame]
Chia-chi Yeh4c5d28c2010-08-06 14:12:05 +08001/*
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
Chia-chi Yehe6695052012-03-30 13:25:19 -070036jfieldID gSocket;
Chia-chi Yeh4c5d28c2010-08-06 14:12:05 +080037
38jint create(JNIEnv *env, jobject thiz, jstring jAddress)
39{
Chia-chi Yehe6695052012-03-30 13:25:19 -070040 env->SetIntField(thiz, gSocket, -1);
Chia-chi Yeh4c5d28c2010-08-06 14:12:05 +080041
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) {
Chia-chi Yehe6695052012-03-30 13:25:19 -070061 env->SetIntField(thiz, gSocket, socket);
Chia-chi Yeh4c5d28c2010-08-06 14:12:05 +080062 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) {
Chia-chi Yehe6695052012-03-30 13:25:19 -070078 env->SetIntField(thiz, gSocket, socket);
Chia-chi Yeh4c5d28c2010-08-06 14:12:05 +080079 return port;
80 }
81 }
82 }
83
84 jniThrowException(env, "java/net/SocketException", strerror(errno));
85 ::close(socket);
86 return -1;
87}
88
Chia-chi Yeh4c5d28c2010-08-06 14:12:05 +080089void close(JNIEnv *env, jobject thiz)
90{
Chia-chi Yehe6695052012-03-30 13:25:19 -070091 int socket = env->GetIntField(thiz, gSocket);
Chia-chi Yeh4c5d28c2010-08-06 14:12:05 +080092 ::close(socket);
Chia-chi Yehe6695052012-03-30 13:25:19 -070093 env->SetIntField(thiz, gSocket, -1);
Chia-chi Yeh4c5d28c2010-08-06 14:12:05 +080094}
95
96JNINativeMethod gMethods[] = {
97 {"create", "(Ljava/lang/String;)I", (void *)create},
Chia-chi Yeh4c5d28c2010-08-06 14:12:05 +080098 {"close", "()V", (void *)close},
99};
100
101} // namespace
102
103int registerRtpStream(JNIEnv *env)
104{
105 jclass clazz;
106 if ((clazz = env->FindClass("android/net/rtp/RtpStream")) == NULL ||
Chia-chi Yehe6695052012-03-30 13:25:19 -0700107 (gSocket = env->GetFieldID(clazz, "mSocket", "I")) == NULL ||
Chia-chi Yeh4c5d28c2010-08-06 14:12:05 +0800108 env->RegisterNatives(clazz, gMethods, NELEM(gMethods)) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000109 ALOGE("JNI registration failed");
Chia-chi Yeh4c5d28c2010-08-06 14:12:05 +0800110 return -1;
111 }
112 return 0;
113}