blob: faa1c0673f20265d4c78717902aa17d81a6eeed4 [file] [log] [blame]
Sauw Ming6e6c2152010-12-14 13:03:10 +00001/* $Id$ */
2/*
3 * Copyright (C) 2008-2010 Teluu Inc. (http://www.teluu.com)
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19#include <pjmedia-videodev/videodev_imp.h>
20#include <pj/assert.h>
21#include <pj/log.h>
22#include <pj/os.h>
23
24#if PJMEDIA_VIDEO_DEV_HAS_IOS
25#include "Availability.h"
26#ifdef __IPHONE_4_0
27
28#import <UIKit/UIKit.h>
29#import <AVFoundation/AVFoundation.h>
30
31#define THIS_FILE "ios_dev.c"
32#define DEFAULT_CLOCK_RATE 9000
33#define DEFAULT_WIDTH 480
34#define DEFAULT_HEIGHT 360
35#define DEFAULT_FPS 15
36
37typedef struct ios_fmt_info
38{
39 pjmedia_format_id pjmedia_format;
40 UInt32 ios_format;
41} ios_fmt_info;
42
43static ios_fmt_info ios_fmts[] =
44{
45 {PJMEDIA_FORMAT_BGRA, kCVPixelFormatType_32BGRA} ,
46};
47
48/* qt device info */
49struct ios_dev_info
50{
51 pjmedia_vid_dev_info info;
52};
53
54/* qt factory */
55struct ios_factory
56{
57 pjmedia_vid_dev_factory base;
58 pj_pool_t *pool;
59 pj_pool_factory *pf;
60
61 unsigned dev_count;
62 struct ios_dev_info *dev_info;
63};
64
65@interface VOutDelegate: NSObject
66 <AVCaptureVideoDataOutputSampleBufferDelegate>
67{
68@public
69 struct ios_stream *stream;
70}
71@end
72
73/* Video stream. */
74struct ios_stream
75{
76 pjmedia_vid_stream base; /**< Base stream */
77 pjmedia_vid_param param; /**< Settings */
78 pj_pool_t *pool; /**< Memory pool. */
79
80 pjmedia_vid_cb vid_cb; /**< Stream callback. */
81 void *user_data; /**< Application data. */
82
83 pjmedia_rect_size size;
84 pj_uint8_t bpp;
85 unsigned bytes_per_row;
86 unsigned frame_size;
87
88 AVCaptureSession *cap_session;
89 AVCaptureDeviceInput *dev_input;
90 AVCaptureVideoDataOutput *video_output;
91 VOutDelegate *vout_delegate;
92
93 UIImageView *imgView;
94 void *buf;
95};
96
97
98/* Prototypes */
99static pj_status_t ios_factory_init(pjmedia_vid_dev_factory *f);
100static pj_status_t ios_factory_destroy(pjmedia_vid_dev_factory *f);
101static unsigned ios_factory_get_dev_count(pjmedia_vid_dev_factory *f);
102static pj_status_t ios_factory_get_dev_info(pjmedia_vid_dev_factory *f,
103 unsigned index,
104 pjmedia_vid_dev_info *info);
105static pj_status_t ios_factory_default_param(pj_pool_t *pool,
106 pjmedia_vid_dev_factory *f,
107 unsigned index,
108 pjmedia_vid_param *param);
109static pj_status_t ios_factory_create_stream(pjmedia_vid_dev_factory *f,
110 const pjmedia_vid_param *param,
111 const pjmedia_vid_cb *cb,
112 void *user_data,
113 pjmedia_vid_stream **p_vid_strm);
114
115static pj_status_t ios_stream_get_param(pjmedia_vid_stream *strm,
116 pjmedia_vid_param *param);
117static pj_status_t ios_stream_get_cap(pjmedia_vid_stream *strm,
118 pjmedia_vid_dev_cap cap,
119 void *value);
120static pj_status_t ios_stream_set_cap(pjmedia_vid_stream *strm,
121 pjmedia_vid_dev_cap cap,
122 const void *value);
123static pj_status_t ios_stream_start(pjmedia_vid_stream *strm);
124static pj_status_t ios_stream_put_frame(pjmedia_vid_stream *strm,
125 const pjmedia_frame *frame);
126static pj_status_t ios_stream_stop(pjmedia_vid_stream *strm);
127static pj_status_t ios_stream_destroy(pjmedia_vid_stream *strm);
128
129/* Operations */
130static pjmedia_vid_dev_factory_op factory_op =
131{
132 &ios_factory_init,
133 &ios_factory_destroy,
134 &ios_factory_get_dev_count,
135 &ios_factory_get_dev_info,
136 &ios_factory_default_param,
137 &ios_factory_create_stream
138};
139
140static pjmedia_vid_stream_op stream_op =
141{
142 &ios_stream_get_param,
143 &ios_stream_get_cap,
144 &ios_stream_set_cap,
145 &ios_stream_start,
146 NULL,
147 &ios_stream_put_frame,
148 &ios_stream_stop,
149 &ios_stream_destroy
150};
151
152
153/****************************************************************************
154 * Factory operations
155 */
156/*
157 * Init ios_ video driver.
158 */
159pjmedia_vid_dev_factory* pjmedia_ios_factory(pj_pool_factory *pf)
160{
161 struct ios_factory *f;
162 pj_pool_t *pool;
163
164 pool = pj_pool_create(pf, "ios video", 512, 512, NULL);
165 f = PJ_POOL_ZALLOC_T(pool, struct ios_factory);
166 f->pf = pf;
167 f->pool = pool;
168 f->base.op = &factory_op;
169
170 return &f->base;
171}
172
173
174/* API: init factory */
175static pj_status_t ios_factory_init(pjmedia_vid_dev_factory *f)
176{
177 struct ios_factory *qf = (struct ios_factory*)f;
178 struct ios_dev_info *qdi;
179 unsigned i, l;
180
181 /* Initialize input and output devices here */
182 qf->dev_info = (struct ios_dev_info*)
183 pj_pool_calloc(qf->pool, 2,
184 sizeof(struct ios_dev_info));
185
186 qf->dev_count = 0;
187 qdi = &qf->dev_info[qf->dev_count++];
188 pj_bzero(qdi, sizeof(*qdi));
189 strcpy(qdi->info.name, "iOS UIView");
190 strcpy(qdi->info.driver, "iOS");
191 qdi->info.dir = PJMEDIA_DIR_RENDER;
192 qdi->info.has_callback = PJ_FALSE;
193
194 if (NSClassFromString(@"AVCaptureSession")) {
195 qdi = &qf->dev_info[qf->dev_count++];
196 pj_bzero(qdi, sizeof(*qdi));
197 strcpy(qdi->info.name, "iOS AVCapture");
198 strcpy(qdi->info.driver, "iOS");
199 qdi->info.dir = PJMEDIA_DIR_CAPTURE;
200 qdi->info.has_callback = PJ_TRUE;
201 }
202
203 for (i = 0; i < qf->dev_count; i++) {
204 qdi = &qf->dev_info[i];
205 qdi->info.fmt_cnt = PJ_ARRAY_SIZE(ios_fmts);
206 qdi->info.caps = PJMEDIA_VID_DEV_CAP_FORMAT;
207 qdi->info.fmt = (pjmedia_format*)
208 pj_pool_calloc(qf->pool, qdi->info.fmt_cnt,
209 sizeof(pjmedia_format));
210
211 for (l = 0; l < PJ_ARRAY_SIZE(ios_fmts); l++) {
212 pjmedia_format *fmt = &qdi->info.fmt[l];
213 pjmedia_format_init_video(fmt,
214 ios_fmts[l].pjmedia_format,
215 DEFAULT_WIDTH,
216 DEFAULT_HEIGHT,
217 DEFAULT_FPS, 1);
218 }
219 }
220
221 PJ_LOG(4, (THIS_FILE, "iOS video initialized with %d devices",
222 qf->dev_count));
223
224 return PJ_SUCCESS;
225}
226
227/* API: destroy factory */
228static pj_status_t ios_factory_destroy(pjmedia_vid_dev_factory *f)
229{
230 struct ios_factory *qf = (struct ios_factory*)f;
231 pj_pool_t *pool = qf->pool;
232
233 qf->pool = NULL;
234 pj_pool_release(pool);
235
236 return PJ_SUCCESS;
237}
238
239/* API: get number of devices */
240static unsigned ios_factory_get_dev_count(pjmedia_vid_dev_factory *f)
241{
242 struct ios_factory *qf = (struct ios_factory*)f;
243 return qf->dev_count;
244}
245
246/* API: get device info */
247static pj_status_t ios_factory_get_dev_info(pjmedia_vid_dev_factory *f,
248 unsigned index,
249 pjmedia_vid_dev_info *info)
250{
251 struct ios_factory *qf = (struct ios_factory*)f;
252
253 PJ_ASSERT_RETURN(index < qf->dev_count, PJMEDIA_EVID_INVDEV);
254
255 pj_memcpy(info, &qf->dev_info[index].info, sizeof(*info));
256
257 return PJ_SUCCESS;
258}
259
260/* API: create default device parameter */
261static pj_status_t ios_factory_default_param(pj_pool_t *pool,
262 pjmedia_vid_dev_factory *f,
263 unsigned index,
264 pjmedia_vid_param *param)
265{
266 struct ios_factory *qf = (struct ios_factory*)f;
267 struct ios_dev_info *di = &qf->dev_info[index];
268
269 PJ_ASSERT_RETURN(index < qf->dev_count, PJMEDIA_EVID_INVDEV);
270
271 PJ_UNUSED_ARG(pool);
272
273 pj_bzero(param, sizeof(*param));
274 if (di->info.dir & PJMEDIA_DIR_CAPTURE_RENDER) {
275 param->dir = PJMEDIA_DIR_CAPTURE_RENDER;
276 param->cap_id = index;
277 param->rend_id = index;
278 } else if (di->info.dir & PJMEDIA_DIR_CAPTURE) {
279 param->dir = PJMEDIA_DIR_CAPTURE;
280 param->cap_id = index;
281 param->rend_id = PJMEDIA_VID_INVALID_DEV;
282 } else if (di->info.dir & PJMEDIA_DIR_RENDER) {
283 param->dir = PJMEDIA_DIR_RENDER;
284 param->rend_id = index;
285 param->cap_id = PJMEDIA_VID_INVALID_DEV;
286 } else {
287 return PJMEDIA_EVID_INVDEV;
288 }
289
290 param->flags = PJMEDIA_VID_DEV_CAP_FORMAT;
291 param->clock_rate = DEFAULT_CLOCK_RATE;
292 param->frame_rate.num = DEFAULT_FPS;
293 param->frame_rate.denum = 1;
294 pj_memcpy(&param->fmt, &di->info.fmt[0], sizeof(param->fmt));
295
296 return PJ_SUCCESS;
297}
298
299@implementation VOutDelegate
300- (void)update_image
301{
302 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
303
304 /* Create a device-dependent RGB color space */
305 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
306
307 /* Create a bitmap graphics context with the sample buffer data */
308 CGContextRef context =
309 CGBitmapContextCreate(stream->buf, stream->size.w, stream->size.h, 8,
310 stream->bytes_per_row, colorSpace,
311 kCGBitmapByteOrder32Little |
312 kCGImageAlphaPremultipliedFirst);
313
314 /**
315 * Create a Quartz image from the pixel data in the bitmap graphics
316 * context
317 */
318 CGImageRef quartzImage = CGBitmapContextCreateImage(context);
319
320 /* Free up the context and color space */
321 CGContextRelease(context);
322 CGColorSpaceRelease(colorSpace);
323
324 /* Create an image object from the Quartz image */
325 UIImage *image = [UIImage imageWithCGImage:quartzImage scale:1.0
326 orientation:UIImageOrientationRight];
327
328 /* Release the Quartz image */
329 CGImageRelease(quartzImage);
330
331 [stream->imgView setImage:image];
332
333 [pool release];
334}
335
336- (void)captureOutput:(AVCaptureOutput *)captureOutput
337 didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
338 fromConnection:(AVCaptureConnection *)connection
339{
340 pjmedia_frame frame;
341 CVImageBufferRef imageBuffer;
342
343 if (!sampleBuffer)
344 return;
345
346 /* Get a CMSampleBuffer's Core Video image buffer for the media data */
347 imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
348
349 /* Lock the base address of the pixel buffer */
350 CVPixelBufferLockBaseAddress(imageBuffer, 0);
351
352 frame.type = PJMEDIA_TYPE_VIDEO;
353 frame.buf = CVPixelBufferGetBaseAddress(imageBuffer);
354 frame.size = stream->frame_size;
355 frame.bit_info = 0;
356 if (stream->vid_cb.capture_cb)
357 (*stream->vid_cb.capture_cb)(&stream->base, stream->user_data, &frame);
358
359 /* Unlock the pixel buffer */
360 CVPixelBufferUnlockBaseAddress(imageBuffer,0);
361}
362@end
363
364static ios_fmt_info* get_ios_format_info(pjmedia_format_id id)
365{
366 unsigned i;
367
368 for (i = 0; i < PJ_ARRAY_SIZE(ios_fmts); i++) {
369 if (ios_fmts[i].pjmedia_format == id)
370 return &ios_fmts[i];
371 }
372
373 return NULL;
374}
375
376/* API: create stream */
377static pj_status_t ios_factory_create_stream(pjmedia_vid_dev_factory *f,
378 const pjmedia_vid_param *param,
379 const pjmedia_vid_cb *cb,
380 void *user_data,
381 pjmedia_vid_stream **p_vid_strm)
382{
383 struct ios_factory *qf = (struct ios_factory*)f;
384 pj_pool_t *pool;
385 struct ios_stream *strm;
386 pjmedia_video_format_detail *vfd;
387 const pjmedia_video_format_info *vfi;
388 pj_status_t status = PJ_SUCCESS;
389 ios_fmt_info *ifi = get_ios_format_info(param->fmt.id);
390 NSError *error;
391
392 PJ_ASSERT_RETURN(f && param && p_vid_strm, PJ_EINVAL);
393 PJ_ASSERT_RETURN(param->fmt.type == PJMEDIA_TYPE_VIDEO &&
394 param->fmt.detail_type == PJMEDIA_FORMAT_DETAIL_VIDEO,
395 PJ_EINVAL);
396
397 if (!(ifi = get_ios_format_info(param->fmt.id)))
398 return PJMEDIA_EVID_BADFORMAT;
399
400 vfi = pjmedia_get_video_format_info(NULL, param->fmt.id);
401 if (!vfi)
402 return PJMEDIA_EVID_BADFORMAT;
403
404 /* Create and Initialize stream descriptor */
405 pool = pj_pool_create(qf->pf, "ios-dev", 4000, 4000, NULL);
406 PJ_ASSERT_RETURN(pool != NULL, PJ_ENOMEM);
407
408 strm = PJ_POOL_ZALLOC_T(pool, struct ios_stream);
409 pj_memcpy(&strm->param, param, sizeof(*param));
410 strm->pool = pool;
411 pj_memcpy(&strm->vid_cb, cb, sizeof(*cb));
412 strm->user_data = user_data;
413
414 vfd = pjmedia_format_get_video_format_detail(&strm->param.fmt, PJ_TRUE);
415 pj_memcpy(&strm->size, &vfd->size, sizeof(vfd->size));
416 strm->bpp = vfi->bpp;
417 strm->bytes_per_row = strm->size.w * strm->bpp / 8;
418 strm->frame_size = strm->bytes_per_row * strm->size.h;
419
420 /* Create capture stream here */
421 if (param->dir & PJMEDIA_DIR_CAPTURE) {
422 strm->cap_session = [[AVCaptureSession alloc] init];
423 if (!strm->cap_session) {
424 status = PJ_ENOMEM;
425 goto on_error;
426 }
427 strm->cap_session.sessionPreset = AVCaptureSessionPresetMedium;
428
429 /* Open video device */
430 AVCaptureDevice *videoDevice =
431 [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
432 if (!videoDevice) {
433 status = PJMEDIA_EVID_SYSERR;
434 goto on_error;
435 }
436
437 /* Add the video device to the session as a device input */
438 strm->dev_input = [AVCaptureDeviceInput
439 deviceInputWithDevice:videoDevice
440 error: &error];
441 if (!strm->dev_input) {
442 status = PJMEDIA_EVID_SYSERR;
443 goto on_error;
444 }
445 [strm->cap_session addInput:strm->dev_input];
446
447 strm->video_output = [[[AVCaptureVideoDataOutput alloc] init]
448 autorelease];
449 if (!strm->video_output) {
450 status = PJMEDIA_EVID_SYSERR;
451 goto on_error;
452 }
453 [strm->cap_session addOutput:strm->video_output];
454
455 // Configure your output.
456 strm->vout_delegate = [VOutDelegate alloc];
457 strm->vout_delegate->stream = strm;
458 dispatch_queue_t queue = dispatch_queue_create("myQueue", NULL);
459 [strm->video_output setSampleBufferDelegate:strm->vout_delegate
460 queue:queue];
461 dispatch_release(queue);
462
463 strm->video_output.videoSettings =
464 [NSDictionary dictionaryWithObjectsAndKeys:
465 [NSNumber numberWithInt:ifi->ios_format],
466 kCVPixelBufferPixelFormatTypeKey,
467 [NSNumber numberWithInt: vfd->size.w],
468 kCVPixelBufferWidthKey,
469 [NSNumber numberWithInt: vfd->size.h],
470 kCVPixelBufferHeightKey, nil];
471 strm->video_output.minFrameDuration = CMTimeMake(vfd->fps.denum,
472 vfd->fps.num);
473 }
474
475 /* Create renderer stream here */
476 if (param->dir & PJMEDIA_DIR_RENDER) {
477 /* Get the main window */
478 UIWindow *window = [[UIApplication sharedApplication] keyWindow];
479
480 pj_assert(window);
481 strm->imgView = [[UIImageView alloc] initWithFrame:[window bounds]];
482 if (!strm->imgView) {
483 status = PJ_ENOMEM;
484 goto on_error;
485 }
486 [window addSubview:strm->imgView];
487
488 if (!strm->vout_delegate) {
489 strm->vout_delegate = [VOutDelegate alloc];
490 strm->vout_delegate->stream = strm;
491 }
492
493 strm->buf = pj_pool_alloc(pool, strm->frame_size);
494 }
495
496 /* Apply the remaining settings */
497 /*
498 if (param->flags & PJMEDIA_VID_DEV_CAP_INPUT_SCALE) {
499 ios_stream_set_cap(&strm->base,
500 PJMEDIA_VID_DEV_CAP_INPUT_SCALE,
501 &param->fmt);
502 }
503 */
504 /* Done */
505 strm->base.op = &stream_op;
506 *p_vid_strm = &strm->base;
507
508 return PJ_SUCCESS;
509
510on_error:
511 ios_stream_destroy((pjmedia_vid_stream *)strm);
512
513 return status;
514}
515
516/* API: Get stream info. */
517static pj_status_t ios_stream_get_param(pjmedia_vid_stream *s,
518 pjmedia_vid_param *pi)
519{
520 struct ios_stream *strm = (struct ios_stream*)s;
521
522 PJ_ASSERT_RETURN(strm && pi, PJ_EINVAL);
523
524 pj_memcpy(pi, &strm->param, sizeof(*pi));
525
526/* if (ios_stream_get_cap(s, PJMEDIA_VID_DEV_CAP_INPUT_SCALE,
527 &pi->fmt.info_size) == PJ_SUCCESS)
528 {
529 pi->flags |= PJMEDIA_VID_DEV_CAP_INPUT_SCALE;
530 }
531*/
532 return PJ_SUCCESS;
533}
534
535/* API: get capability */
536static pj_status_t ios_stream_get_cap(pjmedia_vid_stream *s,
537 pjmedia_vid_dev_cap cap,
538 void *pval)
539{
540 struct ios_stream *strm = (struct ios_stream*)s;
541
542 PJ_UNUSED_ARG(strm);
543
544 PJ_ASSERT_RETURN(s && pval, PJ_EINVAL);
545
546 if (cap==PJMEDIA_VID_DEV_CAP_INPUT_SCALE)
547 {
548 return PJMEDIA_EVID_INVCAP;
549// return PJ_SUCCESS;
550 } else {
551 return PJMEDIA_EVID_INVCAP;
552 }
553}
554
555/* API: set capability */
556static pj_status_t ios_stream_set_cap(pjmedia_vid_stream *s,
557 pjmedia_vid_dev_cap cap,
558 const void *pval)
559{
560 struct ios_stream *strm = (struct ios_stream*)s;
561
562 PJ_UNUSED_ARG(strm);
563
564 PJ_ASSERT_RETURN(s && pval, PJ_EINVAL);
565
566 if (cap==PJMEDIA_VID_DEV_CAP_INPUT_SCALE)
567 {
568 return PJ_SUCCESS;
569 }
570
571 return PJMEDIA_EVID_INVCAP;
572}
573
574/* API: Start stream. */
575static pj_status_t ios_stream_start(pjmedia_vid_stream *strm)
576{
577 struct ios_stream *stream = (struct ios_stream*)strm;
578
579 PJ_UNUSED_ARG(stream);
580
581 PJ_LOG(4, (THIS_FILE, "Starting qt video stream"));
582
583 if (stream->cap_session) {
584 [stream->cap_session startRunning];
585
586 if (![stream->cap_session isRunning])
587 return PJ_EUNKNOWN;
588 }
589
590 return PJ_SUCCESS;
591}
592
593
594/* API: Put frame from stream */
595static pj_status_t ios_stream_put_frame(pjmedia_vid_stream *strm,
596 const pjmedia_frame *frame)
597{
598 struct ios_stream *stream = (struct ios_stream*)strm;
599 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
600
601 pj_assert(stream->frame_size >= frame->size);
602 pj_memcpy(stream->buf, frame->buf, frame->size);
603 /* Perform video display in a background thread */
604// [stream->vout_delegate update_image];
605 [NSThread detachNewThreadSelector:@selector(update_image)
606 toTarget:stream->vout_delegate withObject:nil];
607
608 [pool release];
609
610 return PJ_SUCCESS;
611}
612
613/* API: Stop stream. */
614static pj_status_t ios_stream_stop(pjmedia_vid_stream *strm)
615{
616 struct ios_stream *stream = (struct ios_stream*)strm;
617
618 PJ_UNUSED_ARG(stream);
619
620 PJ_LOG(4, (THIS_FILE, "Stopping qt video stream"));
621
622 if (stream->cap_session && [stream->cap_session isRunning])
623 [stream->cap_session stopRunning];
624
625 return PJ_SUCCESS;
626}
627
628
629/* API: Destroy stream. */
630static pj_status_t ios_stream_destroy(pjmedia_vid_stream *strm)
631{
632 struct ios_stream *stream = (struct ios_stream*)strm;
633
634 PJ_ASSERT_RETURN(stream != NULL, PJ_EINVAL);
635
636 ios_stream_stop(strm);
637
638 if (stream->imgView) {
639 [stream->imgView removeFromSuperview];
640 [stream->imgView release];
641 stream->imgView = NULL;
642 }
643
644 if (stream->cap_session) {
645 [stream->cap_session release];
646 stream->cap_session = NULL;
647 }
648/* if (stream->dev_input) {
649 [stream->dev_input release];
650 stream->dev_input = NULL;
651 }
652*/
653 if (stream->vout_delegate) {
654 [stream->vout_delegate release];
655 stream->vout_delegate = NULL;
656 }
657/* if (stream->video_output) {
658 [stream->video_output release];
659 stream->video_output = NULL;
660 }
661*/
662
663 pj_pool_release(stream->pool);
664
665 return PJ_SUCCESS;
666}
667
668#endif
669#endif /* PJMEDIA_VIDEO_DEV_HAS_IOS */