Sauw Ming | 6e6c215 | 2010-12-14 13:03:10 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
Nanang Izzuddin | fad6f69 | 2011-08-19 09:35:25 +0000 | [diff] [blame] | 3 | * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com) |
Sauw Ming | 6e6c215 | 2010-12-14 13:03:10 +0000 | [diff] [blame] | 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 | |
| 37 | typedef struct ios_fmt_info |
| 38 | { |
| 39 | pjmedia_format_id pjmedia_format; |
| 40 | UInt32 ios_format; |
| 41 | } ios_fmt_info; |
| 42 | |
| 43 | static ios_fmt_info ios_fmts[] = |
| 44 | { |
| 45 | {PJMEDIA_FORMAT_BGRA, kCVPixelFormatType_32BGRA} , |
| 46 | }; |
| 47 | |
| 48 | /* qt device info */ |
| 49 | struct ios_dev_info |
| 50 | { |
| 51 | pjmedia_vid_dev_info info; |
| 52 | }; |
| 53 | |
| 54 | /* qt factory */ |
| 55 | struct 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. */ |
| 74 | struct ios_stream |
| 75 | { |
Sauw Ming | b93e688 | 2011-03-19 05:33:21 +0000 | [diff] [blame] | 76 | pjmedia_vid_dev_stream base; /**< Base stream */ |
Sauw Ming | 2fe2b5e | 2011-07-15 08:36:23 +0000 | [diff] [blame] | 77 | pjmedia_vid_dev_param param; /**< Settings */ |
Sauw Ming | b93e688 | 2011-03-19 05:33:21 +0000 | [diff] [blame] | 78 | pj_pool_t *pool; /**< Memory pool */ |
Sauw Ming | 6e6c215 | 2010-12-14 13:03:10 +0000 | [diff] [blame] | 79 | |
Sauw Ming | 2fe2b5e | 2011-07-15 08:36:23 +0000 | [diff] [blame] | 80 | pjmedia_vid_dev_cb vid_cb; /**< Stream callback */ |
Sauw Ming | b93e688 | 2011-03-19 05:33:21 +0000 | [diff] [blame] | 81 | void *user_data; /**< Application data */ |
Sauw Ming | 6e6c215 | 2010-12-14 13:03:10 +0000 | [diff] [blame] | 82 | |
Sauw Ming | b93e688 | 2011-03-19 05:33:21 +0000 | [diff] [blame] | 83 | pjmedia_rect_size size; |
| 84 | pj_uint8_t bpp; |
| 85 | unsigned bytes_per_row; |
| 86 | unsigned frame_size; |
Sauw Ming | 6e6c215 | 2010-12-14 13:03:10 +0000 | [diff] [blame] | 87 | |
| 88 | AVCaptureSession *cap_session; |
| 89 | AVCaptureDeviceInput *dev_input; |
| 90 | AVCaptureVideoDataOutput *video_output; |
| 91 | VOutDelegate *vout_delegate; |
| 92 | |
| 93 | UIImageView *imgView; |
| 94 | void *buf; |
Benny Prijono | 349037b | 2011-03-17 11:25:19 +0000 | [diff] [blame] | 95 | |
| 96 | pj_timestamp frame_ts; |
| 97 | unsigned ts_inc; |
Sauw Ming | 6e6c215 | 2010-12-14 13:03:10 +0000 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | |
| 101 | /* Prototypes */ |
| 102 | static pj_status_t ios_factory_init(pjmedia_vid_dev_factory *f); |
| 103 | static pj_status_t ios_factory_destroy(pjmedia_vid_dev_factory *f); |
Sauw Ming | 4051892 | 2011-06-21 10:23:53 +0000 | [diff] [blame] | 104 | static pj_status_t ios_factory_refresh(pjmedia_vid_dev_factory *f); |
Sauw Ming | 6e6c215 | 2010-12-14 13:03:10 +0000 | [diff] [blame] | 105 | static unsigned ios_factory_get_dev_count(pjmedia_vid_dev_factory *f); |
| 106 | static pj_status_t ios_factory_get_dev_info(pjmedia_vid_dev_factory *f, |
| 107 | unsigned index, |
| 108 | pjmedia_vid_dev_info *info); |
| 109 | static pj_status_t ios_factory_default_param(pj_pool_t *pool, |
| 110 | pjmedia_vid_dev_factory *f, |
| 111 | unsigned index, |
Sauw Ming | 2fe2b5e | 2011-07-15 08:36:23 +0000 | [diff] [blame] | 112 | pjmedia_vid_dev_param *param); |
Sauw Ming | b93e688 | 2011-03-19 05:33:21 +0000 | [diff] [blame] | 113 | static pj_status_t ios_factory_create_stream( |
| 114 | pjmedia_vid_dev_factory *f, |
Sauw Ming | 2fe2b5e | 2011-07-15 08:36:23 +0000 | [diff] [blame] | 115 | pjmedia_vid_dev_param *param, |
| 116 | const pjmedia_vid_dev_cb *cb, |
Sauw Ming | b93e688 | 2011-03-19 05:33:21 +0000 | [diff] [blame] | 117 | void *user_data, |
| 118 | pjmedia_vid_dev_stream **p_vid_strm); |
Sauw Ming | 6e6c215 | 2010-12-14 13:03:10 +0000 | [diff] [blame] | 119 | |
Sauw Ming | b93e688 | 2011-03-19 05:33:21 +0000 | [diff] [blame] | 120 | static pj_status_t ios_stream_get_param(pjmedia_vid_dev_stream *strm, |
Sauw Ming | 2fe2b5e | 2011-07-15 08:36:23 +0000 | [diff] [blame] | 121 | pjmedia_vid_dev_param *param); |
Sauw Ming | b93e688 | 2011-03-19 05:33:21 +0000 | [diff] [blame] | 122 | static pj_status_t ios_stream_get_cap(pjmedia_vid_dev_stream *strm, |
Sauw Ming | 6e6c215 | 2010-12-14 13:03:10 +0000 | [diff] [blame] | 123 | pjmedia_vid_dev_cap cap, |
| 124 | void *value); |
Sauw Ming | b93e688 | 2011-03-19 05:33:21 +0000 | [diff] [blame] | 125 | static pj_status_t ios_stream_set_cap(pjmedia_vid_dev_stream *strm, |
Sauw Ming | 6e6c215 | 2010-12-14 13:03:10 +0000 | [diff] [blame] | 126 | pjmedia_vid_dev_cap cap, |
| 127 | const void *value); |
Sauw Ming | b93e688 | 2011-03-19 05:33:21 +0000 | [diff] [blame] | 128 | static pj_status_t ios_stream_start(pjmedia_vid_dev_stream *strm); |
| 129 | static pj_status_t ios_stream_put_frame(pjmedia_vid_dev_stream *strm, |
Sauw Ming | 6e6c215 | 2010-12-14 13:03:10 +0000 | [diff] [blame] | 130 | const pjmedia_frame *frame); |
Sauw Ming | b93e688 | 2011-03-19 05:33:21 +0000 | [diff] [blame] | 131 | static pj_status_t ios_stream_stop(pjmedia_vid_dev_stream *strm); |
| 132 | static pj_status_t ios_stream_destroy(pjmedia_vid_dev_stream *strm); |
Sauw Ming | 6e6c215 | 2010-12-14 13:03:10 +0000 | [diff] [blame] | 133 | |
| 134 | /* Operations */ |
| 135 | static 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 Ming | 4051892 | 2011-06-21 10:23:53 +0000 | [diff] [blame] | 142 | &ios_factory_create_stream, |
| 143 | &ios_factory_refresh |
Sauw Ming | 6e6c215 | 2010-12-14 13:03:10 +0000 | [diff] [blame] | 144 | }; |
| 145 | |
Sauw Ming | b93e688 | 2011-03-19 05:33:21 +0000 | [diff] [blame] | 146 | static pjmedia_vid_dev_stream_op stream_op = |
Sauw Ming | 6e6c215 | 2010-12-14 13:03:10 +0000 | [diff] [blame] | 147 | { |
| 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 | */ |
| 165 | pjmedia_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 */ |
| 181 | static 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 Ming | ab49430 | 2010-12-17 13:17:23 +0000 | [diff] [blame] | 198 | qdi->info.has_callback = PJ_FALSE; |
| 199 | qdi->info.caps = PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW; |
Sauw Ming | 6e6c215 | 2010-12-14 13:03:10 +0000 | [diff] [blame] | 200 | |
| 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 Ming | ab49430 | 2010-12-17 13:17:23 +0000 | [diff] [blame] | 213 | qdi->info.caps |= PJMEDIA_VID_DEV_CAP_FORMAT; |
Sauw Ming | 6e6c215 | 2010-12-14 13:03:10 +0000 | [diff] [blame] | 214 | |
| 215 | for (l = 0; l < PJ_ARRAY_SIZE(ios_fmts); l++) { |
| 216 | pjmedia_format *fmt = &qdi->info.fmt[l]; |
| 217 | pjmedia_format_init_video(fmt, |
| 218 | ios_fmts[l].pjmedia_format, |
| 219 | DEFAULT_WIDTH, |
| 220 | DEFAULT_HEIGHT, |
| 221 | DEFAULT_FPS, 1); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | PJ_LOG(4, (THIS_FILE, "iOS video initialized with %d devices", |
| 226 | qf->dev_count)); |
| 227 | |
| 228 | return PJ_SUCCESS; |
| 229 | } |
| 230 | |
| 231 | /* API: destroy factory */ |
| 232 | static pj_status_t ios_factory_destroy(pjmedia_vid_dev_factory *f) |
| 233 | { |
| 234 | struct ios_factory *qf = (struct ios_factory*)f; |
| 235 | pj_pool_t *pool = qf->pool; |
| 236 | |
| 237 | qf->pool = NULL; |
| 238 | pj_pool_release(pool); |
| 239 | |
| 240 | return PJ_SUCCESS; |
| 241 | } |
| 242 | |
Sauw Ming | 4051892 | 2011-06-21 10:23:53 +0000 | [diff] [blame] | 243 | /* API: refresh the list of devices */ |
| 244 | static pj_status_t ios_factory_refresh(pjmedia_vid_dev_factory *f) |
| 245 | { |
| 246 | PJ_UNUSED_ARG(f); |
| 247 | return PJ_SUCCESS; |
| 248 | } |
| 249 | |
Sauw Ming | 6e6c215 | 2010-12-14 13:03:10 +0000 | [diff] [blame] | 250 | /* API: get number of devices */ |
| 251 | static unsigned ios_factory_get_dev_count(pjmedia_vid_dev_factory *f) |
| 252 | { |
| 253 | struct ios_factory *qf = (struct ios_factory*)f; |
| 254 | return qf->dev_count; |
| 255 | } |
| 256 | |
| 257 | /* API: get device info */ |
| 258 | static pj_status_t ios_factory_get_dev_info(pjmedia_vid_dev_factory *f, |
| 259 | unsigned index, |
| 260 | pjmedia_vid_dev_info *info) |
| 261 | { |
| 262 | struct ios_factory *qf = (struct ios_factory*)f; |
| 263 | |
| 264 | PJ_ASSERT_RETURN(index < qf->dev_count, PJMEDIA_EVID_INVDEV); |
| 265 | |
| 266 | pj_memcpy(info, &qf->dev_info[index].info, sizeof(*info)); |
| 267 | |
| 268 | return PJ_SUCCESS; |
| 269 | } |
| 270 | |
| 271 | /* API: create default device parameter */ |
| 272 | static pj_status_t ios_factory_default_param(pj_pool_t *pool, |
| 273 | pjmedia_vid_dev_factory *f, |
| 274 | unsigned index, |
Sauw Ming | 2fe2b5e | 2011-07-15 08:36:23 +0000 | [diff] [blame] | 275 | pjmedia_vid_dev_param *param) |
Sauw Ming | 6e6c215 | 2010-12-14 13:03:10 +0000 | [diff] [blame] | 276 | { |
| 277 | struct ios_factory *qf = (struct ios_factory*)f; |
| 278 | struct ios_dev_info *di = &qf->dev_info[index]; |
| 279 | |
| 280 | PJ_ASSERT_RETURN(index < qf->dev_count, PJMEDIA_EVID_INVDEV); |
| 281 | |
| 282 | PJ_UNUSED_ARG(pool); |
| 283 | |
| 284 | pj_bzero(param, sizeof(*param)); |
Sauw Ming | f477e28 | 2011-06-09 04:13:50 +0000 | [diff] [blame] | 285 | if (di->info.dir & PJMEDIA_DIR_CAPTURE) { |
Sauw Ming | 6e6c215 | 2010-12-14 13:03:10 +0000 | [diff] [blame] | 286 | param->dir = PJMEDIA_DIR_CAPTURE; |
| 287 | param->cap_id = index; |
| 288 | param->rend_id = PJMEDIA_VID_INVALID_DEV; |
| 289 | } else if (di->info.dir & PJMEDIA_DIR_RENDER) { |
| 290 | param->dir = PJMEDIA_DIR_RENDER; |
| 291 | param->rend_id = index; |
| 292 | param->cap_id = PJMEDIA_VID_INVALID_DEV; |
| 293 | } else { |
| 294 | return PJMEDIA_EVID_INVDEV; |
| 295 | } |
| 296 | |
| 297 | param->flags = PJMEDIA_VID_DEV_CAP_FORMAT; |
| 298 | param->clock_rate = DEFAULT_CLOCK_RATE; |
Sauw Ming | 6e6c215 | 2010-12-14 13:03:10 +0000 | [diff] [blame] | 299 | pj_memcpy(¶m->fmt, &di->info.fmt[0], sizeof(param->fmt)); |
| 300 | |
| 301 | return PJ_SUCCESS; |
| 302 | } |
| 303 | |
| 304 | @implementation VOutDelegate |
| 305 | - (void)update_image |
| 306 | { |
| 307 | NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
| 308 | |
| 309 | /* Create a device-dependent RGB color space */ |
| 310 | CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); |
| 311 | |
| 312 | /* Create a bitmap graphics context with the sample buffer data */ |
| 313 | CGContextRef context = |
| 314 | CGBitmapContextCreate(stream->buf, stream->size.w, stream->size.h, 8, |
| 315 | stream->bytes_per_row, colorSpace, |
| 316 | kCGBitmapByteOrder32Little | |
| 317 | kCGImageAlphaPremultipliedFirst); |
| 318 | |
| 319 | /** |
| 320 | * Create a Quartz image from the pixel data in the bitmap graphics |
| 321 | * context |
| 322 | */ |
| 323 | CGImageRef quartzImage = CGBitmapContextCreateImage(context); |
| 324 | |
| 325 | /* Free up the context and color space */ |
| 326 | CGContextRelease(context); |
| 327 | CGColorSpaceRelease(colorSpace); |
| 328 | |
| 329 | /* Create an image object from the Quartz image */ |
| 330 | UIImage *image = [UIImage imageWithCGImage:quartzImage scale:1.0 |
| 331 | orientation:UIImageOrientationRight]; |
| 332 | |
| 333 | /* Release the Quartz image */ |
| 334 | CGImageRelease(quartzImage); |
| 335 | |
Sauw Ming | ab49430 | 2010-12-17 13:17:23 +0000 | [diff] [blame] | 336 | [stream->imgView performSelectorOnMainThread:@selector(setImage:) |
| 337 | withObject:image waitUntilDone:NO]; |
Sauw Ming | 6e6c215 | 2010-12-14 13:03:10 +0000 | [diff] [blame] | 338 | |
| 339 | [pool release]; |
| 340 | } |
| 341 | |
| 342 | - (void)captureOutput:(AVCaptureOutput *)captureOutput |
| 343 | didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer |
| 344 | fromConnection:(AVCaptureConnection *)connection |
| 345 | { |
| 346 | pjmedia_frame frame; |
| 347 | CVImageBufferRef imageBuffer; |
| 348 | |
| 349 | if (!sampleBuffer) |
| 350 | return; |
| 351 | |
| 352 | /* Get a CMSampleBuffer's Core Video image buffer for the media data */ |
| 353 | imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); |
| 354 | |
| 355 | /* Lock the base address of the pixel buffer */ |
| 356 | CVPixelBufferLockBaseAddress(imageBuffer, 0); |
| 357 | |
| 358 | frame.type = PJMEDIA_TYPE_VIDEO; |
| 359 | frame.buf = CVPixelBufferGetBaseAddress(imageBuffer); |
| 360 | frame.size = stream->frame_size; |
| 361 | frame.bit_info = 0; |
Benny Prijono | 349037b | 2011-03-17 11:25:19 +0000 | [diff] [blame] | 362 | frame.timestamp.u64 = stream->frame_ts.u64; |
| 363 | |
Sauw Ming | 6e6c215 | 2010-12-14 13:03:10 +0000 | [diff] [blame] | 364 | if (stream->vid_cb.capture_cb) |
| 365 | (*stream->vid_cb.capture_cb)(&stream->base, stream->user_data, &frame); |
| 366 | |
Benny Prijono | 349037b | 2011-03-17 11:25:19 +0000 | [diff] [blame] | 367 | stream->frame_ts.u64 += stream->ts_inc; |
| 368 | |
Sauw Ming | 6e6c215 | 2010-12-14 13:03:10 +0000 | [diff] [blame] | 369 | /* Unlock the pixel buffer */ |
| 370 | CVPixelBufferUnlockBaseAddress(imageBuffer,0); |
| 371 | } |
| 372 | @end |
| 373 | |
| 374 | static ios_fmt_info* get_ios_format_info(pjmedia_format_id id) |
| 375 | { |
| 376 | unsigned i; |
| 377 | |
| 378 | for (i = 0; i < PJ_ARRAY_SIZE(ios_fmts); i++) { |
| 379 | if (ios_fmts[i].pjmedia_format == id) |
| 380 | return &ios_fmts[i]; |
| 381 | } |
| 382 | |
| 383 | return NULL; |
| 384 | } |
| 385 | |
| 386 | /* API: create stream */ |
Sauw Ming | b93e688 | 2011-03-19 05:33:21 +0000 | [diff] [blame] | 387 | static pj_status_t ios_factory_create_stream( |
| 388 | pjmedia_vid_dev_factory *f, |
Sauw Ming | 2fe2b5e | 2011-07-15 08:36:23 +0000 | [diff] [blame] | 389 | pjmedia_vid_dev_param *param, |
| 390 | const pjmedia_vid_dev_cb *cb, |
Sauw Ming | b93e688 | 2011-03-19 05:33:21 +0000 | [diff] [blame] | 391 | void *user_data, |
| 392 | pjmedia_vid_dev_stream **p_vid_strm) |
Sauw Ming | 6e6c215 | 2010-12-14 13:03:10 +0000 | [diff] [blame] | 393 | { |
| 394 | struct ios_factory *qf = (struct ios_factory*)f; |
| 395 | pj_pool_t *pool; |
| 396 | struct ios_stream *strm; |
Benny Prijono | 349037b | 2011-03-17 11:25:19 +0000 | [diff] [blame] | 397 | const pjmedia_video_format_detail *vfd; |
Sauw Ming | 6e6c215 | 2010-12-14 13:03:10 +0000 | [diff] [blame] | 398 | const pjmedia_video_format_info *vfi; |
| 399 | pj_status_t status = PJ_SUCCESS; |
| 400 | ios_fmt_info *ifi = get_ios_format_info(param->fmt.id); |
| 401 | NSError *error; |
| 402 | |
| 403 | PJ_ASSERT_RETURN(f && param && p_vid_strm, PJ_EINVAL); |
| 404 | PJ_ASSERT_RETURN(param->fmt.type == PJMEDIA_TYPE_VIDEO && |
Sauw Ming | f477e28 | 2011-06-09 04:13:50 +0000 | [diff] [blame] | 405 | param->fmt.detail_type == PJMEDIA_FORMAT_DETAIL_VIDEO && |
| 406 | (param->dir == PJMEDIA_DIR_CAPTURE || |
| 407 | param->dir == PJMEDIA_DIR_RENDER), |
Sauw Ming | 6e6c215 | 2010-12-14 13:03:10 +0000 | [diff] [blame] | 408 | PJ_EINVAL); |
| 409 | |
| 410 | if (!(ifi = get_ios_format_info(param->fmt.id))) |
| 411 | return PJMEDIA_EVID_BADFORMAT; |
| 412 | |
| 413 | vfi = pjmedia_get_video_format_info(NULL, param->fmt.id); |
| 414 | if (!vfi) |
| 415 | return PJMEDIA_EVID_BADFORMAT; |
| 416 | |
| 417 | /* Create and Initialize stream descriptor */ |
| 418 | pool = pj_pool_create(qf->pf, "ios-dev", 4000, 4000, NULL); |
| 419 | PJ_ASSERT_RETURN(pool != NULL, PJ_ENOMEM); |
| 420 | |
| 421 | strm = PJ_POOL_ZALLOC_T(pool, struct ios_stream); |
| 422 | pj_memcpy(&strm->param, param, sizeof(*param)); |
| 423 | strm->pool = pool; |
| 424 | pj_memcpy(&strm->vid_cb, cb, sizeof(*cb)); |
| 425 | strm->user_data = user_data; |
Sauw Ming | ef1dd2c | 2011-07-15 02:04:03 +0000 | [diff] [blame] | 426 | pjmedia_event_publisher_init(&strm->base.epub, PJMEDIA_SIG_VID_DEV_IOS); |
Sauw Ming | 6e6c215 | 2010-12-14 13:03:10 +0000 | [diff] [blame] | 427 | |
| 428 | vfd = pjmedia_format_get_video_format_detail(&strm->param.fmt, PJ_TRUE); |
| 429 | pj_memcpy(&strm->size, &vfd->size, sizeof(vfd->size)); |
| 430 | strm->bpp = vfi->bpp; |
| 431 | strm->bytes_per_row = strm->size.w * strm->bpp / 8; |
| 432 | strm->frame_size = strm->bytes_per_row * strm->size.h; |
Benny Prijono | 349037b | 2011-03-17 11:25:19 +0000 | [diff] [blame] | 433 | strm->ts_inc = PJMEDIA_SPF2(param->clock_rate, &vfd->fps, 1); |
Sauw Ming | 6e6c215 | 2010-12-14 13:03:10 +0000 | [diff] [blame] | 434 | |
Sauw Ming | 6e6c215 | 2010-12-14 13:03:10 +0000 | [diff] [blame] | 435 | if (param->dir & PJMEDIA_DIR_CAPTURE) { |
Sauw Ming | f477e28 | 2011-06-09 04:13:50 +0000 | [diff] [blame] | 436 | /* Create capture stream here */ |
Sauw Ming | 6e6c215 | 2010-12-14 13:03:10 +0000 | [diff] [blame] | 437 | strm->cap_session = [[AVCaptureSession alloc] init]; |
| 438 | if (!strm->cap_session) { |
| 439 | status = PJ_ENOMEM; |
| 440 | goto on_error; |
| 441 | } |
| 442 | strm->cap_session.sessionPreset = AVCaptureSessionPresetMedium; |
| 443 | |
| 444 | /* Open video device */ |
| 445 | AVCaptureDevice *videoDevice = |
| 446 | [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; |
| 447 | if (!videoDevice) { |
| 448 | status = PJMEDIA_EVID_SYSERR; |
| 449 | goto on_error; |
| 450 | } |
| 451 | |
| 452 | /* Add the video device to the session as a device input */ |
| 453 | strm->dev_input = [AVCaptureDeviceInput |
| 454 | deviceInputWithDevice:videoDevice |
| 455 | error: &error]; |
| 456 | if (!strm->dev_input) { |
| 457 | status = PJMEDIA_EVID_SYSERR; |
| 458 | goto on_error; |
| 459 | } |
| 460 | [strm->cap_session addInput:strm->dev_input]; |
| 461 | |
| 462 | strm->video_output = [[[AVCaptureVideoDataOutput alloc] init] |
| 463 | autorelease]; |
| 464 | if (!strm->video_output) { |
| 465 | status = PJMEDIA_EVID_SYSERR; |
| 466 | goto on_error; |
| 467 | } |
| 468 | [strm->cap_session addOutput:strm->video_output]; |
| 469 | |
Sauw Ming | ab49430 | 2010-12-17 13:17:23 +0000 | [diff] [blame] | 470 | /* Configure the video output */ |
Sauw Ming | 6e6c215 | 2010-12-14 13:03:10 +0000 | [diff] [blame] | 471 | strm->vout_delegate = [VOutDelegate alloc]; |
| 472 | strm->vout_delegate->stream = strm; |
| 473 | dispatch_queue_t queue = dispatch_queue_create("myQueue", NULL); |
| 474 | [strm->video_output setSampleBufferDelegate:strm->vout_delegate |
| 475 | queue:queue]; |
| 476 | dispatch_release(queue); |
| 477 | |
| 478 | strm->video_output.videoSettings = |
| 479 | [NSDictionary dictionaryWithObjectsAndKeys: |
| 480 | [NSNumber numberWithInt:ifi->ios_format], |
| 481 | kCVPixelBufferPixelFormatTypeKey, |
| 482 | [NSNumber numberWithInt: vfd->size.w], |
| 483 | kCVPixelBufferWidthKey, |
| 484 | [NSNumber numberWithInt: vfd->size.h], |
| 485 | kCVPixelBufferHeightKey, nil]; |
| 486 | strm->video_output.minFrameDuration = CMTimeMake(vfd->fps.denum, |
| 487 | vfd->fps.num); |
Sauw Ming | f477e28 | 2011-06-09 04:13:50 +0000 | [diff] [blame] | 488 | } else if (param->dir & PJMEDIA_DIR_RENDER) { |
| 489 | /* Create renderer stream here */ |
Sauw Ming | 6e6c215 | 2010-12-14 13:03:10 +0000 | [diff] [blame] | 490 | /* Get the main window */ |
| 491 | UIWindow *window = [[UIApplication sharedApplication] keyWindow]; |
| 492 | |
Sauw Ming | 5707f35 | 2011-08-09 03:31:40 +0000 | [diff] [blame] | 493 | if (param->flags & PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW && |
| 494 | param->window.info.ios.window) |
| 495 | window = (UIWindow *)param->window.info.ios.window; |
Sauw Ming | ab49430 | 2010-12-17 13:17:23 +0000 | [diff] [blame] | 496 | |
Sauw Ming | 6e6c215 | 2010-12-14 13:03:10 +0000 | [diff] [blame] | 497 | pj_assert(window); |
| 498 | strm->imgView = [[UIImageView alloc] initWithFrame:[window bounds]]; |
| 499 | if (!strm->imgView) { |
| 500 | status = PJ_ENOMEM; |
| 501 | goto on_error; |
| 502 | } |
| 503 | [window addSubview:strm->imgView]; |
| 504 | |
| 505 | if (!strm->vout_delegate) { |
| 506 | strm->vout_delegate = [VOutDelegate alloc]; |
| 507 | strm->vout_delegate->stream = strm; |
| 508 | } |
| 509 | |
| 510 | strm->buf = pj_pool_alloc(pool, strm->frame_size); |
| 511 | } |
| 512 | |
| 513 | /* Apply the remaining settings */ |
| 514 | /* |
| 515 | if (param->flags & PJMEDIA_VID_DEV_CAP_INPUT_SCALE) { |
| 516 | ios_stream_set_cap(&strm->base, |
| 517 | PJMEDIA_VID_DEV_CAP_INPUT_SCALE, |
| 518 | ¶m->fmt); |
| 519 | } |
| 520 | */ |
| 521 | /* Done */ |
| 522 | strm->base.op = &stream_op; |
| 523 | *p_vid_strm = &strm->base; |
| 524 | |
| 525 | return PJ_SUCCESS; |
| 526 | |
| 527 | on_error: |
Sauw Ming | b93e688 | 2011-03-19 05:33:21 +0000 | [diff] [blame] | 528 | ios_stream_destroy((pjmedia_vid_dev_stream *)strm); |
Sauw Ming | 6e6c215 | 2010-12-14 13:03:10 +0000 | [diff] [blame] | 529 | |
| 530 | return status; |
| 531 | } |
| 532 | |
| 533 | /* API: Get stream info. */ |
Sauw Ming | b93e688 | 2011-03-19 05:33:21 +0000 | [diff] [blame] | 534 | static pj_status_t ios_stream_get_param(pjmedia_vid_dev_stream *s, |
Sauw Ming | 2fe2b5e | 2011-07-15 08:36:23 +0000 | [diff] [blame] | 535 | pjmedia_vid_dev_param *pi) |
Sauw Ming | 6e6c215 | 2010-12-14 13:03:10 +0000 | [diff] [blame] | 536 | { |
| 537 | struct ios_stream *strm = (struct ios_stream*)s; |
| 538 | |
| 539 | PJ_ASSERT_RETURN(strm && pi, PJ_EINVAL); |
| 540 | |
| 541 | pj_memcpy(pi, &strm->param, sizeof(*pi)); |
| 542 | |
| 543 | /* if (ios_stream_get_cap(s, PJMEDIA_VID_DEV_CAP_INPUT_SCALE, |
| 544 | &pi->fmt.info_size) == PJ_SUCCESS) |
| 545 | { |
| 546 | pi->flags |= PJMEDIA_VID_DEV_CAP_INPUT_SCALE; |
| 547 | } |
| 548 | */ |
| 549 | return PJ_SUCCESS; |
| 550 | } |
| 551 | |
| 552 | /* API: get capability */ |
Sauw Ming | b93e688 | 2011-03-19 05:33:21 +0000 | [diff] [blame] | 553 | static pj_status_t ios_stream_get_cap(pjmedia_vid_dev_stream *s, |
Sauw Ming | 6e6c215 | 2010-12-14 13:03:10 +0000 | [diff] [blame] | 554 | pjmedia_vid_dev_cap cap, |
| 555 | void *pval) |
| 556 | { |
| 557 | struct ios_stream *strm = (struct ios_stream*)s; |
| 558 | |
| 559 | PJ_UNUSED_ARG(strm); |
| 560 | |
| 561 | PJ_ASSERT_RETURN(s && pval, PJ_EINVAL); |
| 562 | |
| 563 | if (cap==PJMEDIA_VID_DEV_CAP_INPUT_SCALE) |
| 564 | { |
| 565 | return PJMEDIA_EVID_INVCAP; |
| 566 | // return PJ_SUCCESS; |
| 567 | } else { |
| 568 | return PJMEDIA_EVID_INVCAP; |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | /* API: set capability */ |
Sauw Ming | b93e688 | 2011-03-19 05:33:21 +0000 | [diff] [blame] | 573 | static pj_status_t ios_stream_set_cap(pjmedia_vid_dev_stream *s, |
Sauw Ming | 6e6c215 | 2010-12-14 13:03:10 +0000 | [diff] [blame] | 574 | pjmedia_vid_dev_cap cap, |
| 575 | const void *pval) |
| 576 | { |
| 577 | struct ios_stream *strm = (struct ios_stream*)s; |
| 578 | |
| 579 | PJ_UNUSED_ARG(strm); |
| 580 | |
| 581 | PJ_ASSERT_RETURN(s && pval, PJ_EINVAL); |
| 582 | |
| 583 | if (cap==PJMEDIA_VID_DEV_CAP_INPUT_SCALE) |
| 584 | { |
| 585 | return PJ_SUCCESS; |
| 586 | } |
| 587 | |
| 588 | return PJMEDIA_EVID_INVCAP; |
| 589 | } |
| 590 | |
| 591 | /* API: Start stream. */ |
Sauw Ming | b93e688 | 2011-03-19 05:33:21 +0000 | [diff] [blame] | 592 | static pj_status_t ios_stream_start(pjmedia_vid_dev_stream *strm) |
Sauw Ming | 6e6c215 | 2010-12-14 13:03:10 +0000 | [diff] [blame] | 593 | { |
| 594 | struct ios_stream *stream = (struct ios_stream*)strm; |
| 595 | |
| 596 | PJ_UNUSED_ARG(stream); |
| 597 | |
| 598 | PJ_LOG(4, (THIS_FILE, "Starting qt video stream")); |
| 599 | |
| 600 | if (stream->cap_session) { |
| 601 | [stream->cap_session startRunning]; |
| 602 | |
| 603 | if (![stream->cap_session isRunning]) |
| 604 | return PJ_EUNKNOWN; |
| 605 | } |
| 606 | |
| 607 | return PJ_SUCCESS; |
| 608 | } |
| 609 | |
| 610 | |
| 611 | /* API: Put frame from stream */ |
Sauw Ming | b93e688 | 2011-03-19 05:33:21 +0000 | [diff] [blame] | 612 | static pj_status_t ios_stream_put_frame(pjmedia_vid_dev_stream *strm, |
Sauw Ming | 6e6c215 | 2010-12-14 13:03:10 +0000 | [diff] [blame] | 613 | const pjmedia_frame *frame) |
| 614 | { |
| 615 | struct ios_stream *stream = (struct ios_stream*)strm; |
| 616 | NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
| 617 | |
| 618 | pj_assert(stream->frame_size >= frame->size); |
| 619 | pj_memcpy(stream->buf, frame->buf, frame->size); |
| 620 | /* Perform video display in a background thread */ |
| 621 | // [stream->vout_delegate update_image]; |
| 622 | [NSThread detachNewThreadSelector:@selector(update_image) |
| 623 | toTarget:stream->vout_delegate withObject:nil]; |
| 624 | |
| 625 | [pool release]; |
| 626 | |
| 627 | return PJ_SUCCESS; |
| 628 | } |
| 629 | |
| 630 | /* API: Stop stream. */ |
Sauw Ming | b93e688 | 2011-03-19 05:33:21 +0000 | [diff] [blame] | 631 | static pj_status_t ios_stream_stop(pjmedia_vid_dev_stream *strm) |
Sauw Ming | 6e6c215 | 2010-12-14 13:03:10 +0000 | [diff] [blame] | 632 | { |
| 633 | struct ios_stream *stream = (struct ios_stream*)strm; |
| 634 | |
| 635 | PJ_UNUSED_ARG(stream); |
| 636 | |
| 637 | PJ_LOG(4, (THIS_FILE, "Stopping qt video stream")); |
| 638 | |
| 639 | if (stream->cap_session && [stream->cap_session isRunning]) |
| 640 | [stream->cap_session stopRunning]; |
| 641 | |
| 642 | return PJ_SUCCESS; |
| 643 | } |
| 644 | |
| 645 | |
| 646 | /* API: Destroy stream. */ |
Sauw Ming | b93e688 | 2011-03-19 05:33:21 +0000 | [diff] [blame] | 647 | static pj_status_t ios_stream_destroy(pjmedia_vid_dev_stream *strm) |
Sauw Ming | 6e6c215 | 2010-12-14 13:03:10 +0000 | [diff] [blame] | 648 | { |
| 649 | struct ios_stream *stream = (struct ios_stream*)strm; |
| 650 | |
| 651 | PJ_ASSERT_RETURN(stream != NULL, PJ_EINVAL); |
| 652 | |
| 653 | ios_stream_stop(strm); |
| 654 | |
| 655 | if (stream->imgView) { |
| 656 | [stream->imgView removeFromSuperview]; |
| 657 | [stream->imgView release]; |
| 658 | stream->imgView = NULL; |
| 659 | } |
| 660 | |
| 661 | if (stream->cap_session) { |
| 662 | [stream->cap_session release]; |
| 663 | stream->cap_session = NULL; |
| 664 | } |
| 665 | /* if (stream->dev_input) { |
| 666 | [stream->dev_input release]; |
| 667 | stream->dev_input = NULL; |
| 668 | } |
| 669 | */ |
| 670 | if (stream->vout_delegate) { |
| 671 | [stream->vout_delegate release]; |
| 672 | stream->vout_delegate = NULL; |
| 673 | } |
| 674 | /* if (stream->video_output) { |
| 675 | [stream->video_output release]; |
| 676 | stream->video_output = NULL; |
| 677 | } |
| 678 | */ |
| 679 | |
| 680 | pj_pool_release(stream->pool); |
| 681 | |
| 682 | return PJ_SUCCESS; |
| 683 | } |
| 684 | |
| 685 | #endif |
| 686 | #endif /* PJMEDIA_VIDEO_DEV_HAS_IOS */ |