blob: 2ac8a9e12aa465ac311b0c4273c1b516d4de25c2 [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{
Sauw Mingb93e6882011-03-19 05:33:21 +000076 pjmedia_vid_dev_stream base; /**< Base stream */
77 pjmedia_vid_param param; /**< Settings */
78 pj_pool_t *pool; /**< Memory pool */
Sauw Ming6e6c2152010-12-14 13:03:10 +000079
Sauw Mingb93e6882011-03-19 05:33:21 +000080 pjmedia_vid_cb vid_cb; /**< Stream callback */
81 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;
Benny Prijono349037b2011-03-17 11:25:19 +000095
96 pj_timestamp frame_ts;
97 unsigned ts_inc;
Sauw Ming6e6c2152010-12-14 13:03:10 +000098};
99
100
101/* Prototypes */
102static pj_status_t ios_factory_init(pjmedia_vid_dev_factory *f);
103static pj_status_t ios_factory_destroy(pjmedia_vid_dev_factory *f);
Sauw Ming40518922011-06-21 10:23:53 +0000104static pj_status_t ios_factory_refresh(pjmedia_vid_dev_factory *f);
Sauw Ming6e6c2152010-12-14 13:03:10 +0000105static unsigned ios_factory_get_dev_count(pjmedia_vid_dev_factory *f);
106static pj_status_t ios_factory_get_dev_info(pjmedia_vid_dev_factory *f,
107 unsigned index,
108 pjmedia_vid_dev_info *info);
109static pj_status_t ios_factory_default_param(pj_pool_t *pool,
110 pjmedia_vid_dev_factory *f,
111 unsigned index,
112 pjmedia_vid_param *param);
Sauw Mingb93e6882011-03-19 05:33:21 +0000113static pj_status_t ios_factory_create_stream(
114 pjmedia_vid_dev_factory *f,
Benny Prijonoe9f70d82011-03-25 08:38:26 +0000115 pjmedia_vid_param *param,
Sauw Mingb93e6882011-03-19 05:33:21 +0000116 const pjmedia_vid_cb *cb,
117 void *user_data,
118 pjmedia_vid_dev_stream **p_vid_strm);
Sauw Ming6e6c2152010-12-14 13:03:10 +0000119
Sauw Mingb93e6882011-03-19 05:33:21 +0000120static pj_status_t ios_stream_get_param(pjmedia_vid_dev_stream *strm,
Sauw Ming6e6c2152010-12-14 13:03:10 +0000121 pjmedia_vid_param *param);
Sauw Mingb93e6882011-03-19 05:33:21 +0000122static pj_status_t ios_stream_get_cap(pjmedia_vid_dev_stream *strm,
Sauw Ming6e6c2152010-12-14 13:03:10 +0000123 pjmedia_vid_dev_cap cap,
124 void *value);
Sauw Mingb93e6882011-03-19 05:33:21 +0000125static pj_status_t ios_stream_set_cap(pjmedia_vid_dev_stream *strm,
Sauw Ming6e6c2152010-12-14 13:03:10 +0000126 pjmedia_vid_dev_cap cap,
127 const void *value);
Sauw Mingb93e6882011-03-19 05:33:21 +0000128static pj_status_t ios_stream_start(pjmedia_vid_dev_stream *strm);
129static pj_status_t ios_stream_put_frame(pjmedia_vid_dev_stream *strm,
Sauw Ming6e6c2152010-12-14 13:03:10 +0000130 const pjmedia_frame *frame);
Sauw Mingb93e6882011-03-19 05:33:21 +0000131static pj_status_t ios_stream_stop(pjmedia_vid_dev_stream *strm);
132static pj_status_t ios_stream_destroy(pjmedia_vid_dev_stream *strm);
Sauw Ming6e6c2152010-12-14 13:03:10 +0000133
134/* Operations */
135static pjmedia_vid_dev_factory_op factory_op =
136{
137 &ios_factory_init,
138 &ios_factory_destroy,
139 &ios_factory_get_dev_count,
140 &ios_factory_get_dev_info,
141 &ios_factory_default_param,
Sauw Ming40518922011-06-21 10:23:53 +0000142 &ios_factory_create_stream,
143 &ios_factory_refresh
Sauw Ming6e6c2152010-12-14 13:03:10 +0000144};
145
Sauw Mingb93e6882011-03-19 05:33:21 +0000146static pjmedia_vid_dev_stream_op stream_op =
Sauw Ming6e6c2152010-12-14 13:03:10 +0000147{
148 &ios_stream_get_param,
149 &ios_stream_get_cap,
150 &ios_stream_set_cap,
151 &ios_stream_start,
152 NULL,
153 &ios_stream_put_frame,
154 &ios_stream_stop,
155 &ios_stream_destroy
156};
157
158
159/****************************************************************************
160 * Factory operations
161 */
162/*
163 * Init ios_ video driver.
164 */
165pjmedia_vid_dev_factory* pjmedia_ios_factory(pj_pool_factory *pf)
166{
167 struct ios_factory *f;
168 pj_pool_t *pool;
169
170 pool = pj_pool_create(pf, "ios video", 512, 512, NULL);
171 f = PJ_POOL_ZALLOC_T(pool, struct ios_factory);
172 f->pf = pf;
173 f->pool = pool;
174 f->base.op = &factory_op;
175
176 return &f->base;
177}
178
179
180/* API: init factory */
181static pj_status_t ios_factory_init(pjmedia_vid_dev_factory *f)
182{
183 struct ios_factory *qf = (struct ios_factory*)f;
184 struct ios_dev_info *qdi;
185 unsigned i, l;
186
187 /* Initialize input and output devices here */
188 qf->dev_info = (struct ios_dev_info*)
189 pj_pool_calloc(qf->pool, 2,
190 sizeof(struct ios_dev_info));
191
192 qf->dev_count = 0;
193 qdi = &qf->dev_info[qf->dev_count++];
194 pj_bzero(qdi, sizeof(*qdi));
195 strcpy(qdi->info.name, "iOS UIView");
196 strcpy(qdi->info.driver, "iOS");
197 qdi->info.dir = PJMEDIA_DIR_RENDER;
Sauw Mingab494302010-12-17 13:17:23 +0000198 qdi->info.has_callback = PJ_FALSE;
199 qdi->info.caps = PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW;
Sauw Ming6e6c2152010-12-14 13:03:10 +0000200
201 if (NSClassFromString(@"AVCaptureSession")) {
202 qdi = &qf->dev_info[qf->dev_count++];
203 pj_bzero(qdi, sizeof(*qdi));
204 strcpy(qdi->info.name, "iOS AVCapture");
205 strcpy(qdi->info.driver, "iOS");
206 qdi->info.dir = PJMEDIA_DIR_CAPTURE;
207 qdi->info.has_callback = PJ_TRUE;
208 }
209
210 for (i = 0; i < qf->dev_count; i++) {
211 qdi = &qf->dev_info[i];
212 qdi->info.fmt_cnt = PJ_ARRAY_SIZE(ios_fmts);
Sauw Mingab494302010-12-17 13:17:23 +0000213 qdi->info.caps |= PJMEDIA_VID_DEV_CAP_FORMAT;
Sauw Ming6e6c2152010-12-14 13:03:10 +0000214 qdi->info.fmt = (pjmedia_format*)
215 pj_pool_calloc(qf->pool, qdi->info.fmt_cnt,
216 sizeof(pjmedia_format));
217
218 for (l = 0; l < PJ_ARRAY_SIZE(ios_fmts); l++) {
219 pjmedia_format *fmt = &qdi->info.fmt[l];
220 pjmedia_format_init_video(fmt,
221 ios_fmts[l].pjmedia_format,
222 DEFAULT_WIDTH,
223 DEFAULT_HEIGHT,
224 DEFAULT_FPS, 1);
225 }
226 }
227
228 PJ_LOG(4, (THIS_FILE, "iOS video initialized with %d devices",
229 qf->dev_count));
230
231 return PJ_SUCCESS;
232}
233
234/* API: destroy factory */
235static pj_status_t ios_factory_destroy(pjmedia_vid_dev_factory *f)
236{
237 struct ios_factory *qf = (struct ios_factory*)f;
238 pj_pool_t *pool = qf->pool;
239
240 qf->pool = NULL;
241 pj_pool_release(pool);
242
243 return PJ_SUCCESS;
244}
245
Sauw Ming40518922011-06-21 10:23:53 +0000246/* API: refresh the list of devices */
247static pj_status_t ios_factory_refresh(pjmedia_vid_dev_factory *f)
248{
249 PJ_UNUSED_ARG(f);
250 return PJ_SUCCESS;
251}
252
Sauw Ming6e6c2152010-12-14 13:03:10 +0000253/* API: get number of devices */
254static unsigned ios_factory_get_dev_count(pjmedia_vid_dev_factory *f)
255{
256 struct ios_factory *qf = (struct ios_factory*)f;
257 return qf->dev_count;
258}
259
260/* API: get device info */
261static pj_status_t ios_factory_get_dev_info(pjmedia_vid_dev_factory *f,
262 unsigned index,
263 pjmedia_vid_dev_info *info)
264{
265 struct ios_factory *qf = (struct ios_factory*)f;
266
267 PJ_ASSERT_RETURN(index < qf->dev_count, PJMEDIA_EVID_INVDEV);
268
269 pj_memcpy(info, &qf->dev_info[index].info, sizeof(*info));
270
271 return PJ_SUCCESS;
272}
273
274/* API: create default device parameter */
275static pj_status_t ios_factory_default_param(pj_pool_t *pool,
276 pjmedia_vid_dev_factory *f,
277 unsigned index,
278 pjmedia_vid_param *param)
279{
280 struct ios_factory *qf = (struct ios_factory*)f;
281 struct ios_dev_info *di = &qf->dev_info[index];
282
283 PJ_ASSERT_RETURN(index < qf->dev_count, PJMEDIA_EVID_INVDEV);
284
285 PJ_UNUSED_ARG(pool);
286
287 pj_bzero(param, sizeof(*param));
Sauw Mingf477e282011-06-09 04:13:50 +0000288 if (di->info.dir & PJMEDIA_DIR_CAPTURE) {
Sauw Ming6e6c2152010-12-14 13:03:10 +0000289 param->dir = PJMEDIA_DIR_CAPTURE;
290 param->cap_id = index;
291 param->rend_id = PJMEDIA_VID_INVALID_DEV;
292 } else if (di->info.dir & PJMEDIA_DIR_RENDER) {
293 param->dir = PJMEDIA_DIR_RENDER;
294 param->rend_id = index;
295 param->cap_id = PJMEDIA_VID_INVALID_DEV;
296 } else {
297 return PJMEDIA_EVID_INVDEV;
298 }
299
300 param->flags = PJMEDIA_VID_DEV_CAP_FORMAT;
301 param->clock_rate = DEFAULT_CLOCK_RATE;
Sauw Ming6e6c2152010-12-14 13:03:10 +0000302 pj_memcpy(&param->fmt, &di->info.fmt[0], sizeof(param->fmt));
303
304 return PJ_SUCCESS;
305}
306
307@implementation VOutDelegate
308- (void)update_image
309{
310 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
311
312 /* Create a device-dependent RGB color space */
313 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
314
315 /* Create a bitmap graphics context with the sample buffer data */
316 CGContextRef context =
317 CGBitmapContextCreate(stream->buf, stream->size.w, stream->size.h, 8,
318 stream->bytes_per_row, colorSpace,
319 kCGBitmapByteOrder32Little |
320 kCGImageAlphaPremultipliedFirst);
321
322 /**
323 * Create a Quartz image from the pixel data in the bitmap graphics
324 * context
325 */
326 CGImageRef quartzImage = CGBitmapContextCreateImage(context);
327
328 /* Free up the context and color space */
329 CGContextRelease(context);
330 CGColorSpaceRelease(colorSpace);
331
332 /* Create an image object from the Quartz image */
333 UIImage *image = [UIImage imageWithCGImage:quartzImage scale:1.0
334 orientation:UIImageOrientationRight];
335
336 /* Release the Quartz image */
337 CGImageRelease(quartzImage);
338
Sauw Mingab494302010-12-17 13:17:23 +0000339 [stream->imgView performSelectorOnMainThread:@selector(setImage:)
340 withObject:image waitUntilDone:NO];
Sauw Ming6e6c2152010-12-14 13:03:10 +0000341
342 [pool release];
343}
344
345- (void)captureOutput:(AVCaptureOutput *)captureOutput
346 didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
347 fromConnection:(AVCaptureConnection *)connection
348{
349 pjmedia_frame frame;
350 CVImageBufferRef imageBuffer;
351
352 if (!sampleBuffer)
353 return;
354
355 /* Get a CMSampleBuffer's Core Video image buffer for the media data */
356 imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
357
358 /* Lock the base address of the pixel buffer */
359 CVPixelBufferLockBaseAddress(imageBuffer, 0);
360
361 frame.type = PJMEDIA_TYPE_VIDEO;
362 frame.buf = CVPixelBufferGetBaseAddress(imageBuffer);
363 frame.size = stream->frame_size;
364 frame.bit_info = 0;
Benny Prijono349037b2011-03-17 11:25:19 +0000365 frame.timestamp.u64 = stream->frame_ts.u64;
366
Sauw Ming6e6c2152010-12-14 13:03:10 +0000367 if (stream->vid_cb.capture_cb)
368 (*stream->vid_cb.capture_cb)(&stream->base, stream->user_data, &frame);
369
Benny Prijono349037b2011-03-17 11:25:19 +0000370 stream->frame_ts.u64 += stream->ts_inc;
371
Sauw Ming6e6c2152010-12-14 13:03:10 +0000372 /* Unlock the pixel buffer */
373 CVPixelBufferUnlockBaseAddress(imageBuffer,0);
374}
375@end
376
377static ios_fmt_info* get_ios_format_info(pjmedia_format_id id)
378{
379 unsigned i;
380
381 for (i = 0; i < PJ_ARRAY_SIZE(ios_fmts); i++) {
382 if (ios_fmts[i].pjmedia_format == id)
383 return &ios_fmts[i];
384 }
385
386 return NULL;
387}
388
389/* API: create stream */
Sauw Mingb93e6882011-03-19 05:33:21 +0000390static pj_status_t ios_factory_create_stream(
391 pjmedia_vid_dev_factory *f,
Benny Prijonoe9f70d82011-03-25 08:38:26 +0000392 pjmedia_vid_param *param,
Sauw Mingb93e6882011-03-19 05:33:21 +0000393 const pjmedia_vid_cb *cb,
394 void *user_data,
395 pjmedia_vid_dev_stream **p_vid_strm)
Sauw Ming6e6c2152010-12-14 13:03:10 +0000396{
397 struct ios_factory *qf = (struct ios_factory*)f;
398 pj_pool_t *pool;
399 struct ios_stream *strm;
Benny Prijono349037b2011-03-17 11:25:19 +0000400 const pjmedia_video_format_detail *vfd;
Sauw Ming6e6c2152010-12-14 13:03:10 +0000401 const pjmedia_video_format_info *vfi;
402 pj_status_t status = PJ_SUCCESS;
403 ios_fmt_info *ifi = get_ios_format_info(param->fmt.id);
404 NSError *error;
405
406 PJ_ASSERT_RETURN(f && param && p_vid_strm, PJ_EINVAL);
407 PJ_ASSERT_RETURN(param->fmt.type == PJMEDIA_TYPE_VIDEO &&
Sauw Mingf477e282011-06-09 04:13:50 +0000408 param->fmt.detail_type == PJMEDIA_FORMAT_DETAIL_VIDEO &&
409 (param->dir == PJMEDIA_DIR_CAPTURE ||
410 param->dir == PJMEDIA_DIR_RENDER),
Sauw Ming6e6c2152010-12-14 13:03:10 +0000411 PJ_EINVAL);
412
413 if (!(ifi = get_ios_format_info(param->fmt.id)))
414 return PJMEDIA_EVID_BADFORMAT;
415
416 vfi = pjmedia_get_video_format_info(NULL, param->fmt.id);
417 if (!vfi)
418 return PJMEDIA_EVID_BADFORMAT;
419
420 /* Create and Initialize stream descriptor */
421 pool = pj_pool_create(qf->pf, "ios-dev", 4000, 4000, NULL);
422 PJ_ASSERT_RETURN(pool != NULL, PJ_ENOMEM);
423
424 strm = PJ_POOL_ZALLOC_T(pool, struct ios_stream);
425 pj_memcpy(&strm->param, param, sizeof(*param));
426 strm->pool = pool;
427 pj_memcpy(&strm->vid_cb, cb, sizeof(*cb));
428 strm->user_data = user_data;
Sauw Mingef1dd2c2011-07-15 02:04:03 +0000429 pjmedia_event_publisher_init(&strm->base.epub, PJMEDIA_SIG_VID_DEV_IOS);
Sauw Ming6e6c2152010-12-14 13:03:10 +0000430
431 vfd = pjmedia_format_get_video_format_detail(&strm->param.fmt, PJ_TRUE);
432 pj_memcpy(&strm->size, &vfd->size, sizeof(vfd->size));
433 strm->bpp = vfi->bpp;
434 strm->bytes_per_row = strm->size.w * strm->bpp / 8;
435 strm->frame_size = strm->bytes_per_row * strm->size.h;
Benny Prijono349037b2011-03-17 11:25:19 +0000436 strm->ts_inc = PJMEDIA_SPF2(param->clock_rate, &vfd->fps, 1);
Sauw Ming6e6c2152010-12-14 13:03:10 +0000437
Sauw Ming6e6c2152010-12-14 13:03:10 +0000438 if (param->dir & PJMEDIA_DIR_CAPTURE) {
Sauw Mingf477e282011-06-09 04:13:50 +0000439 /* Create capture stream here */
Sauw Ming6e6c2152010-12-14 13:03:10 +0000440 strm->cap_session = [[AVCaptureSession alloc] init];
441 if (!strm->cap_session) {
442 status = PJ_ENOMEM;
443 goto on_error;
444 }
445 strm->cap_session.sessionPreset = AVCaptureSessionPresetMedium;
446
447 /* Open video device */
448 AVCaptureDevice *videoDevice =
449 [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
450 if (!videoDevice) {
451 status = PJMEDIA_EVID_SYSERR;
452 goto on_error;
453 }
454
455 /* Add the video device to the session as a device input */
456 strm->dev_input = [AVCaptureDeviceInput
457 deviceInputWithDevice:videoDevice
458 error: &error];
459 if (!strm->dev_input) {
460 status = PJMEDIA_EVID_SYSERR;
461 goto on_error;
462 }
463 [strm->cap_session addInput:strm->dev_input];
464
465 strm->video_output = [[[AVCaptureVideoDataOutput alloc] init]
466 autorelease];
467 if (!strm->video_output) {
468 status = PJMEDIA_EVID_SYSERR;
469 goto on_error;
470 }
471 [strm->cap_session addOutput:strm->video_output];
472
Sauw Mingab494302010-12-17 13:17:23 +0000473 /* Configure the video output */
Sauw Ming6e6c2152010-12-14 13:03:10 +0000474 strm->vout_delegate = [VOutDelegate alloc];
475 strm->vout_delegate->stream = strm;
476 dispatch_queue_t queue = dispatch_queue_create("myQueue", NULL);
477 [strm->video_output setSampleBufferDelegate:strm->vout_delegate
478 queue:queue];
479 dispatch_release(queue);
480
481 strm->video_output.videoSettings =
482 [NSDictionary dictionaryWithObjectsAndKeys:
483 [NSNumber numberWithInt:ifi->ios_format],
484 kCVPixelBufferPixelFormatTypeKey,
485 [NSNumber numberWithInt: vfd->size.w],
486 kCVPixelBufferWidthKey,
487 [NSNumber numberWithInt: vfd->size.h],
488 kCVPixelBufferHeightKey, nil];
489 strm->video_output.minFrameDuration = CMTimeMake(vfd->fps.denum,
490 vfd->fps.num);
Sauw Mingf477e282011-06-09 04:13:50 +0000491 } else if (param->dir & PJMEDIA_DIR_RENDER) {
492 /* Create renderer stream here */
Sauw Ming6e6c2152010-12-14 13:03:10 +0000493 /* Get the main window */
494 UIWindow *window = [[UIApplication sharedApplication] keyWindow];
495
Sauw Mingab494302010-12-17 13:17:23 +0000496 if (param->flags & PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW && param->window)
Sauw Mingb93e6882011-03-19 05:33:21 +0000497 window = (UIWindow *)param->window;
Sauw Mingab494302010-12-17 13:17:23 +0000498
Sauw Ming6e6c2152010-12-14 13:03:10 +0000499 pj_assert(window);
500 strm->imgView = [[UIImageView alloc] initWithFrame:[window bounds]];
501 if (!strm->imgView) {
502 status = PJ_ENOMEM;
503 goto on_error;
504 }
505 [window addSubview:strm->imgView];
506
507 if (!strm->vout_delegate) {
508 strm->vout_delegate = [VOutDelegate alloc];
509 strm->vout_delegate->stream = strm;
510 }
511
512 strm->buf = pj_pool_alloc(pool, strm->frame_size);
513 }
514
515 /* Apply the remaining settings */
516 /*
517 if (param->flags & PJMEDIA_VID_DEV_CAP_INPUT_SCALE) {
518 ios_stream_set_cap(&strm->base,
519 PJMEDIA_VID_DEV_CAP_INPUT_SCALE,
520 &param->fmt);
521 }
522 */
523 /* Done */
524 strm->base.op = &stream_op;
525 *p_vid_strm = &strm->base;
526
527 return PJ_SUCCESS;
528
529on_error:
Sauw Mingb93e6882011-03-19 05:33:21 +0000530 ios_stream_destroy((pjmedia_vid_dev_stream *)strm);
Sauw Ming6e6c2152010-12-14 13:03:10 +0000531
532 return status;
533}
534
535/* API: Get stream info. */
Sauw Mingb93e6882011-03-19 05:33:21 +0000536static pj_status_t ios_stream_get_param(pjmedia_vid_dev_stream *s,
Sauw Ming6e6c2152010-12-14 13:03:10 +0000537 pjmedia_vid_param *pi)
538{
539 struct ios_stream *strm = (struct ios_stream*)s;
540
541 PJ_ASSERT_RETURN(strm && pi, PJ_EINVAL);
542
543 pj_memcpy(pi, &strm->param, sizeof(*pi));
544
545/* if (ios_stream_get_cap(s, PJMEDIA_VID_DEV_CAP_INPUT_SCALE,
546 &pi->fmt.info_size) == PJ_SUCCESS)
547 {
548 pi->flags |= PJMEDIA_VID_DEV_CAP_INPUT_SCALE;
549 }
550*/
551 return PJ_SUCCESS;
552}
553
554/* API: get capability */
Sauw Mingb93e6882011-03-19 05:33:21 +0000555static pj_status_t ios_stream_get_cap(pjmedia_vid_dev_stream *s,
Sauw Ming6e6c2152010-12-14 13:03:10 +0000556 pjmedia_vid_dev_cap cap,
557 void *pval)
558{
559 struct ios_stream *strm = (struct ios_stream*)s;
560
561 PJ_UNUSED_ARG(strm);
562
563 PJ_ASSERT_RETURN(s && pval, PJ_EINVAL);
564
565 if (cap==PJMEDIA_VID_DEV_CAP_INPUT_SCALE)
566 {
567 return PJMEDIA_EVID_INVCAP;
568// return PJ_SUCCESS;
569 } else {
570 return PJMEDIA_EVID_INVCAP;
571 }
572}
573
574/* API: set capability */
Sauw Mingb93e6882011-03-19 05:33:21 +0000575static pj_status_t ios_stream_set_cap(pjmedia_vid_dev_stream *s,
Sauw Ming6e6c2152010-12-14 13:03:10 +0000576 pjmedia_vid_dev_cap cap,
577 const void *pval)
578{
579 struct ios_stream *strm = (struct ios_stream*)s;
580
581 PJ_UNUSED_ARG(strm);
582
583 PJ_ASSERT_RETURN(s && pval, PJ_EINVAL);
584
585 if (cap==PJMEDIA_VID_DEV_CAP_INPUT_SCALE)
586 {
587 return PJ_SUCCESS;
588 }
589
590 return PJMEDIA_EVID_INVCAP;
591}
592
593/* API: Start stream. */
Sauw Mingb93e6882011-03-19 05:33:21 +0000594static pj_status_t ios_stream_start(pjmedia_vid_dev_stream *strm)
Sauw Ming6e6c2152010-12-14 13:03:10 +0000595{
596 struct ios_stream *stream = (struct ios_stream*)strm;
597
598 PJ_UNUSED_ARG(stream);
599
600 PJ_LOG(4, (THIS_FILE, "Starting qt video stream"));
601
602 if (stream->cap_session) {
603 [stream->cap_session startRunning];
604
605 if (![stream->cap_session isRunning])
606 return PJ_EUNKNOWN;
607 }
608
609 return PJ_SUCCESS;
610}
611
612
613/* API: Put frame from stream */
Sauw Mingb93e6882011-03-19 05:33:21 +0000614static pj_status_t ios_stream_put_frame(pjmedia_vid_dev_stream *strm,
Sauw Ming6e6c2152010-12-14 13:03:10 +0000615 const pjmedia_frame *frame)
616{
617 struct ios_stream *stream = (struct ios_stream*)strm;
618 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
619
620 pj_assert(stream->frame_size >= frame->size);
621 pj_memcpy(stream->buf, frame->buf, frame->size);
622 /* Perform video display in a background thread */
623// [stream->vout_delegate update_image];
624 [NSThread detachNewThreadSelector:@selector(update_image)
625 toTarget:stream->vout_delegate withObject:nil];
626
627 [pool release];
628
629 return PJ_SUCCESS;
630}
631
632/* API: Stop stream. */
Sauw Mingb93e6882011-03-19 05:33:21 +0000633static pj_status_t ios_stream_stop(pjmedia_vid_dev_stream *strm)
Sauw Ming6e6c2152010-12-14 13:03:10 +0000634{
635 struct ios_stream *stream = (struct ios_stream*)strm;
636
637 PJ_UNUSED_ARG(stream);
638
639 PJ_LOG(4, (THIS_FILE, "Stopping qt video stream"));
640
641 if (stream->cap_session && [stream->cap_session isRunning])
642 [stream->cap_session stopRunning];
643
644 return PJ_SUCCESS;
645}
646
647
648/* API: Destroy stream. */
Sauw Mingb93e6882011-03-19 05:33:21 +0000649static pj_status_t ios_stream_destroy(pjmedia_vid_dev_stream *strm)
Sauw Ming6e6c2152010-12-14 13:03:10 +0000650{
651 struct ios_stream *stream = (struct ios_stream*)strm;
652
653 PJ_ASSERT_RETURN(stream != NULL, PJ_EINVAL);
654
655 ios_stream_stop(strm);
656
657 if (stream->imgView) {
658 [stream->imgView removeFromSuperview];
659 [stream->imgView release];
660 stream->imgView = NULL;
661 }
662
663 if (stream->cap_session) {
664 [stream->cap_session release];
665 stream->cap_session = NULL;
666 }
667/* if (stream->dev_input) {
668 [stream->dev_input release];
669 stream->dev_input = NULL;
670 }
671*/
672 if (stream->vout_delegate) {
673 [stream->vout_delegate release];
674 stream->vout_delegate = NULL;
675 }
676/* if (stream->video_output) {
677 [stream->video_output release];
678 stream->video_output = NULL;
679 }
680*/
681
682 pj_pool_release(stream->pool);
683
684 return PJ_SUCCESS;
685}
686
687#endif
688#endif /* PJMEDIA_VIDEO_DEV_HAS_IOS */