1
/* $Id: ios_dev.m 3979 2012-03-20 08:55:33Z ming $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
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.
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.
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
19
#include <pjmedia-videodev/videodev_imp.h>
20
#include <pj/assert.h>
24
#if PJMEDIA_VIDEO_DEV_HAS_IOS
25
#include "Availability.h"
28
#import <UIKit/UIKit.h>
29
#import <AVFoundation/AVFoundation.h>
31
#define THIS_FILE "ios_dev.c"
32
#define DEFAULT_CLOCK_RATE 90000
33
#define DEFAULT_WIDTH 480
34
#define DEFAULT_HEIGHT 360
35
#define DEFAULT_FPS 15
37
typedef struct ios_fmt_info
39
pjmedia_format_id pjmedia_format;
43
static ios_fmt_info ios_fmts[] =
45
{PJMEDIA_FORMAT_BGRA, kCVPixelFormatType_32BGRA} ,
51
pjmedia_vid_dev_info info;
57
pjmedia_vid_dev_factory base;
62
struct ios_dev_info *dev_info;
65
@interface VOutDelegate: NSObject
66
<AVCaptureVideoDataOutputSampleBufferDelegate>
69
struct ios_stream *stream;
76
pjmedia_vid_dev_stream base; /**< Base stream */
77
pjmedia_vid_dev_param param; /**< Settings */
78
pj_pool_t *pool; /**< Memory pool */
80
pjmedia_vid_dev_cb vid_cb; /**< Stream callback */
81
void *user_data; /**< Application data */
83
pjmedia_rect_size size;
85
unsigned bytes_per_row;
88
AVCaptureSession *cap_session;
89
AVCaptureDeviceInput *dev_input;
90
AVCaptureVideoDataOutput *video_output;
91
VOutDelegate *vout_delegate;
95
dispatch_queue_t render_queue;
97
pj_timestamp frame_ts;
103
static pj_status_t ios_factory_init(pjmedia_vid_dev_factory *f);
104
static pj_status_t ios_factory_destroy(pjmedia_vid_dev_factory *f);
105
static pj_status_t ios_factory_refresh(pjmedia_vid_dev_factory *f);
106
static unsigned ios_factory_get_dev_count(pjmedia_vid_dev_factory *f);
107
static pj_status_t ios_factory_get_dev_info(pjmedia_vid_dev_factory *f,
109
pjmedia_vid_dev_info *info);
110
static pj_status_t ios_factory_default_param(pj_pool_t *pool,
111
pjmedia_vid_dev_factory *f,
113
pjmedia_vid_dev_param *param);
114
static pj_status_t ios_factory_create_stream(
115
pjmedia_vid_dev_factory *f,
116
pjmedia_vid_dev_param *param,
117
const pjmedia_vid_dev_cb *cb,
119
pjmedia_vid_dev_stream **p_vid_strm);
121
static pj_status_t ios_stream_get_param(pjmedia_vid_dev_stream *strm,
122
pjmedia_vid_dev_param *param);
123
static pj_status_t ios_stream_get_cap(pjmedia_vid_dev_stream *strm,
124
pjmedia_vid_dev_cap cap,
126
static pj_status_t ios_stream_set_cap(pjmedia_vid_dev_stream *strm,
127
pjmedia_vid_dev_cap cap,
129
static pj_status_t ios_stream_start(pjmedia_vid_dev_stream *strm);
130
static pj_status_t ios_stream_put_frame(pjmedia_vid_dev_stream *strm,
131
const pjmedia_frame *frame);
132
static pj_status_t ios_stream_stop(pjmedia_vid_dev_stream *strm);
133
static pj_status_t ios_stream_destroy(pjmedia_vid_dev_stream *strm);
136
static pjmedia_vid_dev_factory_op factory_op =
139
&ios_factory_destroy,
140
&ios_factory_get_dev_count,
141
&ios_factory_get_dev_info,
142
&ios_factory_default_param,
143
&ios_factory_create_stream,
147
static pjmedia_vid_dev_stream_op stream_op =
149
&ios_stream_get_param,
154
&ios_stream_put_frame,
160
/****************************************************************************
164
* Init ios_ video driver.
166
pjmedia_vid_dev_factory* pjmedia_ios_factory(pj_pool_factory *pf)
168
struct ios_factory *f;
171
pool = pj_pool_create(pf, "ios video", 512, 512, NULL);
172
f = PJ_POOL_ZALLOC_T(pool, struct ios_factory);
175
f->base.op = &factory_op;
181
/* API: init factory */
182
static pj_status_t ios_factory_init(pjmedia_vid_dev_factory *f)
184
struct ios_factory *qf = (struct ios_factory*)f;
185
struct ios_dev_info *qdi;
188
/* Initialize input and output devices here */
189
qf->dev_info = (struct ios_dev_info*)
190
pj_pool_calloc(qf->pool, 2,
191
sizeof(struct ios_dev_info));
194
qdi = &qf->dev_info[qf->dev_count++];
195
pj_bzero(qdi, sizeof(*qdi));
196
strcpy(qdi->info.name, "iOS UIView");
197
strcpy(qdi->info.driver, "iOS");
198
qdi->info.dir = PJMEDIA_DIR_RENDER;
199
qdi->info.has_callback = PJ_FALSE;
200
qdi->info.caps = PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW;
202
if (NSClassFromString(@"AVCaptureSession")) {
203
qdi = &qf->dev_info[qf->dev_count++];
204
pj_bzero(qdi, sizeof(*qdi));
205
strcpy(qdi->info.name, "iOS AVCapture");
206
strcpy(qdi->info.driver, "iOS");
207
qdi->info.dir = PJMEDIA_DIR_CAPTURE;
208
qdi->info.has_callback = PJ_TRUE;
211
for (i = 0; i < qf->dev_count; i++) {
212
qdi = &qf->dev_info[i];
213
qdi->info.fmt_cnt = PJ_ARRAY_SIZE(ios_fmts);
214
qdi->info.caps |= PJMEDIA_VID_DEV_CAP_FORMAT;
216
for (l = 0; l < PJ_ARRAY_SIZE(ios_fmts); l++) {
217
pjmedia_format *fmt = &qdi->info.fmt[l];
218
pjmedia_format_init_video(fmt,
219
ios_fmts[l].pjmedia_format,
226
PJ_LOG(4, (THIS_FILE, "iOS video initialized with %d devices",
232
/* API: destroy factory */
233
static pj_status_t ios_factory_destroy(pjmedia_vid_dev_factory *f)
235
struct ios_factory *qf = (struct ios_factory*)f;
236
pj_pool_t *pool = qf->pool;
239
pj_pool_release(pool);
244
/* API: refresh the list of devices */
245
static pj_status_t ios_factory_refresh(pjmedia_vid_dev_factory *f)
251
/* API: get number of devices */
252
static unsigned ios_factory_get_dev_count(pjmedia_vid_dev_factory *f)
254
struct ios_factory *qf = (struct ios_factory*)f;
255
return qf->dev_count;
258
/* API: get device info */
259
static pj_status_t ios_factory_get_dev_info(pjmedia_vid_dev_factory *f,
261
pjmedia_vid_dev_info *info)
263
struct ios_factory *qf = (struct ios_factory*)f;
265
PJ_ASSERT_RETURN(index < qf->dev_count, PJMEDIA_EVID_INVDEV);
267
pj_memcpy(info, &qf->dev_info[index].info, sizeof(*info));
272
/* API: create default device parameter */
273
static pj_status_t ios_factory_default_param(pj_pool_t *pool,
274
pjmedia_vid_dev_factory *f,
276
pjmedia_vid_dev_param *param)
278
struct ios_factory *qf = (struct ios_factory*)f;
279
struct ios_dev_info *di = &qf->dev_info[index];
281
PJ_ASSERT_RETURN(index < qf->dev_count, PJMEDIA_EVID_INVDEV);
285
pj_bzero(param, sizeof(*param));
286
if (di->info.dir & PJMEDIA_DIR_CAPTURE) {
287
param->dir = PJMEDIA_DIR_CAPTURE;
288
param->cap_id = index;
289
param->rend_id = PJMEDIA_VID_INVALID_DEV;
290
} else if (di->info.dir & PJMEDIA_DIR_RENDER) {
291
param->dir = PJMEDIA_DIR_RENDER;
292
param->rend_id = index;
293
param->cap_id = PJMEDIA_VID_INVALID_DEV;
295
return PJMEDIA_EVID_INVDEV;
298
param->flags = PJMEDIA_VID_DEV_CAP_FORMAT;
299
param->clock_rate = DEFAULT_CLOCK_RATE;
300
pj_memcpy(¶m->fmt, &di->info.fmt[0], sizeof(param->fmt));
305
@implementation VOutDelegate
308
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
310
/* Create a device-dependent RGB color space */
311
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
313
/* Create a bitmap graphics context with the sample buffer data */
314
CGContextRef context =
315
CGBitmapContextCreate(stream->buf, stream->size.w, stream->size.h, 8,
316
stream->bytes_per_row, colorSpace,
317
kCGBitmapByteOrder32Little |
318
kCGImageAlphaPremultipliedFirst);
321
* Create a Quartz image from the pixel data in the bitmap graphics
324
CGImageRef quartzImage = CGBitmapContextCreateImage(context);
326
/* Free up the context and color space */
327
CGContextRelease(context);
328
CGColorSpaceRelease(colorSpace);
330
/* Create an image object from the Quartz image */
331
UIImage *image = [UIImage imageWithCGImage:quartzImage scale:1.0
332
orientation:UIImageOrientationRight];
334
/* Release the Quartz image */
335
CGImageRelease(quartzImage);
337
dispatch_async(dispatch_get_main_queue(),
338
^{[stream->imgView setImage:image];});
340
[stream->imgView performSelectorOnMainThread:@selector(setImage:)
341
withObject:image waitUntilDone:NO];
347
- (void)captureOutput:(AVCaptureOutput *)captureOutput
348
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
349
fromConnection:(AVCaptureConnection *)connection
352
CVImageBufferRef imageBuffer;
357
/* Get a CMSampleBuffer's Core Video image buffer for the media data */
358
imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
360
/* Lock the base address of the pixel buffer */
361
CVPixelBufferLockBaseAddress(imageBuffer, 0);
363
frame.type = PJMEDIA_FRAME_TYPE_VIDEO;
364
frame.buf = CVPixelBufferGetBaseAddress(imageBuffer);
365
frame.size = stream->frame_size;
367
frame.timestamp.u64 = stream->frame_ts.u64;
369
if (stream->vid_cb.capture_cb)
370
(*stream->vid_cb.capture_cb)(&stream->base, stream->user_data, &frame);
372
stream->frame_ts.u64 += stream->ts_inc;
374
/* Unlock the pixel buffer */
375
CVPixelBufferUnlockBaseAddress(imageBuffer,0);
379
static ios_fmt_info* get_ios_format_info(pjmedia_format_id id)
383
for (i = 0; i < PJ_ARRAY_SIZE(ios_fmts); i++) {
384
if (ios_fmts[i].pjmedia_format == id)
391
/* API: create stream */
392
static pj_status_t ios_factory_create_stream(
393
pjmedia_vid_dev_factory *f,
394
pjmedia_vid_dev_param *param,
395
const pjmedia_vid_dev_cb *cb,
397
pjmedia_vid_dev_stream **p_vid_strm)
399
struct ios_factory *qf = (struct ios_factory*)f;
401
struct ios_stream *strm;
402
const pjmedia_video_format_detail *vfd;
403
const pjmedia_video_format_info *vfi;
404
pj_status_t status = PJ_SUCCESS;
405
ios_fmt_info *ifi = get_ios_format_info(param->fmt.id);
408
PJ_ASSERT_RETURN(f && param && p_vid_strm, PJ_EINVAL);
409
PJ_ASSERT_RETURN(param->fmt.type == PJMEDIA_TYPE_VIDEO &&
410
param->fmt.detail_type == PJMEDIA_FORMAT_DETAIL_VIDEO &&
411
(param->dir == PJMEDIA_DIR_CAPTURE ||
412
param->dir == PJMEDIA_DIR_RENDER),
415
if (!(ifi = get_ios_format_info(param->fmt.id)))
416
return PJMEDIA_EVID_BADFORMAT;
418
vfi = pjmedia_get_video_format_info(NULL, param->fmt.id);
420
return PJMEDIA_EVID_BADFORMAT;
422
/* Create and Initialize stream descriptor */
423
pool = pj_pool_create(qf->pf, "ios-dev", 4000, 4000, NULL);
424
PJ_ASSERT_RETURN(pool != NULL, PJ_ENOMEM);
426
strm = PJ_POOL_ZALLOC_T(pool, struct ios_stream);
427
pj_memcpy(&strm->param, param, sizeof(*param));
429
pj_memcpy(&strm->vid_cb, cb, sizeof(*cb));
430
strm->user_data = user_data;
432
vfd = pjmedia_format_get_video_format_detail(&strm->param.fmt, PJ_TRUE);
433
pj_memcpy(&strm->size, &vfd->size, sizeof(vfd->size));
434
strm->bpp = vfi->bpp;
435
strm->bytes_per_row = strm->size.w * strm->bpp / 8;
436
strm->frame_size = strm->bytes_per_row * strm->size.h;
437
strm->ts_inc = PJMEDIA_SPF2(param->clock_rate, &vfd->fps, 1);
439
if (param->dir & PJMEDIA_DIR_CAPTURE) {
440
/* Create capture stream here */
441
strm->cap_session = [[AVCaptureSession alloc] init];
442
if (!strm->cap_session) {
446
strm->cap_session.sessionPreset = AVCaptureSessionPresetMedium;
448
/* Open video device */
449
AVCaptureDevice *videoDevice =
450
[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
452
status = PJMEDIA_EVID_SYSERR;
456
/* Add the video device to the session as a device input */
457
strm->dev_input = [AVCaptureDeviceInput
458
deviceInputWithDevice:videoDevice
460
if (!strm->dev_input) {
461
status = PJMEDIA_EVID_SYSERR;
464
[strm->cap_session addInput:strm->dev_input];
466
strm->video_output = [[[AVCaptureVideoDataOutput alloc] init]
468
if (!strm->video_output) {
469
status = PJMEDIA_EVID_SYSERR;
472
[strm->cap_session addOutput:strm->video_output];
474
/* Configure the video output */
475
strm->vout_delegate = [VOutDelegate alloc];
476
strm->vout_delegate->stream = strm;
477
dispatch_queue_t queue = dispatch_queue_create("myQueue", NULL);
478
[strm->video_output setSampleBufferDelegate:strm->vout_delegate
480
dispatch_release(queue);
482
strm->video_output.videoSettings =
483
[NSDictionary dictionaryWithObjectsAndKeys:
484
[NSNumber numberWithInt:ifi->ios_format],
485
kCVPixelBufferPixelFormatTypeKey,
486
[NSNumber numberWithInt: vfd->size.w],
487
kCVPixelBufferWidthKey,
488
[NSNumber numberWithInt: vfd->size.h],
489
kCVPixelBufferHeightKey, nil];
490
strm->video_output.minFrameDuration = CMTimeMake(vfd->fps.denum,
492
} else if (param->dir & PJMEDIA_DIR_RENDER) {
493
/* Create renderer stream here */
494
/* Get the main window */
495
UIWindow *window = [[UIApplication sharedApplication] keyWindow];
497
if (param->flags & PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW &&
498
param->window.info.ios.window)
499
window = (UIWindow *)param->window.info.ios.window;
502
strm->imgView = [[UIImageView alloc] initWithFrame:[window bounds]];
503
if (!strm->imgView) {
507
[window addSubview:strm->imgView];
509
if (!strm->vout_delegate) {
510
strm->vout_delegate = [VOutDelegate alloc];
511
strm->vout_delegate->stream = strm;
514
strm->render_queue = dispatch_queue_create("com.pjsip.render_queue",
516
if (!strm->render_queue)
519
strm->buf = pj_pool_alloc(pool, strm->frame_size);
522
/* Apply the remaining settings */
524
if (param->flags & PJMEDIA_VID_DEV_CAP_INPUT_SCALE) {
525
ios_stream_set_cap(&strm->base,
526
PJMEDIA_VID_DEV_CAP_INPUT_SCALE,
531
strm->base.op = &stream_op;
532
*p_vid_strm = &strm->base;
537
ios_stream_destroy((pjmedia_vid_dev_stream *)strm);
542
/* API: Get stream info. */
543
static pj_status_t ios_stream_get_param(pjmedia_vid_dev_stream *s,
544
pjmedia_vid_dev_param *pi)
546
struct ios_stream *strm = (struct ios_stream*)s;
548
PJ_ASSERT_RETURN(strm && pi, PJ_EINVAL);
550
pj_memcpy(pi, &strm->param, sizeof(*pi));
552
/* if (ios_stream_get_cap(s, PJMEDIA_VID_DEV_CAP_INPUT_SCALE,
553
&pi->fmt.info_size) == PJ_SUCCESS)
555
pi->flags |= PJMEDIA_VID_DEV_CAP_INPUT_SCALE;
561
/* API: get capability */
562
static pj_status_t ios_stream_get_cap(pjmedia_vid_dev_stream *s,
563
pjmedia_vid_dev_cap cap,
566
struct ios_stream *strm = (struct ios_stream*)s;
570
PJ_ASSERT_RETURN(s && pval, PJ_EINVAL);
572
if (cap==PJMEDIA_VID_DEV_CAP_INPUT_SCALE)
574
return PJMEDIA_EVID_INVCAP;
575
// return PJ_SUCCESS;
577
return PJMEDIA_EVID_INVCAP;
581
/* API: set capability */
582
static pj_status_t ios_stream_set_cap(pjmedia_vid_dev_stream *s,
583
pjmedia_vid_dev_cap cap,
586
struct ios_stream *strm = (struct ios_stream*)s;
590
PJ_ASSERT_RETURN(s && pval, PJ_EINVAL);
592
if (cap==PJMEDIA_VID_DEV_CAP_INPUT_SCALE)
597
return PJMEDIA_EVID_INVCAP;
600
/* API: Start stream. */
601
static pj_status_t ios_stream_start(pjmedia_vid_dev_stream *strm)
603
struct ios_stream *stream = (struct ios_stream*)strm;
605
PJ_UNUSED_ARG(stream);
607
PJ_LOG(4, (THIS_FILE, "Starting ios video stream"));
609
if (stream->cap_session) {
610
[stream->cap_session startRunning];
612
if (![stream->cap_session isRunning])
620
/* API: Put frame from stream */
621
static pj_status_t ios_stream_put_frame(pjmedia_vid_dev_stream *strm,
622
const pjmedia_frame *frame)
624
struct ios_stream *stream = (struct ios_stream*)strm;
625
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
627
pj_assert(stream->frame_size >= frame->size);
628
pj_memcpy(stream->buf, frame->buf, frame->size);
629
/* Perform video display in a background thread */
631
[stream->vout_delegate update_image];
632
[NSThread detachNewThreadSelector:@selector(update_image)
633
toTarget:stream->vout_delegate withObject:nil];
635
dispatch_async(stream->render_queue,
636
^{[stream->vout_delegate update_image];});
643
/* API: Stop stream. */
644
static pj_status_t ios_stream_stop(pjmedia_vid_dev_stream *strm)
646
struct ios_stream *stream = (struct ios_stream*)strm;
648
PJ_UNUSED_ARG(stream);
650
PJ_LOG(4, (THIS_FILE, "Stopping ios video stream"));
652
if (stream->cap_session && [stream->cap_session isRunning])
653
[stream->cap_session stopRunning];
659
/* API: Destroy stream. */
660
static pj_status_t ios_stream_destroy(pjmedia_vid_dev_stream *strm)
662
struct ios_stream *stream = (struct ios_stream*)strm;
664
PJ_ASSERT_RETURN(stream != NULL, PJ_EINVAL);
666
ios_stream_stop(strm);
668
if (stream->imgView) {
669
[stream->imgView removeFromSuperview];
670
[stream->imgView release];
671
stream->imgView = NULL;
674
if (stream->cap_session) {
675
[stream->cap_session release];
676
stream->cap_session = NULL;
678
/* if (stream->dev_input) {
679
[stream->dev_input release];
680
stream->dev_input = NULL;
683
if (stream->vout_delegate) {
684
[stream->vout_delegate release];
685
stream->vout_delegate = NULL;
687
/* if (stream->video_output) {
688
[stream->video_output release];
689
stream->video_output = NULL;
692
if (stream->render_queue) {
693
dispatch_release(stream->render_queue);
694
stream->render_queue = NULL;
697
pj_pool_release(stream->pool);
703
#endif /* PJMEDIA_VIDEO_DEV_HAS_IOS */