msandroidvideo.cpp 10.6 KB
Newer Older
1 2 3
/*
mediastreamer2 library - modular sound and video processing and streaming

4
 * Copyright (C) 2010  Belledonne Communications, Grenoble, France
5 6 7 8 9 10 11 12 13 14 15 16 17 18

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
 */
20 21 22 23 24 25 26 27 28 29 30 31 32 33


#ifdef HAVE_CONFIG_H
#include "mediastreamer-config.h"
#endif

#include "mediastreamer2/mscommon.h"
#include "mediastreamer2/msvideo.h"
#include "mediastreamer2/msfilter.h"
#include "mediastreamer2/msticker.h"
#include "mediastreamer2/mswebcam.h"

#include <jni.h>

34
JavaVM *ms_andvid_jvm =0;
35 36 37 38



struct AndroidReaderContext {
39
	AndroidReaderContext():jvm(ms_andvid_jvm),frame(0),fps(5){
40 41
		ms_message("Creating AndroidReaderContext for Android VIDEO capture filter");

42 43 44 45 46
		ms_mutex_init(&mutex,NULL);

		JNIEnv *env = 0;
		jint result = jvm->AttachCurrentThread(&env,NULL);
		if (result != 0) {
47
			ms_fatal("cannot attach VM\n");
48 49 50
			return;
		}

51 52 53
		// Get SDK version
		jclass stockRecordClass = env->FindClass("org/linphone/core/AndroidCameraRecord");
		jfieldID sdk_field_id = env->GetStaticFieldID(stockRecordClass, "ANDROID_VERSION", "I");
54
		sdk_ver = (int) env->GetStaticObjectField(stockRecordClass, sdk_field_id);
55
		ms_message("SDK version is %i", sdk_ver);
56 57 58

		// Instanciate AndroidCameraRecord according to SDK version
		const char* videoClassName = "org/linphone/core/AndroidCameraRecordImpl";
59 60 61 62 63 64 65 66
		if (sdk_ver >= 8) videoClassName = "org/linphone/core/AndroidCameraRecordBufferedImpl";
		videoClassType = env->FindClass(videoClassName);
		if (videoClassType == 0) {
			ms_fatal("cannot find  %s\n", videoClassName);
			return;
		}

		videoClassType = (jclass) env->NewGlobalRef(videoClassType);
67
		if (videoClassType == 0) {
68
			ms_fatal("cannot register  %s\n", videoClassName);
69 70
			return;
		}
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85


		jmethodID constructorId = env->GetMethodID(videoClassType,"<init>", "(J)V");
		if (constructorId == 0) {
			ms_fatal("cannot find  %s\n", constructorId);
			return;
		}


		javaAndroidCameraRecord = env->NewObject(videoClassType, constructorId, (jlong) this);
		if (javaAndroidCameraRecord == 0) {
			ms_fatal("cannot instantiate  %s\n", javaAndroidCameraRecord);
			return;
		}

86
		javaAndroidCameraRecord = env->NewGlobalRef(javaAndroidCameraRecord);
87 88 89 90
		if (javaAndroidCameraRecord == 0) {
			ms_fatal("cannot register  %s\n", javaAndroidCameraRecord);
			return;
		}
91 92 93 94
	};

	~AndroidReaderContext(){
		ms_mutex_destroy(&mutex);
95 96

		// FIXME release JNI references
97 98
	};

99
	/*	long expectedBuffSize() {
100 101 102 103
		// http://developer.android.com/reference/android/hardware/Camera.html#addCallbackBuffer%28byte[]%29
		return height * width * bits / 8;
	}*/

104
	JavaVM	*jvm;
105
	//jbyte* buff;
106
	mblk_t *frame;
107 108
	float fps;
	MSVideoSize vsize;
109
	jobject javaAndroidCameraRecord;
110
	jclass videoClassType;
111 112 113 114
	ms_mutex_t mutex;
	int sdk_ver;
};

115 116
static AndroidReaderContext *getContext(MSFilter *f) {
	return (AndroidReaderContext*) f->data;
117 118
}

119 120 121 122 123 124 125 126
static int attachVM(JNIEnv **env, AndroidReaderContext *d) {
	jint result = d->jvm->AttachCurrentThread(env,NULL);
	if (result != 0) {
		ms_error("cannot attach VM\n");
		return 1;
	}
	return 0;
}
127 128 129



130 131 132 133 134
static int video_capture_set_fps(MSFilter *f, void *arg){
	AndroidReaderContext* d = (AndroidReaderContext*) f->data;
	d->fps=*((float*)arg);
	return 0;
}
135

136 137 138 139
static int video_capture_get_fps(MSFilter *f, void *arg){
	AndroidReaderContext* d = (AndroidReaderContext*) f->data;
	*((float*)arg) = d->fps;
	return 0;
140 141
}

142 143 144 145 146 147 148 149 150 151 152 153
static int video_capture_set_vsize(MSFilter *f, void* data){
	AndroidReaderContext* d = (AndroidReaderContext*) f->data;;
	d->vsize=*(MSVideoSize*)data;
	ms_message("Android video capture size set to h=%i w=%i in data structure", d->vsize.height, d->vsize.width);
	return 0;
}

static int video_capture_get_vsize(MSFilter *f, void* data){
	AndroidReaderContext* d = (AndroidReaderContext*) f->data;
	*(MSVideoSize*)data=d->vsize;
	return 0;
}
154

155 156 157 158 159 160
static int video_capture_get_pix_fmt(MSFilter *f, void *data){
	*(MSPixFmt*)data=MS_YUV420P;
	return 0;
}

static MSFilterMethod video_capture_methods[]={
161 162 163 164 165 166
		{	MS_FILTER_SET_FPS,	&video_capture_set_fps},
		{	MS_FILTER_GET_FPS,	&video_capture_get_fps},
		{	MS_FILTER_SET_VIDEO_SIZE, &video_capture_set_vsize},
		{	MS_FILTER_GET_VIDEO_SIZE, &video_capture_get_vsize},
		{	MS_FILTER_GET_PIX_FMT, &video_capture_get_pix_fmt},
		{	0,0 }
167 168 169 170 171 172 173 174
};


static void video_capture_postprocess(MSFilter *f);
static void video_capture_process(MSFilter *f);
static void video_capture_preprocess(MSFilter *f);

MSFilterDesc ms_video_capture_desc={
175 176 177 178 179 180 181 182 183 184 185 186 187
		MS_ANDROID_VIDEO_READ_ID,
		"MSAndroidVideoCapture",
		N_("A filter that capture Android video."),
		MS_FILTER_OTHER,
		NULL,
		0,
		1,
		NULL,
		video_capture_preprocess,
		video_capture_process,
		video_capture_postprocess,
		NULL,
		video_capture_methods
188 189 190 191 192 193 194 195 196 197
};

MS_FILTER_DESC_EXPORT(ms_video_capture_desc)

static void video_capture_detect(MSWebCamManager *obj);

static void video_capture_cam_init(MSWebCam *cam){
	ms_message("Android VIDEO capture filter cam init");
	cam->name=ms_strdup("Android video");
}
198 199

static MSFilter *video_capture_create_reader(MSWebCam *obj){
200
	ms_message("Instanciating Android VIDEO capture MS filter");
201 202

	MSFilter* lFilter = ms_filter_new_from_desc(&ms_video_capture_desc);
203 204 205 206
	lFilter->data = new AndroidReaderContext();
	ms_message("Android VIDEO capture MS filter instanciated");
	return lFilter;

207 208 209

}

210
MSWebCamDesc ms_android_video_capture_desc={
211 212 213 214 215
		"AndroidVideoCapture",
		&video_capture_detect,
		&video_capture_cam_init,
		&video_capture_create_reader,
		NULL
216 217 218 219 220 221
};

static void video_capture_detect(MSWebCamManager *obj){
	// FIXME list available camera throw a JNI call
	// Currently only create one camera

222 223
	ms_message("Detecting Android VIDEO cards");
	MSWebCam *cam=ms_web_cam_new(&ms_android_video_capture_desc);
224
	ms_web_cam_manager_add_cam(obj,cam);
225
	ms_message("Detection of Android VIDEO cards done");
226 227
}

228 229 230 231



// Set Video parameters to java recorder
232
void video_capture_preprocess(MSFilter *f){
233
	ms_message("Preprocessing of Android VIDEO capture filter");
234 235 236 237 238

	AndroidReaderContext *d = getContext(f);
	JNIEnv *env = 0;
	if (attachVM(&env, d) != 0) return;

239
	jmethodID setParamMethod = env->GetMethodID(d->videoClassType,"setParameters", "(IIF)V");
240 241 242 243 244 245 246
	if (setParamMethod == 0) {
		ms_message("cannot find  %s\n", setParamMethod);
		return;
	}

	ms_message("Android video capture setting parameters h=%i, w=%i fps=%f through JNI", d->vsize.height, d->vsize.width, d->fps);
	ms_mutex_lock(&d->mutex);
247
	env->CallVoidMethod(d->javaAndroidCameraRecord, setParamMethod, d->vsize.height, d->vsize.width, d->fps);
248 249 250
	ms_mutex_unlock(&d->mutex);

	ms_message("Preprocessing of Android VIDEO capture filter done");
251 252
}

253

254

255 256
static void video_capture_process(MSFilter *f){
	AndroidReaderContext* d = getContext(f);
257

258
	// If frame not ready, return
259
	if (d->frame == 0) {
260 261 262 263 264 265
		return;
	}


	ms_mutex_lock(&d->mutex);

266 267
	ms_queue_put(f->outputs[0],d->frame);
	d->frame = 0;
268

269 270
	ms_mutex_unlock(&d->mutex);
}
271 272


273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290

static void rotate_plane(int w, int h, uint8_t* src, int src_stride, uint8_t* dst, int dst_stride, int step) {
	int alpha = (w-h) / 2; // the stripe

	dst += alpha + h;
	src += step * alpha;
	int xmax = h*step;

	for (int y=0; y<h; y++) {
		uint8_t* dst2 = dst;
		for (int x=0; x<xmax; x+=step) {
			*dst2 = src[x];
			dst2 += dst_stride;
		}
		dst--;
		src += src_stride;
	}
}
291
static mblk_t *copy_frame_to_true_yuv(jbyte* initial_frame, int orientation, int w, int h) {
292

293
	//ms_message("Orientation %i; width %i; heigth %i", orientation, w, h);
294
	MSPicture pict;
295
	mblk_t *yuv_block = ms_yuv_buf_alloc(&pict, w, h);
296

297
	int ysize = w * h;
298

299 300

	// Copying Y
301 302 303 304 305 306 307 308 309 310 311
	uint8_t* dsty = pict.planes[0];
	uint8_t* srcy = (uint8_t*) initial_frame;
	int leftStrip = (w-h)/2;
	int rightStrip = (w+h) / 2;
	switch (orientation) {
	case 2: // -->
		for (int i=0; i < ysize; i++) {
			*(dsty+i) = *(srcy + ysize - i - 1);
		}
		break;
	case 1: // <--
312 313
		memset(dsty, 16, ysize);
		rotate_plane(w,h,srcy,w,dsty,w, 1);
314 315
		break;
	case 0: // ^^^
316
		memcpy(pict.planes[0],srcy,ysize);
317 318 319 320 321
		break;
	default:
		break;
	}

322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
	uint8_t* dstu = pict.planes[2];
	uint8_t* dstv = pict.planes[1];
	int uorvsize = ysize / 4;
	uint8_t* srcuv = (uint8_t*) initial_frame + ysize;
	switch (orientation) {
		case 1:
		{
			memset(dstu, 128, uorvsize);
			memset(dstv, 128, uorvsize);

			int uvw = w/2;
			int uvh = h/2;
			rotate_plane(uvw,uvh,srcuv,w,dstu,uvw, 2);
			rotate_plane(uvw,uvh,srcuv +1,w,dstv,uvw, 2);
			break;
		}
		case 0:
			for (int i = 0; i < uorvsize; i++) {
				*(dstu++) = *(srcuv++); // Copying U
				*(dstv++) = *(srcuv++); // Copying V
			}
			break;
		case 2:
			srcuv += 2 * uorvsize;
			for (int i = 0; i < uorvsize; i++) {
				*(dstu++) = *(srcuv--); // Copying U
				*(dstv++) = *(srcuv--); // Copying V
			}
			break;
		default:
			break;
353 354
	}

355

356 357
	return yuv_block;
}
358

359
extern "C" void Java_org_linphone_core_AndroidCameraRecordImpl_putImage(JNIEnv*  env,
360
		jobject  thiz,jlong nativePtr,jbyteArray jbadyuvframe, jint jorientation) {
361

362
	AndroidReaderContext* d = ((AndroidReaderContext*) nativePtr);
363

364 365 366 367
	jboolean isCopied;
	jbyte* jinternal_buff = env->GetByteArrayElements(jbadyuvframe, &isCopied);
	if (isCopied) {
		ms_warning("The video frame received from Java has been copied");
368 369 370
	}


371
	// Get a copy of the frame, encoded in a non interleaved YUV format
372
	mblk_t *yuv_frame=copy_frame_to_true_yuv(jinternal_buff, (int) jorientation, d->vsize.width, d->vsize.height);
373 374


375 376
	ms_mutex_lock(&d->mutex);
	if (d->frame != 0) {
377
		ms_message("Android video capture: putImage replacing old frame with new one");
378 379
		freemsg(d->frame);
		d->frame = 0;
380 381
	}

382
	d->frame = yuv_frame;
383
	ms_mutex_unlock(&d->mutex);
384 385 386 387

	// JNI_ABORT free the buffer without copying back the possible changes
	env->ReleaseByteArrayElements(jbadyuvframe, jinternal_buff, JNI_ABORT);

388 389 390 391
}



392

393
static void video_capture_postprocess(MSFilter *f){
394 395
	ms_message("Postprocessing of Android VIDEO capture filter");

396 397 398 399 400 401 402 403
	AndroidReaderContext* d = getContext(f);

	JNIEnv *env = 0;
	if (attachVM(&env, d) != 0) return;

	ms_message("Stoping video capture callback");
	jmethodID stopMethod = env->GetMethodID(d->videoClassType,"stopCaptureCallback", "()V");
	env->CallVoidMethod(d->javaAndroidCameraRecord, stopMethod);
404
	delete d;
405
	ms_message("Postprocessing of Android VIDEO capture filter done");
406 407 408 409 410
}

extern "C" void ms_andvid_set_jvm(JavaVM *jvm) {
	ms_andvid_jvm=jvm;
}