blob: 93b165f1687ff0576eea64d693530733e7be9526 [file] [log] [blame]
Sauw Ming6e6c2152010-12-14 13:03:10 +00001/* $Id$ */
2/*
Nanang Izzuddinfad6f692011-08-19 09:35:25 +00003 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
Sauw Ming6e6c2152010-12-14 13:03:10 +00004 *
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{
Sauw Mingb93e6882011-03-19 05:33:21 +000076 pjmedia_vid_dev_stream base; /**< Base stream */
Sauw Ming2fe2b5e2011-07-15 08:36:23 +000077 pjmedia_vid_dev_param param; /**< Settings */
Sauw Mingb93e6882011-03-19 05:33:21 +000078 pj_pool_t *pool; /**< Memory pool */
Sauw Ming6e6c2152010-12-14 13:03:10 +000079
Sauw Ming2fe2b5e2011-07-15 08:36:23 +000080 pjmedia_vid_dev_cb vid_cb; /**< Stream callback */
Sauw Mingb93e6882011-03-19 05:33:21 +000081 void *user_data; /**< Application data */
Sauw Ming6e6c2152010-12-14 13:03:10 +000082
Sauw Mingb93e6882011-03-19 05:33:21 +000083 pjmedia_rect_size size;
84 pj_uint8_t bpp;
85 unsigned bytes_per_row;
86 unsigned frame_size;
Sauw Ming6e6c2152010-12-14 13:03:10 +000087
88 AVCaptureSession *cap_session;
89 AVCaptureDeviceInput *dev_input;
90 AVCaptureVideoDataOutput *video_output;
91 VOutDelegate *vout_delegate;
92
93 UIImageView *imgView;
94 void *buf;
Sauw Ming0cf69d12011-09-16 02:32:12 +000095 dispatch_queue_t render_queue;
Benny Prijono349037b2011-03-17 11:25:19 +000096
97 pj_timestamp frame_ts;
98 unsigned ts_inc;
Sauw Ming6e6c2152010-12-14 13:03:10 +000099};
100
101
102/* Prototypes */
103static pj_status_t ios_factory_init(pjmedia_vid_dev_factory *f);
104static pj_status_t ios_factory_destroy(pjmedia_vid_dev_factory *f);
Sauw Ming40518922011-06-21 10:23:53 +0000105static pj_status_t ios_factory_refresh(pjmedia_vid_dev_factory *f);
Sauw Ming6e6c2152010-12-14 13:03:10 +0000106static unsigned ios_factory_get_dev_count(pjmedia_vid_dev_factory *f);
107static pj_status_t ios_factory_get_dev_info(pjmedia_vid_dev_factory *f,
108 unsigned index,
109 pjmedia_vid_dev_info *info);
110static pj_status_t ios_factory_default_param(pj_pool_t *pool,
111 pjmedia_vid_dev_factory *f,
112 unsigned index,
Sauw Ming2fe2b5e2011-07-15 08:36:23 +0000113 pjmedia_vid_dev_param *param);
Sauw Mingb93e6882011-03-19 05:33:21 +0000114static pj_status_t ios_factory_create_stream(
115 pjmedia_vid_dev_factory *f,
Sauw Ming2fe2b5e2011-07-15 08:36:23 +0000116 pjmedia_vid_dev_param *param,
117 const pjmedia_vid_dev_cb *cb,
Sauw Mingb93e6882011-03-19 05:33:21 +0000118 void *user_data,
119 pjmedia_vid_dev_stream **p_vid_strm);
Sauw Ming6e6c2152010-12-14 13:03:10 +0000120
Sauw Mingb93e6882011-03-19 05:33:21 +0000121static pj_status_t ios_stream_get_param(pjmedia_vid_dev_stream *strm,
Sauw Ming2fe2b5e2011-07-15 08:36:23 +0000122 pjmedia_vid_dev_param *param);
Sauw Mingb93e6882011-03-19 05:33:21 +0000123static pj_status_t ios_stream_get_cap(pjmedia_vid_dev_stream *strm,
Sauw Ming6e6c2152010-12-14 13:03:10 +0000124 pjmedia_vid_dev_cap cap,
125 void *value);
Sauw Mingb93e6882011-03-19 05:33:21 +0000126static pj_status_t ios_stream_set_cap(pjmedia_vid_dev_stream *strm,
Sauw Ming6e6c2152010-12-14 13:03:10 +0000127 pjmedia_vid_dev_cap cap,
128 const void *value);
Sauw Mingb93e6882011-03-19 05:33:21 +0000129static pj_status_t ios_stream_start(pjmedia_vid_dev_stream *strm);
130static pj_status_t ios_stream_put_frame(pjmedia_vid_dev_stream *strm,
Sauw Ming6e6c2152010-12-14 13:03:10 +0000131 const pjmedia_frame *frame);
Sauw Mingb93e6882011-03-19 05:33:21 +0000132static pj_status_t ios_stream_stop(pjmedia_vid_dev_stream *strm);
133static pj_status_t ios_stream_destroy(pjmedia_vid_dev_stream *strm);
Sauw Ming6e6c2152010-12-14 13:03:10 +0000134
135/* Operations */
136static pjmedia_vid_dev_factory_op factory_op =
137{
138 &ios_factory_init,
139 &ios_factory_destroy,
140 &ios_factory_get_dev_count,
141 &ios_factory_get_dev_info,
142 &ios_factory_default_param,
Sauw Ming40518922011-06-21 10:23:53 +0000143 &ios_factory_create_stream,
144 &ios_factory_refresh
Sauw Ming6e6c2152010-12-14 13:03:10 +0000145};
146
Sauw Mingb93e6882011-03-19 05:33:21 +0000147static pjmedia_vid_dev_stream_op stream_op =
Sauw Ming6e6c2152010-12-14 13:03:10 +0000148{
149 &ios_stream_get_param,
150 &ios_stream_get_cap,
151 &ios_stream_set_cap,
152 &ios_stream_start,
153 NULL,
154 &ios_stream_put_frame,
155 &ios_stream_stop,
156 &ios_stream_destroy
157};
158
159
160/****************************************************************************
161 * Factory operations
162 */
163/*
164 * Init ios_ video driver.
165 */
166pjmedia_vid_dev_factory* pjmedia_ios_factory(pj_pool_factory *pf)
167{
168 struct ios_factory *f;
169 pj_pool_t *pool;
170
171 pool = pj_pool_create(pf, "ios video", 512, 512, NULL);
172 f = PJ_POOL_ZALLOC_T(pool, struct ios_factory);
173 f->pf = pf;
174 f->pool = pool;
175 f->base.op = &factory_op;
176
177 return &f->base;
178}
179
180
181/* API: init factory */
182static pj_status_t ios_factory_init(pjmedia_vid_dev_factory *f)
183{
184 struct ios_factory *qf = (struct ios_factory*)f;
185 struct ios_dev_info *qdi;
186 unsigned i, l;
187
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));
192
193 qf->dev_count = 0;
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;
Sauw Mingab494302010-12-17 13:17:23 +0000199 qdi->info.has_callback = PJ_FALSE;
200 qdi->info.caps = PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW;
Sauw Ming6e6c2152010-12-14 13:03:10 +0000201
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;
209 }
210
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);
Sauw Mingab494302010-12-17 13:17:23 +0000214 qdi->info.caps |= PJMEDIA_VID_DEV_CAP_FORMAT;
Sauw Ming6e6c2152010-12-14 13:03:10 +0000215
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,
220 DEFAULT_WIDTH,
221 DEFAULT_HEIGHT,
222 DEFAULT_FPS, 1);
223 }
224 }
225
226 PJ_LOG(4, (THIS_FILE, "iOS video initialized with %d devices",
227 qf->dev_count));
228
229 return PJ_SUCCESS;
230}
231
232/* API: destroy factory */
233static pj_status_t ios_factory_destroy(pjmedia_vid_dev_factory *f)
234{
235 struct ios_factory *qf = (struct ios_factory*)f;
236 pj_pool_t *pool = qf->pool;
237
238 qf->pool = NULL;
239 pj_pool_release(pool);
240
241 return PJ_SUCCESS;
242}
243
Sauw Ming40518922011-06-21 10:23:53 +0000244/* API: refresh the list of devices */
245static pj_status_t ios_factory_refresh(pjmedia_vid_dev_factory *f)
246{
247 PJ_UNUSED_ARG(f);
248 return PJ_SUCCESS;
249}
250
Sauw Ming6e6c2152010-12-14 13:03:10 +0000251/* API: get number of devices */
252static unsigned ios_factory_get_dev_count(pjmedia_vid_dev_factory *f)
253{
254 struct ios_factory *qf = (struct ios_factory*)f;
255 return qf->dev_count;
256}
257
258/* API: get device info */
259static pj_status_t ios_factory_get_dev_info(pjmedia_vid_dev_factory *f,
260 unsigned index,
261 pjmedia_vid_dev_info *info)
262{
263 struct ios_factory *qf = (struct ios_factory*)f;
264
265 PJ_ASSERT_RETURN(index < qf->dev_count, PJMEDIA_EVID_INVDEV);
266
267 pj_memcpy(info, &qf->dev_info[index].info, sizeof(*info));
268
269 return PJ_SUCCESS;
270}
271
272/* API: create default device parameter */
273static pj_status_t ios_factory_default_param(pj_pool_t *pool,
274 pjmedia_vid_dev_factory *f,
275 unsigned index,
Sauw Ming2fe2b5e2011-07-15 08:36:23 +0000276 pjmedia_vid_dev_param *param)
Sauw Ming6e6c2152010-12-14 13:03:10 +0000277{
278 struct ios_factory *qf = (struct ios_factory*)f;
279 struct ios_dev_info *di = &qf->dev_info[index];
280
281 PJ_ASSERT_RETURN(index < qf->dev_count, PJMEDIA_EVID_INVDEV);
282
283 PJ_UNUSED_ARG(pool);
284
285 pj_bzero(param, sizeof(*param));
Sauw Mingf477e282011-06-09 04:13:50 +0000286 if (di->info.dir & PJMEDIA_DIR_CAPTURE) {
Sauw Ming6e6c2152010-12-14 13:03:10 +0000287 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;
294 } else {
295 return PJMEDIA_EVID_INVDEV;
296 }
297
298 param->flags = PJMEDIA_VID_DEV_CAP_FORMAT;
299 param->clock_rate = DEFAULT_CLOCK_RATE;
Sauw Ming6e6c2152010-12-14 13:03:10 +0000300 pj_memcpy(&param->fmt, &di->info.fmt[0], sizeof(param->fmt));
301
302 return PJ_SUCCESS;
303}
304
305@implementation VOutDelegate
306- (void)update_image
307{
308 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
309
310 /* Create a device-dependent RGB color space */
311 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
312
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);
319
320 /**
321 * Create a Quartz image from the pixel data in the bitmap graphics
322 * context
323 */
324 CGImageRef quartzImage = CGBitmapContextCreateImage(context);
325
326 /* Free up the context and color space */
327 CGContextRelease(context);
328 CGColorSpaceRelease(colorSpace);
329
330 /* Create an image object from the Quartz image */
331 UIImage *image = [UIImage imageWithCGImage:quartzImage scale:1.0
332 orientation:UIImageOrientationRight];
333
334 /* Release the Quartz image */
335 CGImageRelease(quartzImage);
336
Sauw Ming0cf69d12011-09-16 02:32:12 +0000337 dispatch_async(dispatch_get_main_queue(),
338 ^{[stream->imgView setImage:image];});
339 /*
Sauw Mingab494302010-12-17 13:17:23 +0000340 [stream->imgView performSelectorOnMainThread:@selector(setImage:)
341 withObject:image waitUntilDone:NO];
Sauw Ming0cf69d12011-09-16 02:32:12 +0000342 */
Sauw Ming6e6c2152010-12-14 13:03:10 +0000343
344 [pool release];
345}
346
347- (void)captureOutput:(AVCaptureOutput *)captureOutput
348 didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
349 fromConnection:(AVCaptureConnection *)connection
350{
351 pjmedia_frame frame;
352 CVImageBufferRef imageBuffer;
353
354 if (!sampleBuffer)
355 return;
356
357 /* Get a CMSampleBuffer's Core Video image buffer for the media data */
358 imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
359
360 /* Lock the base address of the pixel buffer */
361 CVPixelBufferLockBaseAddress(imageBuffer, 0);
362
363 frame.type = PJMEDIA_TYPE_VIDEO;
364 frame.buf = CVPixelBufferGetBaseAddress(imageBuffer);
365 frame.size = stream->frame_size;
366 frame.bit_info = 0;
Benny Prijono349037b2011-03-17 11:25:19 +0000367 frame.timestamp.u64 = stream->frame_ts.u64;
368
Sauw Ming6e6c2152010-12-14 13:03:10 +0000369 if (stream->vid_cb.capture_cb)
370 (*stream->vid_cb.capture_cb)(&stream->base, stream->user_data, &frame);
371
Benny Prijono349037b2011-03-17 11:25:19 +0000372 stream->frame_ts.u64 += stream->ts_inc;
373
Sauw Ming6e6c2152010-12-14 13:03:10 +0000374 /* Unlock the pixel buffer */
375 CVPixelBufferUnlockBaseAddress(imageBuffer,0);
376}
377@end
378
379static ios_fmt_info* get_ios_format_info(pjmedia_format_id id)
380{
381 unsigned i;
382
383 for (i = 0; i < PJ_ARRAY_SIZE(ios_fmts); i++) {
384 if (ios_fmts[i].pjmedia_format == id)
385 return &ios_fmts[i];
386 }
387
388 return NULL;
389}
390
391/* API: create stream */
Sauw Mingb93e6882011-03-19 05:33:21 +0000392static pj_status_t ios_factory_create_stream(
393 pjmedia_vid_dev_factory *f,
Sauw Ming2fe2b5e2011-07-15 08:36:23 +0000394 pjmedia_vid_dev_param *param,
395 const pjmedia_vid_dev_cb *cb,
Sauw Mingb93e6882011-03-19 05:33:21 +0000396 void *user_data,
397 pjmedia_vid_dev_stream **p_vid_strm)
Sauw Ming6e6c2152010-12-14 13:03:10 +0000398{
399 struct ios_factory *qf = (struct ios_factory*)f;
400 pj_pool_t *pool;
401 struct ios_stream *strm;
Benny Prijono349037b2011-03-17 11:25:19 +0000402 const pjmedia_video_format_detail *vfd;
Sauw Ming6e6c2152010-12-14 13:03:10 +0000403 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);
406 NSError *error;
407
408 PJ_ASSERT_RETURN(f && param && p_vid_strm, PJ_EINVAL);
409 PJ_ASSERT_RETURN(param->fmt.type == PJMEDIA_TYPE_VIDEO &&
Sauw Mingf477e282011-06-09 04:13:50 +0000410 param->fmt.detail_type == PJMEDIA_FORMAT_DETAIL_VIDEO &&
411 (param->dir == PJMEDIA_DIR_CAPTURE ||
412 param->dir == PJMEDIA_DIR_RENDER),
Sauw Ming6e6c2152010-12-14 13:03:10 +0000413 PJ_EINVAL);
414
415 if (!(ifi = get_ios_format_info(param->fmt.id)))
416 return PJMEDIA_EVID_BADFORMAT;
417
418 vfi = pjmedia_get_video_format_info(NULL, param->fmt.id);
419 if (!vfi)
420 return PJMEDIA_EVID_BADFORMAT;
421
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);
425
426 strm = PJ_POOL_ZALLOC_T(pool, struct ios_stream);
427 pj_memcpy(&strm->param, param, sizeof(*param));
428 strm->pool = pool;
429 pj_memcpy(&strm->vid_cb, cb, sizeof(*cb));
430 strm->user_data = user_data;
Sauw Mingef1dd2c2011-07-15 02:04:03 +0000431 pjmedia_event_publisher_init(&strm->base.epub, PJMEDIA_SIG_VID_DEV_IOS);
Sauw Ming6e6c2152010-12-14 13:03:10 +0000432
433 vfd = pjmedia_format_get_video_format_detail(&strm->param.fmt, PJ_TRUE);
434 pj_memcpy(&strm->size, &vfd->size, sizeof(vfd->size));
435 strm->bpp = vfi->bpp;
436 strm->bytes_per_row = strm->size.w * strm->bpp / 8;
437 strm->frame_size = strm->bytes_per_row * strm->size.h;
Benny Prijono349037b2011-03-17 11:25:19 +0000438 strm->ts_inc = PJMEDIA_SPF2(param->clock_rate, &vfd->fps, 1);
Sauw Ming6e6c2152010-12-14 13:03:10 +0000439
Sauw Ming6e6c2152010-12-14 13:03:10 +0000440 if (param->dir & PJMEDIA_DIR_CAPTURE) {
Sauw Mingf477e282011-06-09 04:13:50 +0000441 /* Create capture stream here */
Sauw Ming6e6c2152010-12-14 13:03:10 +0000442 strm->cap_session = [[AVCaptureSession alloc] init];
443 if (!strm->cap_session) {
444 status = PJ_ENOMEM;
445 goto on_error;
446 }
447 strm->cap_session.sessionPreset = AVCaptureSessionPresetMedium;
448
449 /* Open video device */
450 AVCaptureDevice *videoDevice =
451 [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
452 if (!videoDevice) {
453 status = PJMEDIA_EVID_SYSERR;
454 goto on_error;
455 }
456
457 /* Add the video device to the session as a device input */
458 strm->dev_input = [AVCaptureDeviceInput
459 deviceInputWithDevice:videoDevice
460 error: &error];
461 if (!strm->dev_input) {
462 status = PJMEDIA_EVID_SYSERR;
463 goto on_error;
464 }
465 [strm->cap_session addInput:strm->dev_input];
466
467 strm->video_output = [[[AVCaptureVideoDataOutput alloc] init]
468 autorelease];
469 if (!strm->video_output) {
470 status = PJMEDIA_EVID_SYSERR;
471 goto on_error;
472 }
473 [strm->cap_session addOutput:strm->video_output];
474
Sauw Mingab494302010-12-17 13:17:23 +0000475 /* Configure the video output */
Sauw Ming6e6c2152010-12-14 13:03:10 +0000476 strm->vout_delegate = [VOutDelegate alloc];
477 strm->vout_delegate->stream = strm;
478 dispatch_queue_t queue = dispatch_queue_create("myQueue", NULL);
479 [strm->video_output setSampleBufferDelegate:strm->vout_delegate
480 queue:queue];
481 dispatch_release(queue);
482
483 strm->video_output.videoSettings =
484 [NSDictionary dictionaryWithObjectsAndKeys:
485 [NSNumber numberWithInt:ifi->ios_format],
486 kCVPixelBufferPixelFormatTypeKey,
487 [NSNumber numberWithInt: vfd->size.w],
488 kCVPixelBufferWidthKey,
489 [NSNumber numberWithInt: vfd->size.h],
490 kCVPixelBufferHeightKey, nil];
491 strm->video_output.minFrameDuration = CMTimeMake(vfd->fps.denum,
492 vfd->fps.num);
Sauw Mingf477e282011-06-09 04:13:50 +0000493 } else if (param->dir & PJMEDIA_DIR_RENDER) {
494 /* Create renderer stream here */
Sauw Ming6e6c2152010-12-14 13:03:10 +0000495 /* Get the main window */
496 UIWindow *window = [[UIApplication sharedApplication] keyWindow];
497
Sauw Ming5707f352011-08-09 03:31:40 +0000498 if (param->flags & PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW &&
499 param->window.info.ios.window)
500 window = (UIWindow *)param->window.info.ios.window;
Sauw Mingab494302010-12-17 13:17:23 +0000501
Sauw Ming6e6c2152010-12-14 13:03:10 +0000502 pj_assert(window);
503 strm->imgView = [[UIImageView alloc] initWithFrame:[window bounds]];
504 if (!strm->imgView) {
505 status = PJ_ENOMEM;
506 goto on_error;
507 }
508 [window addSubview:strm->imgView];
509
510 if (!strm->vout_delegate) {
511 strm->vout_delegate = [VOutDelegate alloc];
512 strm->vout_delegate->stream = strm;
513 }
Sauw Ming0cf69d12011-09-16 02:32:12 +0000514
515 strm->render_queue = dispatch_queue_create("com.pjsip.render_queue",
516 NULL);
517 if (!strm->render_queue)
518 goto on_error;
Sauw Ming6e6c2152010-12-14 13:03:10 +0000519
520 strm->buf = pj_pool_alloc(pool, strm->frame_size);
521 }
522
523 /* Apply the remaining settings */
524 /*
525 if (param->flags & PJMEDIA_VID_DEV_CAP_INPUT_SCALE) {
526 ios_stream_set_cap(&strm->base,
527 PJMEDIA_VID_DEV_CAP_INPUT_SCALE,
528 &param->fmt);
529 }
530 */
531 /* Done */
532 strm->base.op = &stream_op;
533 *p_vid_strm = &strm->base;
534
535 return PJ_SUCCESS;
536
537on_error:
Sauw Mingb93e6882011-03-19 05:33:21 +0000538 ios_stream_destroy((pjmedia_vid_dev_stream *)strm);
Sauw Ming6e6c2152010-12-14 13:03:10 +0000539
540 return status;
541}
542
543/* API: Get stream info. */
Sauw Mingb93e6882011-03-19 05:33:21 +0000544static pj_status_t ios_stream_get_param(pjmedia_vid_dev_stream *s,
Sauw Ming2fe2b5e2011-07-15 08:36:23 +0000545 pjmedia_vid_dev_param *pi)
Sauw Ming6e6c2152010-12-14 13:03:10 +0000546{
547 struct ios_stream *strm = (struct ios_stream*)s;
548
549 PJ_ASSERT_RETURN(strm && pi, PJ_EINVAL);
550
551 pj_memcpy(pi, &strm->param, sizeof(*pi));
552
553/* if (ios_stream_get_cap(s, PJMEDIA_VID_DEV_CAP_INPUT_SCALE,
554 &pi->fmt.info_size) == PJ_SUCCESS)
555 {
556 pi->flags |= PJMEDIA_VID_DEV_CAP_INPUT_SCALE;
557 }
558*/
559 return PJ_SUCCESS;
560}
561
562/* API: get capability */
Sauw Mingb93e6882011-03-19 05:33:21 +0000563static pj_status_t ios_stream_get_cap(pjmedia_vid_dev_stream *s,
Sauw Ming6e6c2152010-12-14 13:03:10 +0000564 pjmedia_vid_dev_cap cap,
565 void *pval)
566{
567 struct ios_stream *strm = (struct ios_stream*)s;
568
569 PJ_UNUSED_ARG(strm);
570
571 PJ_ASSERT_RETURN(s && pval, PJ_EINVAL);
572
573 if (cap==PJMEDIA_VID_DEV_CAP_INPUT_SCALE)
574 {
575 return PJMEDIA_EVID_INVCAP;
576// return PJ_SUCCESS;
577 } else {
578 return PJMEDIA_EVID_INVCAP;
579 }
580}
581
582/* API: set capability */
Sauw Mingb93e6882011-03-19 05:33:21 +0000583static pj_status_t ios_stream_set_cap(pjmedia_vid_dev_stream *s,
Sauw Ming6e6c2152010-12-14 13:03:10 +0000584 pjmedia_vid_dev_cap cap,
585 const void *pval)
586{
587 struct ios_stream *strm = (struct ios_stream*)s;
588
589 PJ_UNUSED_ARG(strm);
590
591 PJ_ASSERT_RETURN(s && pval, PJ_EINVAL);
592
593 if (cap==PJMEDIA_VID_DEV_CAP_INPUT_SCALE)
594 {
595 return PJ_SUCCESS;
596 }
597
598 return PJMEDIA_EVID_INVCAP;
599}
600
601/* API: Start stream. */
Sauw Mingb93e6882011-03-19 05:33:21 +0000602static pj_status_t ios_stream_start(pjmedia_vid_dev_stream *strm)
Sauw Ming6e6c2152010-12-14 13:03:10 +0000603{
604 struct ios_stream *stream = (struct ios_stream*)strm;
605
606 PJ_UNUSED_ARG(stream);
607
Sauw Ming0cf69d12011-09-16 02:32:12 +0000608 PJ_LOG(4, (THIS_FILE, "Starting ios video stream"));
Sauw Ming6e6c2152010-12-14 13:03:10 +0000609
610 if (stream->cap_session) {
611 [stream->cap_session startRunning];
612
613 if (![stream->cap_session isRunning])
614 return PJ_EUNKNOWN;
615 }
616
617 return PJ_SUCCESS;
618}
619
620
621/* API: Put frame from stream */
Sauw Mingb93e6882011-03-19 05:33:21 +0000622static pj_status_t ios_stream_put_frame(pjmedia_vid_dev_stream *strm,
Sauw Ming6e6c2152010-12-14 13:03:10 +0000623 const pjmedia_frame *frame)
624{
625 struct ios_stream *stream = (struct ios_stream*)strm;
626 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
627
628 pj_assert(stream->frame_size >= frame->size);
629 pj_memcpy(stream->buf, frame->buf, frame->size);
630 /* Perform video display in a background thread */
Sauw Ming0cf69d12011-09-16 02:32:12 +0000631/*
632 [stream->vout_delegate update_image];
Sauw Ming6e6c2152010-12-14 13:03:10 +0000633 [NSThread detachNewThreadSelector:@selector(update_image)
634 toTarget:stream->vout_delegate withObject:nil];
Sauw Ming0cf69d12011-09-16 02:32:12 +0000635*/
636 dispatch_async(stream->render_queue,
637 ^{[stream->vout_delegate update_image];});
Sauw Ming6e6c2152010-12-14 13:03:10 +0000638
639 [pool release];
640
641 return PJ_SUCCESS;
642}
643
644/* API: Stop stream. */
Sauw Mingb93e6882011-03-19 05:33:21 +0000645static pj_status_t ios_stream_stop(pjmedia_vid_dev_stream *strm)
Sauw Ming6e6c2152010-12-14 13:03:10 +0000646{
647 struct ios_stream *stream = (struct ios_stream*)strm;
648
649 PJ_UNUSED_ARG(stream);
650
Sauw Ming0cf69d12011-09-16 02:32:12 +0000651 PJ_LOG(4, (THIS_FILE, "Stopping ios video stream"));
Sauw Ming6e6c2152010-12-14 13:03:10 +0000652
653 if (stream->cap_session && [stream->cap_session isRunning])
654 [stream->cap_session stopRunning];
655
656 return PJ_SUCCESS;
657}
658
659
660/* API: Destroy stream. */
Sauw Mingb93e6882011-03-19 05:33:21 +0000661static pj_status_t ios_stream_destroy(pjmedia_vid_dev_stream *strm)
Sauw Ming6e6c2152010-12-14 13:03:10 +0000662{
663 struct ios_stream *stream = (struct ios_stream*)strm;
664
665 PJ_ASSERT_RETURN(stream != NULL, PJ_EINVAL);
666
667 ios_stream_stop(strm);
668
669 if (stream->imgView) {
670 [stream->imgView removeFromSuperview];
671 [stream->imgView release];
672 stream->imgView = NULL;
673 }
674
675 if (stream->cap_session) {
676 [stream->cap_session release];
677 stream->cap_session = NULL;
678 }
679/* if (stream->dev_input) {
680 [stream->dev_input release];
681 stream->dev_input = NULL;
682 }
683*/
684 if (stream->vout_delegate) {
685 [stream->vout_delegate release];
686 stream->vout_delegate = NULL;
687 }
688/* if (stream->video_output) {
689 [stream->video_output release];
690 stream->video_output = NULL;
691 }
692*/
Sauw Ming0cf69d12011-09-16 02:32:12 +0000693 if (stream->render_queue) {
694 dispatch_release(stream->render_queue);
695 stream->render_queue = NULL;
696 }
Sauw Ming6e6c2152010-12-14 13:03:10 +0000697
698 pj_pool_release(stream->pool);
699
700 return PJ_SUCCESS;
701}
702
703#endif
704#endif /* PJMEDIA_VIDEO_DEV_HAS_IOS */