| 1 |
/* |
| 2 |
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com) |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation; either version 2 of the License, or |
| 7 |
* (at your option) any later version. |
| 8 |
* |
| 9 |
* This program is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU General Public License |
| 15 |
* along with this program; if not, write to the Free Software |
| 16 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 |
*/ |
| 18 |
#include "util.h" |
| 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 defined(PJMEDIA_HAS_VIDEO) && PJMEDIA_HAS_VIDEO != 0 && \ |
| 25 |
defined(PJMEDIA_VIDEO_DEV_HAS_DARWIN) && PJMEDIA_VIDEO_DEV_HAS_DARWIN != 0 |
| 26 |
|
| 27 |
#include "TargetConditionals.h" |
| 28 |
|
| 29 |
#if TARGET_OS_IPHONE |
| 30 |
/* On iOS, we supports rendering using UIView */ |
| 31 |
#import <UIKit/UIKit.h> |
| 32 |
#endif |
| 33 |
|
| 34 |
#import <AVFoundation/AVFoundation.h> |
| 35 |
#import <QuartzCore/QuartzCore.h> |
| 36 |
|
| 37 |
#define THIS_FILE "darwin_dev.m" |
| 38 |
#define DEFAULT_CLOCK_RATE 90000 |
| 39 |
#define DEFAULT_WIDTH 640 |
| 40 |
#define DEFAULT_HEIGHT 480 |
| 41 |
#define DEFAULT_FPS 15 |
| 42 |
/* When the camera orientation changes, maintain horizon-level capture |
| 43 |
* relative to gravity. |
| 44 |
*/ |
| 45 |
#define USE_HORIZON_LEVEL 1 |
| 46 |
|
| 47 |
/* Define whether we should maintain the aspect ratio when rotating the image. |
| 48 |
* For more details, please refer to util.h. |
| 49 |
*/ |
| 50 |
#define MAINTAIN_ASPECT_RATIO PJ_TRUE |
| 51 |
|
| 52 |
typedef struct darwin_fmt_info |
| 53 |
{ |
| 54 |
pjmedia_format_id pjmedia_format; |
| 55 |
UInt32 darwin_format; |
| 56 |
} darwin_fmt_info; |
| 57 |
|
| 58 |
static darwin_fmt_info darwin_fmts[] = |
| 59 |
{ |
| 60 |
#if !TARGET_OS_IPHONE |
| 61 |
{ PJMEDIA_FORMAT_YUY2, kCVPixelFormatType_422YpCbCr8_yuvs }, |
| 62 |
{ PJMEDIA_FORMAT_UYVY, kCVPixelFormatType_422YpCbCr8 }, |
| 63 |
#endif |
| 64 |
{ PJMEDIA_FORMAT_BGRA, kCVPixelFormatType_32BGRA }, |
| 65 |
{ PJMEDIA_FORMAT_I420, kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange } |
| 66 |
}; |
| 67 |
|
| 68 |
typedef struct darwin_supported_size |
| 69 |
{ |
| 70 |
pj_size_t supported_size_w; |
| 71 |
pj_size_t supported_size_h; |
| 72 |
NSString *preset_str; |
| 73 |
} darwin_supported_size; |
| 74 |
|
| 75 |
/* Set the preset_str on set_preset_str method. */ |
| 76 |
static darwin_supported_size darwin_sizes[] = |
| 77 |
{ |
| 78 |
#if !TARGET_OS_IPHONE |
| 79 |
{ 320, 240, NULL }, |
| 80 |
#endif |
| 81 |
{ 352, 288, NULL }, |
| 82 |
{ 640, 480, NULL }, |
| 83 |
#if !TARGET_OS_IPHONE |
| 84 |
{ 960, 540, NULL }, |
| 85 |
#endif |
| 86 |
{ 1280, 720, NULL } |
| 87 |
#if TARGET_OS_IPHONE |
| 88 |
,{ 1920, 1080, NULL } |
| 89 |
#endif |
| 90 |
}; |
| 91 |
|
| 92 |
/* darwin device info */ |
| 93 |
struct darwin_dev_info |
| 94 |
{ |
| 95 |
pjmedia_vid_dev_info info; |
| 96 |
AVCaptureDevice *dev; |
| 97 |
}; |
| 98 |
|
| 99 |
/* darwin factory */ |
| 100 |
struct darwin_factory |
| 101 |
{ |
| 102 |
pjmedia_vid_dev_factory base; |
| 103 |
pj_pool_t *pool; |
| 104 |
pj_pool_factory *pf; |
| 105 |
|
| 106 |
unsigned dev_count; |
| 107 |
struct darwin_dev_info *dev_info; |
| 108 |
}; |
| 109 |
|
| 110 |
@interface VOutDelegate: NSObject |
| 111 |
<AVCaptureVideoDataOutputSampleBufferDelegate> |
| 112 |
{ |
| 113 |
@public |
| 114 |
struct darwin_stream *stream; |
| 115 |
} |
| 116 |
@end |
| 117 |
|
| 118 |
/* Video stream. */ |
| 119 |
struct darwin_stream |
| 120 |
{ |
| 121 |
pjmedia_vid_dev_stream base; /**< Base stream */ |
| 122 |
pjmedia_vid_dev_param param; /**< Settings */ |
| 123 |
pj_pool_t *pool; /**< Memory pool */ |
| 124 |
struct darwin_factory *factory; /**< Factory */ |
| 125 |
|
| 126 |
pjmedia_vid_dev_cb vid_cb; /**< Stream callback */ |
| 127 |
void *user_data; /**< Application data */ |
| 128 |
|
| 129 |
pjmedia_rect_size size; |
| 130 |
unsigned bytes_per_row; |
| 131 |
unsigned frame_size; /**< Frame size (bytes)*/ |
| 132 |
pj_bool_t is_planar; |
| 133 |
NSLock *frame_lock; |
| 134 |
void *capture_buf; |
| 135 |
void *frame_buf; |
| 136 |
|
| 137 |
pjmedia_vid_dev_conv conv; |
| 138 |
pjmedia_rect_size vid_size; |
| 139 |
|
| 140 |
AVCaptureSession *cap_session; |
| 141 |
AVCaptureDeviceInput *dev_input; |
| 142 |
pj_bool_t has_image; |
| 143 |
AVCaptureVideoDataOutput *video_output; |
| 144 |
VOutDelegate *vout_delegate; |
| 145 |
dispatch_queue_t queue; |
| 146 |
|
| 147 |
pj_bool_t is_running; |
| 148 |
#if TARGET_OS_IPHONE |
| 149 |
pj_bool_t is_rendering; |
| 150 |
void *render_buf; |
| 151 |
pj_size_t render_buf_size; |
| 152 |
CGDataProviderRef render_data_provider; |
| 153 |
UIView *render_view; |
| 154 |
AVCaptureVideoPreviewLayer *prev_layer; |
| 155 |
UIView *prev_view; |
| 156 |
#endif |
| 157 |
pj_timestamp frame_ts; |
| 158 |
unsigned ts_inc; |
| 159 |
}; |
| 160 |
|
| 161 |
|
| 162 |
/* Prototypes */ |
| 163 |
static pj_status_t darwin_factory_init(pjmedia_vid_dev_factory *f); |
| 164 |
static pj_status_t darwin_factory_destroy(pjmedia_vid_dev_factory *f); |
| 165 |
static pj_status_t darwin_factory_refresh(pjmedia_vid_dev_factory *f); |
| 166 |
static unsigned darwin_factory_get_dev_count(pjmedia_vid_dev_factory *f); |
| 167 |
static pj_status_t darwin_factory_get_dev_info(pjmedia_vid_dev_factory *f, |
| 168 |
unsigned index, |
| 169 |
pjmedia_vid_dev_info *info); |
| 170 |
static pj_status_t darwin_factory_default_param(pj_pool_t *pool, |
| 171 |
pjmedia_vid_dev_factory *f, |
| 172 |
unsigned index, |
| 173 |
pjmedia_vid_dev_param *param); |
| 174 |
static pj_status_t darwin_factory_create_stream( |
| 175 |
pjmedia_vid_dev_factory *f, |
| 176 |
pjmedia_vid_dev_param *param, |
| 177 |
const pjmedia_vid_dev_cb *cb, |
| 178 |
void *user_data, |
| 179 |
pjmedia_vid_dev_stream **p_vid_strm); |
| 180 |
|
| 181 |
static pj_status_t darwin_stream_get_param(pjmedia_vid_dev_stream *strm, |
| 182 |
pjmedia_vid_dev_param *param); |
| 183 |
static pj_status_t darwin_stream_get_cap(pjmedia_vid_dev_stream *strm, |
| 184 |
pjmedia_vid_dev_cap cap, |
| 185 |
void *value); |
| 186 |
static pj_status_t darwin_stream_set_cap(pjmedia_vid_dev_stream *strm, |
| 187 |
pjmedia_vid_dev_cap cap, |
| 188 |
const void *value); |
| 189 |
static pj_status_t darwin_stream_start(pjmedia_vid_dev_stream *strm); |
| 190 |
static pj_status_t darwin_stream_get_frame(pjmedia_vid_dev_stream *strm, |
| 191 |
pjmedia_frame *frame); |
| 192 |
static pj_status_t darwin_stream_put_frame(pjmedia_vid_dev_stream *strm, |
| 193 |
const pjmedia_frame *frame); |
| 194 |
static pj_status_t darwin_stream_stop(pjmedia_vid_dev_stream *strm); |
| 195 |
static pj_status_t darwin_stream_destroy(pjmedia_vid_dev_stream *strm); |
| 196 |
|
| 197 |
/* Operations */ |
| 198 |
static pjmedia_vid_dev_factory_op factory_op = |
| 199 |
{ |
| 200 |
&darwin_factory_init, |
| 201 |
&darwin_factory_destroy, |
| 202 |
&darwin_factory_get_dev_count, |
| 203 |
&darwin_factory_get_dev_info, |
| 204 |
&darwin_factory_default_param, |
| 205 |
&darwin_factory_create_stream, |
| 206 |
&darwin_factory_refresh |
| 207 |
}; |
| 208 |
|
| 209 |
static pjmedia_vid_dev_stream_op stream_op = |
| 210 |
{ |
| 211 |
&darwin_stream_get_param, |
| 212 |
&darwin_stream_get_cap, |
| 213 |
&darwin_stream_set_cap, |
| 214 |
&darwin_stream_start, |
| 215 |
&darwin_stream_get_frame, |
| 216 |
&darwin_stream_put_frame, |
| 217 |
&darwin_stream_stop, |
| 218 |
&darwin_stream_destroy |
| 219 |
}; |
| 220 |
|
| 221 |
static void set_preset_str() |
| 222 |
{ |
| 223 |
int idx = 0; |
| 224 |
#if !TARGET_OS_IPHONE |
| 225 |
darwin_sizes[idx++].preset_str = AVCaptureSessionPreset320x240; |
| 226 |
#endif |
| 227 |
darwin_sizes[idx++].preset_str = AVCaptureSessionPreset352x288; |
| 228 |
darwin_sizes[idx++].preset_str = AVCaptureSessionPreset640x480; |
| 229 |
#if !TARGET_OS_IPHONE |
| 230 |
darwin_sizes[idx++].preset_str = AVCaptureSessionPreset960x540; |
| 231 |
#endif |
| 232 |
darwin_sizes[idx++].preset_str = AVCaptureSessionPreset1280x720; |
| 233 |
#if TARGET_OS_IPHONE |
| 234 |
darwin_sizes[idx++].preset_str = AVCaptureSessionPreset1920x1080; |
| 235 |
#endif |
| 236 |
} |
| 237 |
|
| 238 |
#if TARGET_OS_IPHONE |
| 239 |
static void dispatch_sync_on_main_queue(void (^block)(void)) |
| 240 |
{ |
| 241 |
if ([NSThread isMainThread]) { |
| 242 |
block(); |
| 243 |
} else { |
| 244 |
dispatch_sync(dispatch_get_main_queue(), block); |
| 245 |
} |
| 246 |
} |
| 247 |
#endif |
| 248 |
|
| 249 |
/**************************************************************************** |
| 250 |
* Factory operations |
| 251 |
*/ |
| 252 |
/* |
| 253 |
* Init darwin_ video driver. |
| 254 |
*/ |
| 255 |
pjmedia_vid_dev_factory* pjmedia_darwin_factory(pj_pool_factory *pf) |
| 256 |
{ |
| 257 |
struct darwin_factory *f; |
| 258 |
pj_pool_t *pool; |
| 259 |
|
| 260 |
pool = pj_pool_create(pf, "darwin video", 8000, 4000, NULL); |
| 261 |
f = PJ_POOL_ZALLOC_T(pool, struct darwin_factory); |
| 262 |
f->pf = pf; |
| 263 |
f->pool = pool; |
| 264 |
f->base.op = &factory_op; |
| 265 |
|
| 266 |
return &f->base; |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
/* API: init factory */ |
| 271 |
static pj_status_t darwin_factory_init(pjmedia_vid_dev_factory *f) |
| 272 |
{ |
| 273 |
return darwin_factory_refresh(f); |
| 274 |
} |
| 275 |
|
| 276 |
/* API: destroy factory */ |
| 277 |
static pj_status_t darwin_factory_destroy(pjmedia_vid_dev_factory *f) |
| 278 |
{ |
| 279 |
struct darwin_factory *qf = (struct darwin_factory*)f; |
| 280 |
pj_pool_t *pool = qf->pool; |
| 281 |
|
| 282 |
qf->pool = NULL; |
| 283 |
pj_pool_release(pool); |
| 284 |
|
| 285 |
return PJ_SUCCESS; |
| 286 |
} |
| 287 |
|
| 288 |
/* API: refresh the list of devices */ |
| 289 |
static pj_status_t darwin_factory_refresh(pjmedia_vid_dev_factory *f) |
| 290 |
{ |
| 291 |
struct darwin_factory *qf = (struct darwin_factory*)f; |
| 292 |
struct darwin_dev_info *qdi; |
| 293 |
unsigned i, l, first_idx, front_idx = -1; |
| 294 |
enum { MAX_DEV_COUNT = 8 }; |
| 295 |
|
| 296 |
set_preset_str(); |
| 297 |
|
| 298 |
/* Initialize input and output devices here */ |
| 299 |
qf->dev_info = (struct darwin_dev_info*) |
| 300 |
pj_pool_calloc(qf->pool, MAX_DEV_COUNT, |
| 301 |
sizeof(struct darwin_dev_info)); |
| 302 |
qf->dev_count = 0; |
| 303 |
|
| 304 |
#if TARGET_OS_IPHONE |
| 305 |
/* Init output device */ |
| 306 |
qdi = &qf->dev_info[qf->dev_count++]; |
| 307 |
pj_bzero(qdi, sizeof(*qdi)); |
| 308 |
pj_ansi_strxcpy(qdi->info.name, "UIView", sizeof(qdi->info.name)); |
| 309 |
pj_ansi_strxcpy(qdi->info.driver, "iOS", sizeof(qdi->info.driver)); |
| 310 |
qdi->info.dir = PJMEDIA_DIR_RENDER; |
| 311 |
qdi->info.has_callback = PJ_FALSE; |
| 312 |
#endif |
| 313 |
|
| 314 |
/* Init input device */ |
| 315 |
first_idx = qf->dev_count; |
| 316 |
if (NSClassFromString(@"AVCaptureSession")) { |
| 317 |
NSArray *dev_list = NULL; |
| 318 |
|
| 319 |
if (@available(macOS 10.15, iOS 10.0, *)) { |
| 320 |
/* Starting in iOS 10 and macOS 10.15, [AVCaptureDevice devices] |
| 321 |
* is deprecated and replaced by AVCaptureDeviceDiscoverySession. |
| 322 |
*/ |
| 323 |
AVCaptureDeviceDiscoverySession *dds; |
| 324 |
NSMutableArray<AVCaptureDeviceType> *dev_types = |
| 325 |
[NSMutableArray arrayWithCapacity:5]; |
| 326 |
|
| 327 |
[dev_types addObject:AVCaptureDeviceTypeBuiltInWideAngleCamera]; |
| 328 |
#if TARGET_OS_IPHONE && defined(__IPHONE_10_0) |
| 329 |
// Deprecated in iOS 10.2 |
| 330 |
// AVCaptureDeviceTypeBuiltInDuoCamera |
| 331 |
[dev_types addObject:AVCaptureDeviceTypeBuiltInTelephotoCamera]; |
| 332 |
#endif |
| 333 |
|
| 334 |
#if (TARGET_OS_IPHONE && defined(__IPHONE_17_0)) || \ |
| 335 |
(TARGET_OS_OSX && defined(__MAC_14_0)) |
| 336 |
if (@available(macOS 14.0, iOS 17.0, *)) { |
| 337 |
[dev_types addObject:AVCaptureDeviceTypeExternal]; |
| 338 |
} else { |
| 339 |
# if (TARGET_OS_OSX && __MAC_OS_X_VERSION_MIN_REQUIRED < 140000) |
| 340 |
[dev_types addObject:AVCaptureDeviceTypeExternalUnknown]; |
| 341 |
# endif |
| 342 |
} |
| 343 |
|
| 344 |
#elif TARGET_OS_OSX |
| 345 |
[dev_types addObject:AVCaptureDeviceTypeExternalUnknown]; |
| 346 |
#endif |
| 347 |
|
| 348 |
dds = [AVCaptureDeviceDiscoverySession |
| 349 |
discoverySessionWithDeviceTypes:dev_types |
| 350 |
mediaType:AVMediaTypeVideo |
| 351 |
position:AVCaptureDevicePositionUnspecified]; |
| 352 |
|
| 353 |
dev_list = [dds devices]; |
| 354 |
} else { |
| 355 |
#if (TARGET_OS_OSX && __MAC_OS_X_VERSION_MIN_REQUIRED < __MAC_10_15) || \ |
| 356 |
(TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_10_0) |
| 357 |
dev_list = [AVCaptureDevice devices]; |
| 358 |
#endif |
| 359 |
} |
| 360 |
|
| 361 |
for (AVCaptureDevice *device in dev_list) { |
| 362 |
if (![device hasMediaType:AVMediaTypeVideo] || |
| 363 |
qf->dev_count >= MAX_DEV_COUNT) |
| 364 |
{ |
| 365 |
continue; |
| 366 |
} |
| 367 |
|
| 368 |
if (front_idx == -1 && |
| 369 |
[device position] == AVCaptureDevicePositionFront) |
| 370 |
{ |
| 371 |
front_idx = qf->dev_count; |
| 372 |
} |
| 373 |
|
| 374 |
qdi = &qf->dev_info[qf->dev_count++]; |
| 375 |
pj_bzero(qdi, sizeof(*qdi)); |
| 376 |
pj_ansi_strxcpy(qdi->info.name, |
| 377 |
[device.localizedName UTF8String], |
| 378 |
sizeof(qdi->info.name)); |
| 379 |
pj_ansi_strxcpy(qdi->info.driver, "AVF", |
| 380 |
sizeof(qdi->info.driver)); |
| 381 |
qdi->info.dir = PJMEDIA_DIR_CAPTURE; |
| 382 |
qdi->info.has_callback = PJ_FALSE; |
| 383 |
#if TARGET_OS_IPHONE |
| 384 |
qdi->info.caps = PJMEDIA_VID_DEV_CAP_INPUT_PREVIEW | |
| 385 |
PJMEDIA_VID_DEV_CAP_SWITCH; |
| 386 |
#endif |
| 387 |
qdi->dev = device; |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
/* Set front camera to be the first input device (as default dev) */ |
| 392 |
if (front_idx != -1 && front_idx != first_idx) { |
| 393 |
struct darwin_dev_info tmp_dev_info = qf->dev_info[first_idx]; |
| 394 |
qf->dev_info[first_idx] = qf->dev_info[front_idx]; |
| 395 |
qf->dev_info[front_idx] = tmp_dev_info; |
| 396 |
} |
| 397 |
|
| 398 |
/* Set supported formats */ |
| 399 |
for (i = 0; i < qf->dev_count; i++) { |
| 400 |
qdi = &qf->dev_info[i]; |
| 401 |
qdi->info.caps |= PJMEDIA_VID_DEV_CAP_FORMAT | |
| 402 |
PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW | |
| 403 |
PJMEDIA_VID_DEV_CAP_OUTPUT_RESIZE | |
| 404 |
PJMEDIA_VID_DEV_CAP_OUTPUT_POSITION | |
| 405 |
PJMEDIA_VID_DEV_CAP_OUTPUT_HIDE | |
| 406 |
PJMEDIA_VID_DEV_CAP_ORIENTATION; |
| 407 |
|
| 408 |
for (l = 0; l < PJ_ARRAY_SIZE(darwin_fmts); l++) { |
| 409 |
pjmedia_format *fmt; |
| 410 |
|
| 411 |
/* Simple renderer UIView only supports BGRA */ |
| 412 |
if (qdi->info.dir == PJMEDIA_DIR_RENDER && |
| 413 |
darwin_fmts[l].pjmedia_format != PJMEDIA_FORMAT_BGRA) |
| 414 |
{ |
| 415 |
continue; |
| 416 |
} |
| 417 |
|
| 418 |
if (qdi->info.dir == PJMEDIA_DIR_RENDER) { |
| 419 |
fmt = &qdi->info.fmt[qdi->info.fmt_cnt++]; |
| 420 |
pjmedia_format_init_video(fmt, |
| 421 |
darwin_fmts[l].pjmedia_format, |
| 422 |
DEFAULT_WIDTH, |
| 423 |
DEFAULT_HEIGHT, |
| 424 |
DEFAULT_FPS, 1); |
| 425 |
} else { |
| 426 |
int m; |
| 427 |
AVCaptureDevice *dev = qdi->dev; |
| 428 |
|
| 429 |
/* Set supported size for capture device */ |
| 430 |
for(m = 0; |
| 431 |
m < PJ_ARRAY_SIZE(darwin_sizes) && |
| 432 |
qdi->info.fmt_cnt<PJMEDIA_VID_DEV_INFO_FMT_CNT; |
| 433 |
m++) |
| 434 |
{ |
| 435 |
if ([dev supportsAVCaptureSessionPreset: |
| 436 |
darwin_sizes[m].preset_str]) |
| 437 |
{ |
| 438 |
/* Landscape video */ |
| 439 |
fmt = &qdi->info.fmt[qdi->info.fmt_cnt++]; |
| 440 |
pjmedia_format_init_video(fmt, |
| 441 |
darwin_fmts[l].pjmedia_format, |
| 442 |
darwin_sizes[m].supported_size_w, |
| 443 |
darwin_sizes[m].supported_size_h, |
| 444 |
DEFAULT_FPS, 1); |
| 445 |
/* Portrait video */ |
| 446 |
fmt = &qdi->info.fmt[qdi->info.fmt_cnt++]; |
| 447 |
pjmedia_format_init_video(fmt, |
| 448 |
darwin_fmts[l].pjmedia_format, |
| 449 |
darwin_sizes[m].supported_size_h, |
| 450 |
darwin_sizes[m].supported_size_w, |
| 451 |
DEFAULT_FPS, 1); |
| 452 |
} |
| 453 |
} |
| 454 |
} |
| 455 |
} |
| 456 |
} |
| 457 |
|
| 458 |
PJ_LOG(4, (THIS_FILE, "Darwin video initialized with %d devices:", |
| 459 |
qf->dev_count)); |
| 460 |
for (i = 0; i < qf->dev_count; i++) { |
| 461 |
qdi = &qf->dev_info[i]; |
| 462 |
PJ_LOG(4, (THIS_FILE, "%2d: [%s] %s - %s", i, |
| 463 |
(qdi->info.dir==PJMEDIA_DIR_CAPTURE? "Capturer":"Renderer"), |
| 464 |
qdi->info.driver, qdi->info.name)); |
| 465 |
} |
| 466 |
|
| 467 |
return PJ_SUCCESS; |
| 468 |
} |
| 469 |
|
| 470 |
/* API: get number of devices */ |
| 471 |
static unsigned darwin_factory_get_dev_count(pjmedia_vid_dev_factory *f) |
| 472 |
{ |
| 473 |
struct darwin_factory *qf = (struct darwin_factory*)f; |
| 474 |
return qf->dev_count; |
| 475 |
} |
| 476 |
|
| 477 |
/* API: get device info */ |
| 478 |
static pj_status_t darwin_factory_get_dev_info(pjmedia_vid_dev_factory *f, |
| 479 |
unsigned index, |
| 480 |
pjmedia_vid_dev_info *info) |
| 481 |
{ |
| 482 |
struct darwin_factory *qf = (struct darwin_factory*)f; |
| 483 |
|
| 484 |
PJ_ASSERT_RETURN(index < qf->dev_count, PJMEDIA_EVID_INVDEV); |
| 485 |
|
| 486 |
pj_memcpy(info, &qf->dev_info[index].info, sizeof(*info)); |
| 487 |
|
| 488 |
return PJ_SUCCESS; |
| 489 |
} |
| 490 |
|
| 491 |
/* API: create default device parameter */ |
| 492 |
static pj_status_t darwin_factory_default_param(pj_pool_t *pool, |
| 493 |
pjmedia_vid_dev_factory *f, |
| 494 |
unsigned index, |
| 495 |
pjmedia_vid_dev_param *param) |
| 496 |
{ |
| 497 |
struct darwin_factory *qf = (struct darwin_factory*)f; |
| 498 |
struct darwin_dev_info *di; |
| 499 |
|
| 500 |
PJ_ASSERT_RETURN(index < qf->dev_count, PJMEDIA_EVID_INVDEV); |
| 501 |
PJ_UNUSED_ARG(pool); |
| 502 |
|
| 503 |
di = &qf->dev_info[index]; |
| 504 |
|
| 505 |
pj_bzero(param, sizeof(*param)); |
| 506 |
if (di->info.dir & PJMEDIA_DIR_CAPTURE) { |
| 507 |
param->dir = PJMEDIA_DIR_CAPTURE; |
| 508 |
param->cap_id = index; |
| 509 |
param->rend_id = PJMEDIA_VID_INVALID_DEV; |
| 510 |
} else if (di->info.dir & PJMEDIA_DIR_RENDER) { |
| 511 |
param->dir = PJMEDIA_DIR_RENDER; |
| 512 |
param->rend_id = index; |
| 513 |
param->cap_id = PJMEDIA_VID_INVALID_DEV; |
| 514 |
} else { |
| 515 |
return PJMEDIA_EVID_INVDEV; |
| 516 |
} |
| 517 |
|
| 518 |
param->flags = PJMEDIA_VID_DEV_CAP_FORMAT; |
| 519 |
param->clock_rate = DEFAULT_CLOCK_RATE; |
| 520 |
pj_memcpy(¶m->fmt, &di->info.fmt[0], sizeof(param->fmt)); |
| 521 |
|
| 522 |
return PJ_SUCCESS; |
| 523 |
} |
| 524 |
|
| 525 |
@implementation VOutDelegate |
| 526 |
#if TARGET_OS_IPHONE |
| 527 |
- (void)update_image |
| 528 |
{ |
| 529 |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
| 530 |
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); |
| 531 |
|
| 532 |
CGImageRef cgIm = CGImageCreate(stream->size.w, stream->size.h, |
| 533 |
8, 32, stream->bytes_per_row, colorSpace, |
| 534 |
kCGImageAlphaFirst | |
| 535 |
kCGBitmapByteOrder32Little, |
| 536 |
stream->render_data_provider, 0, |
| 537 |
false, kCGRenderingIntentDefault); |
| 538 |
CGColorSpaceRelease(colorSpace); |
| 539 |
|
| 540 |
stream->render_view.layer.contents = (__bridge id)(cgIm); |
| 541 |
CGImageRelease(cgIm); |
| 542 |
|
| 543 |
[pool release]; |
| 544 |
stream->is_rendering = PJ_FALSE; |
| 545 |
} |
| 546 |
|
| 547 |
- (void)finish_render |
| 548 |
{ |
| 549 |
/* Do nothing. This function is serialized in the main thread, so when |
| 550 |
* it is called, we can be sure that update_image() has completed. |
| 551 |
*/ |
| 552 |
} |
| 553 |
|
| 554 |
#endif |
| 555 |
|
| 556 |
- (void)session_runtime_error:(NSNotification *)notification |
| 557 |
{ |
| 558 |
// This function is called from NSNotificationCenter. |
| 559 |
// Make sure the thread is registered. |
| 560 |
if (!pj_thread_is_registered()) { |
| 561 |
pj_thread_t *thread; |
| 562 |
static pj_thread_desc thread_desc; |
| 563 |
pj_bzero(thread_desc, sizeof(pj_thread_desc)); |
| 564 |
pj_thread_register("NSNotificationCenter", thread_desc, &thread); |
| 565 |
} |
| 566 |
|
| 567 |
NSError *error = notification.userInfo[AVCaptureSessionErrorKey]; |
| 568 |
PJ_LOG(3, (THIS_FILE, "Capture session runtime error: %s, %s", |
| 569 |
[error.localizedDescription UTF8String], |
| 570 |
[error.localizedFailureReason UTF8String])); |
| 571 |
} |
| 572 |
|
| 573 |
- (void)captureOutput:(AVCaptureOutput *)captureOutput |
| 574 |
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer |
| 575 |
fromConnection:(AVCaptureConnection *)connection |
| 576 |
{ |
| 577 |
CVImageBufferRef img; |
| 578 |
pj_status_t status; |
| 579 |
void *frame_buf; |
| 580 |
|
| 581 |
/* Refrain from calling pjlib functions which require thread registration |
| 582 |
* here, since according to the doc, dispatch queue cannot guarantee |
| 583 |
* the reliability of underlying functions required by PJSIP to perform |
| 584 |
* the registration, such as pthread_self() and pthread_getspecific()/ |
| 585 |
* pthread_setspecific(). |
| 586 |
*/ |
| 587 |
|
| 588 |
if (!sampleBuffer) |
| 589 |
return; |
| 590 |
|
| 591 |
/* Get a CMSampleBuffer's Core Video image buffer for the media data */ |
| 592 |
img = CMSampleBufferGetImageBuffer(sampleBuffer); |
| 593 |
|
| 594 |
/* Lock the base address of the pixel buffer */ |
| 595 |
CVPixelBufferLockBaseAddress(img, kCVPixelBufferLock_ReadOnly); |
| 596 |
|
| 597 |
[stream->frame_lock lock]; |
| 598 |
stream->has_image = PJ_TRUE; |
| 599 |
|
| 600 |
if (stream->is_planar && stream->capture_buf) { |
| 601 |
if (stream->param.fmt.id == PJMEDIA_FORMAT_I420) { |
| 602 |
/* kCVPixelFormatType_420YpCbCr8BiPlanar* is NV12 */ |
| 603 |
pj_uint8_t *p, *p_end, *Y, *U, *V; |
| 604 |
pj_size_t p_len; |
| 605 |
pj_size_t wxh = stream->vid_size.w * stream->vid_size.h; |
| 606 |
/* Image stride is not always equal to the image width. |
| 607 |
* For example, resolution 352*288 can have a stride of 384. |
| 608 |
*/ |
| 609 |
pj_size_t stride = CVPixelBufferGetBytesPerRowOfPlane(img, 0); |
| 610 |
/* Image height is not always equal to the video resolution. |
| 611 |
* For example, resolution 352*288 can have a height of 264. |
| 612 |
*/ |
| 613 |
pj_size_t height = CVPixelBufferGetHeight(img); |
| 614 |
pj_bool_t need_clip; |
| 615 |
|
| 616 |
/* Auto detect rotation */ |
| 617 |
if ((stream->vid_size.w > stream->vid_size.h && stride < height) || |
| 618 |
(stream->vid_size.h > stream->vid_size.w && stride > height)) |
| 619 |
{ |
| 620 |
pj_size_t w = stream->vid_size.w; |
| 621 |
stream->vid_size.w = stream->vid_size.h; |
| 622 |
stream->vid_size.h = w; |
| 623 |
} |
| 624 |
|
| 625 |
need_clip = (stride != stream->vid_size.w); |
| 626 |
|
| 627 |
p = (pj_uint8_t*)CVPixelBufferGetBaseAddressOfPlane(img, 0); |
| 628 |
|
| 629 |
p_len = stream->vid_size.w * height; |
| 630 |
Y = (pj_uint8_t*)stream->capture_buf; |
| 631 |
U = Y + wxh; |
| 632 |
V = U + wxh/4; |
| 633 |
|
| 634 |
if (!need_clip) { |
| 635 |
pj_memcpy(Y, p, p_len); |
| 636 |
Y += p_len; |
| 637 |
} else { |
| 638 |
int i = 0; |
| 639 |
for (; i < height; ++i) { |
| 640 |
pj_memcpy(Y, p, stream->vid_size.w); |
| 641 |
Y += stream->vid_size.w; |
| 642 |
p += stride; |
| 643 |
} |
| 644 |
} |
| 645 |
|
| 646 |
if (stream->vid_size.h > height) { |
| 647 |
pj_memset(Y, 16, (stream->vid_size.h - height) * |
| 648 |
stream->vid_size.w); |
| 649 |
} |
| 650 |
|
| 651 |
p = (pj_uint8_t*)CVPixelBufferGetBaseAddressOfPlane(img, 1); |
| 652 |
if (!need_clip) { |
| 653 |
p_len >>= 1; |
| 654 |
p_end = p + p_len; |
| 655 |
|
| 656 |
while (p < p_end) { |
| 657 |
*U++ = *p++; |
| 658 |
*V++ = *p++; |
| 659 |
} |
| 660 |
} else { |
| 661 |
int i = 0; |
| 662 |
for (;i<height/2;++i) { |
| 663 |
int y=0; |
| 664 |
for (;y<(stream->vid_size.w)/2;++y) { |
| 665 |
*U++ = *p++; |
| 666 |
*V++ = *p++; |
| 667 |
} |
| 668 |
p += (stride - stream->vid_size.w); |
| 669 |
} |
| 670 |
} |
| 671 |
|
| 672 |
if (stream->vid_size.h > height) { |
| 673 |
pj_size_t UV_size = (stream->vid_size.h - height) * |
| 674 |
stream->vid_size.w / 4; |
| 675 |
pj_memset(U, 0x80, UV_size); |
| 676 |
pj_memset(V, 0x80, UV_size); |
| 677 |
} |
| 678 |
} |
| 679 |
} else { |
| 680 |
pj_memcpy(stream->capture_buf, CVPixelBufferGetBaseAddress(img), |
| 681 |
stream->frame_size); |
| 682 |
} |
| 683 |
|
| 684 |
status = pjmedia_vid_dev_conv_resize_and_rotate(&stream->conv, |
| 685 |
stream->capture_buf, |
| 686 |
&frame_buf); |
| 687 |
if (status == PJ_SUCCESS) { |
| 688 |
stream->frame_buf = frame_buf; |
| 689 |
} |
| 690 |
|
| 691 |
stream->frame_ts.u64 += stream->ts_inc; |
| 692 |
[stream->frame_lock unlock]; |
| 693 |
|
| 694 |
/* Unlock the pixel buffer */ |
| 695 |
CVPixelBufferUnlockBaseAddress(img, kCVPixelBufferLock_ReadOnly); |
| 696 |
} |
| 697 |
@end |
| 698 |
|
| 699 |
static pj_status_t darwin_stream_get_frame(pjmedia_vid_dev_stream *strm, |
| 700 |
pjmedia_frame *frame) |
| 701 |
{ |
| 702 |
struct darwin_stream *stream = (struct darwin_stream *)strm; |
| 703 |
|
| 704 |
if (!stream->has_image) { |
| 705 |
frame->size = 0; |
| 706 |
frame->type = PJMEDIA_FRAME_TYPE_NONE; |
| 707 |
frame->timestamp.u64 = stream->frame_ts.u64; |
| 708 |
return PJMEDIA_EVID_NOTREADY; |
| 709 |
} |
| 710 |
|
| 711 |
frame->type = PJMEDIA_FRAME_TYPE_VIDEO; |
| 712 |
frame->bit_info = 0; |
| 713 |
pj_assert(frame->size >= stream->frame_size); |
| 714 |
frame->size = stream->frame_size; |
| 715 |
frame->timestamp.u64 = stream->frame_ts.u64; |
| 716 |
|
| 717 |
[stream->frame_lock lock]; |
| 718 |
pj_memcpy(frame->buf, stream->frame_buf, stream->frame_size); |
| 719 |
[stream->frame_lock unlock]; |
| 720 |
|
| 721 |
return PJ_SUCCESS; |
| 722 |
} |
| 723 |
|
| 724 |
|
| 725 |
static darwin_fmt_info* get_darwin_format_info(pjmedia_format_id id) |
| 726 |
{ |
| 727 |
unsigned i; |
| 728 |
|
| 729 |
for (i = 0; i < PJ_ARRAY_SIZE(darwin_fmts); i++) { |
| 730 |
if (darwin_fmts[i].pjmedia_format == id) |
| 731 |
return &darwin_fmts[i]; |
| 732 |
} |
| 733 |
|
| 734 |
return NULL; |
| 735 |
} |
| 736 |
|
| 737 |
|
| 738 |
#if TARGET_OS_IPHONE |
| 739 |
static pj_status_t darwin_init_view(struct darwin_stream *strm) |
| 740 |
{ |
| 741 |
pjmedia_vid_dev_param *param = &strm->param; |
| 742 |
CGRect view_rect = CGRectMake(0, 0, param->fmt.det.vid.size.w, |
| 743 |
param->fmt.det.vid.size.h); |
| 744 |
|
| 745 |
if (param->flags & PJMEDIA_VID_DEV_CAP_OUTPUT_RESIZE) { |
| 746 |
view_rect.size.width = param->disp_size.w; |
| 747 |
view_rect.size.height = param->disp_size.h; |
| 748 |
} |
| 749 |
|
| 750 |
if (param->flags & PJMEDIA_VID_DEV_CAP_OUTPUT_POSITION) { |
| 751 |
view_rect.origin.x = param->window_pos.x; |
| 752 |
view_rect.origin.y = param->window_pos.y; |
| 753 |
} |
| 754 |
|
| 755 |
strm->render_view = [[UIView alloc] initWithFrame:view_rect]; |
| 756 |
strm->param.window.info.ios.window = strm->render_view; |
| 757 |
|
| 758 |
if (param->flags & PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW) { |
| 759 |
PJ_ASSERT_RETURN(param->window.info.ios.window, PJ_EINVAL); |
| 760 |
darwin_stream_set_cap(&strm->base, PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW, |
| 761 |
param->window.info.ios.window); |
| 762 |
} |
| 763 |
if (param->flags & PJMEDIA_VID_DEV_CAP_OUTPUT_HIDE) { |
| 764 |
darwin_stream_set_cap(&strm->base, PJMEDIA_VID_DEV_CAP_OUTPUT_HIDE, |
| 765 |
¶m->window_hide); |
| 766 |
} |
| 767 |
if (param->flags & PJMEDIA_VID_DEV_CAP_ORIENTATION) { |
| 768 |
darwin_stream_set_cap(&strm->base, PJMEDIA_VID_DEV_CAP_ORIENTATION, |
| 769 |
¶m->orient); |
| 770 |
} |
| 771 |
|
| 772 |
return PJ_SUCCESS; |
| 773 |
} |
| 774 |
#endif /* TARGET_OS_IPHONE */ |
| 775 |
|
| 776 |
|
| 777 |
/* API: create stream */ |
| 778 |
static pj_status_t darwin_factory_create_stream( |
| 779 |
pjmedia_vid_dev_factory *f, |
| 780 |
pjmedia_vid_dev_param *param, |
| 781 |
const pjmedia_vid_dev_cb *cb, |
| 782 |
void *user_data, |
| 783 |
pjmedia_vid_dev_stream **p_vid_strm) |
| 784 |
{ |
| 785 |
struct darwin_factory *qf = (struct darwin_factory*)f; |
| 786 |
pj_pool_t *pool; |
| 787 |
struct darwin_stream *strm; |
| 788 |
pjmedia_video_format_detail *vfd; |
| 789 |
const pjmedia_video_format_info *vfi; |
| 790 |
pj_status_t status = PJ_SUCCESS; |
| 791 |
darwin_fmt_info *ifi = get_darwin_format_info(param->fmt.id); |
| 792 |
|
| 793 |
PJ_ASSERT_RETURN(f && param && p_vid_strm, PJ_EINVAL); |
| 794 |
PJ_ASSERT_RETURN(param->fmt.type == PJMEDIA_TYPE_VIDEO && |
| 795 |
param->fmt.detail_type == PJMEDIA_FORMAT_DETAIL_VIDEO && |
| 796 |
(param->dir == PJMEDIA_DIR_CAPTURE || |
| 797 |
param->dir == PJMEDIA_DIR_RENDER), |
| 798 |
PJ_EINVAL); |
| 799 |
|
| 800 |
if (!(ifi = get_darwin_format_info(param->fmt.id))) |
| 801 |
return PJMEDIA_EVID_BADFORMAT; |
| 802 |
|
| 803 |
vfi = pjmedia_get_video_format_info(NULL, param->fmt.id); |
| 804 |
if (!vfi) |
| 805 |
return PJMEDIA_EVID_BADFORMAT; |
| 806 |
|
| 807 |
/* Create and Initialize stream descriptor */ |
| 808 |
pool = pj_pool_create(qf->pf, "darwin-dev", 4000, 4000, NULL); |
| 809 |
PJ_ASSERT_RETURN(pool != NULL, PJ_ENOMEM); |
| 810 |
|
| 811 |
strm = PJ_POOL_ZALLOC_T(pool, struct darwin_stream); |
| 812 |
pj_memcpy(&strm->param, param, sizeof(*param)); |
| 813 |
strm->pool = pool; |
| 814 |
pj_memcpy(&strm->vid_cb, cb, sizeof(*cb)); |
| 815 |
strm->user_data = user_data; |
| 816 |
strm->factory = qf; |
| 817 |
|
| 818 |
vfd = pjmedia_format_get_video_format_detail(&strm->param.fmt, PJ_TRUE); |
| 819 |
pj_memcpy(&strm->size, &vfd->size, sizeof(vfd->size)); |
| 820 |
strm->bytes_per_row = strm->size.w * vfi->bpp / 8; |
| 821 |
strm->frame_size = strm->bytes_per_row * strm->size.h; |
| 822 |
strm->ts_inc = PJMEDIA_SPF2(param->clock_rate, &vfd->fps, 1); |
| 823 |
strm->is_planar = vfi->plane_cnt > 1; |
| 824 |
|
| 825 |
if (param->dir & PJMEDIA_DIR_CAPTURE) { |
| 826 |
int i; |
| 827 |
|
| 828 |
/* Create capture stream here */ |
| 829 |
strm->cap_session = [[AVCaptureSession alloc] init]; |
| 830 |
if (!strm->cap_session) { |
| 831 |
PJ_LOG(2, (THIS_FILE, "Unable to create AV capture session")); |
| 832 |
status = PJ_ENOMEM; |
| 833 |
goto on_error; |
| 834 |
} |
| 835 |
AVCaptureDevice *dev = qf->dev_info[param->cap_id].dev; |
| 836 |
|
| 837 |
for (i = PJ_ARRAY_SIZE(darwin_sizes)-1; i > 0; --i) { |
| 838 |
if (((vfd->size.w == darwin_sizes[i].supported_size_w) && |
| 839 |
(vfd->size.h == darwin_sizes[i].supported_size_h)) || |
| 840 |
((vfd->size.w == darwin_sizes[i].supported_size_h) && |
| 841 |
(vfd->size.h == darwin_sizes[i].supported_size_w))) |
| 842 |
{ |
| 843 |
break; |
| 844 |
} |
| 845 |
} |
| 846 |
strm->cap_session.sessionPreset = darwin_sizes[i].preset_str; |
| 847 |
|
| 848 |
/* If the requested size is portrait (or landscape), we make |
| 849 |
* our natural orientation portrait (or landscape) as well. |
| 850 |
*/ |
| 851 |
if (vfd->size.w > vfd->size.h) { |
| 852 |
vfd->size.w = darwin_sizes[i].supported_size_w; |
| 853 |
vfd->size.h = darwin_sizes[i].supported_size_h; |
| 854 |
} else { |
| 855 |
vfd->size.h = darwin_sizes[i].supported_size_w; |
| 856 |
vfd->size.w = darwin_sizes[i].supported_size_h; |
| 857 |
} |
| 858 |
strm->size = vfd->size; |
| 859 |
strm->vid_size = vfd->size; |
| 860 |
strm->bytes_per_row = strm->size.w * vfi->bpp / 8; |
| 861 |
strm->frame_size = strm->bytes_per_row * strm->size.h; |
| 862 |
|
| 863 |
/* Update param as output */ |
| 864 |
param->fmt = strm->param.fmt; |
| 865 |
|
| 866 |
#if TARGET_OS_IPHONE |
| 867 |
/* Set frame rate, this may only work on iOS 7 or later. |
| 868 |
* On Mac, this may raise an exception if we set it to a value |
| 869 |
* unsupported by the device. We need to query |
| 870 |
* activeFormat.videoSupportedFrameRateRanges to get the valid |
| 871 |
* range. |
| 872 |
*/ |
| 873 |
if ([dev respondsToSelector:@selector(activeVideoMinFrameDuration)] && |
| 874 |
[dev lockForConfiguration:NULL]) |
| 875 |
{ |
| 876 |
dev.activeVideoMinFrameDuration = CMTimeMake(vfd->fps.denum, |
| 877 |
vfd->fps.num); |
| 878 |
dev.activeVideoMaxFrameDuration = CMTimeMake(vfd->fps.denum, |
| 879 |
vfd->fps.num); |
| 880 |
[dev unlockForConfiguration]; |
| 881 |
} |
| 882 |
#endif |
| 883 |
|
| 884 |
/* Add the video device to the session as a device input */ |
| 885 |
NSError *error; |
| 886 |
strm->dev_input = [AVCaptureDeviceInput |
| 887 |
deviceInputWithDevice:dev |
| 888 |
error: &error]; |
| 889 |
if (error || !strm->dev_input) { |
| 890 |
PJ_LOG(2, (THIS_FILE, "Unable to get input capture device")); |
| 891 |
status = PJMEDIA_EVID_SYSERR; |
| 892 |
goto on_error; |
| 893 |
} |
| 894 |
|
| 895 |
if ([strm->cap_session canAddInput:strm->dev_input]) { |
| 896 |
[strm->cap_session addInput:strm->dev_input]; |
| 897 |
} else { |
| 898 |
PJ_LOG(2, (THIS_FILE, "Unable to add input capture device")); |
| 899 |
status = PJMEDIA_EVID_SYSERR; |
| 900 |
goto on_error; |
| 901 |
} |
| 902 |
|
| 903 |
strm->video_output = [[AVCaptureVideoDataOutput alloc] init]; |
| 904 |
if (!strm->video_output) { |
| 905 |
PJ_LOG(2, (THIS_FILE, "Unable to create AV video output")); |
| 906 |
status = PJ_ENOMEM; |
| 907 |
goto on_error; |
| 908 |
} |
| 909 |
|
| 910 |
/* Configure the video output */ |
| 911 |
strm->video_output.alwaysDiscardsLateVideoFrames = YES; |
| 912 |
strm->video_output.videoSettings = |
| 913 |
[NSDictionary dictionaryWithObjectsAndKeys: |
| 914 |
#if !TARGET_OS_IPHONE |
| 915 |
/* On Mac, we need to set the buffer's dimension |
| 916 |
* to avoid extra padding. |
| 917 |
*/ |
| 918 |
[NSNumber numberWithDouble: |
| 919 |
darwin_sizes[i].supported_size_w], |
| 920 |
(id)kCVPixelBufferWidthKey, |
| 921 |
[NSNumber numberWithDouble: |
| 922 |
darwin_sizes[i].supported_size_h], |
| 923 |
(id)kCVPixelBufferHeightKey, |
| 924 |
#endif |
| 925 |
[NSNumber numberWithInt:ifi->darwin_format], |
| 926 |
kCVPixelBufferPixelFormatTypeKey, nil]; |
| 927 |
|
| 928 |
strm->vout_delegate = [VOutDelegate alloc]; |
| 929 |
strm->vout_delegate->stream = strm; |
| 930 |
strm->queue = dispatch_queue_create("vout_queue", |
| 931 |
DISPATCH_QUEUE_SERIAL); |
| 932 |
[strm->video_output setSampleBufferDelegate:strm->vout_delegate |
| 933 |
queue:strm->queue]; |
| 934 |
|
| 935 |
/* Add observer to catch notification when the capture session |
| 936 |
* fails to start running or encounters an error during runtime. |
| 937 |
*/ |
| 938 |
[[NSNotificationCenter defaultCenter] addObserver:strm->vout_delegate |
| 939 |
selector:@selector(session_runtime_error:) |
| 940 |
name:AVCaptureSessionRuntimeErrorNotification |
| 941 |
object:strm->cap_session]; |
| 942 |
|
| 943 |
if ([strm->cap_session canAddOutput:strm->video_output]) { |
| 944 |
[strm->cap_session addOutput:strm->video_output]; |
| 945 |
} else { |
| 946 |
PJ_LOG(2, (THIS_FILE, "Unable to add video data output")); |
| 947 |
status = PJMEDIA_EVID_SYSERR; |
| 948 |
goto on_error; |
| 949 |
} |
| 950 |
|
| 951 |
strm->capture_buf = pj_pool_alloc(strm->pool, strm->frame_size); |
| 952 |
strm->frame_buf = strm->capture_buf; |
| 953 |
strm->frame_lock = [[NSLock alloc]init]; |
| 954 |
|
| 955 |
/* Native preview */ |
| 956 |
if (param->flags & PJMEDIA_VID_DEV_CAP_INPUT_PREVIEW) { |
| 957 |
darwin_stream_set_cap(&strm->base, PJMEDIA_VID_DEV_CAP_INPUT_PREVIEW, |
| 958 |
¶m->native_preview); |
| 959 |
} |
| 960 |
|
| 961 |
/* Video orientation. |
| 962 |
* If we send in portrait, we need to set up orientation converter |
| 963 |
* as well. |
| 964 |
*/ |
| 965 |
if ((param->flags & PJMEDIA_VID_DEV_CAP_ORIENTATION) || |
| 966 |
(vfd->size.h > vfd->size.w)) |
| 967 |
{ |
| 968 |
if (param->orient == PJMEDIA_ORIENT_UNKNOWN) { |
| 969 |
#if (USE_HORIZON_LEVEL) |
| 970 |
/* Handle sending portrait. */ |
| 971 |
if (vfd->size.h > vfd->size.w) |
| 972 |
param->orient = PJMEDIA_ORIENT_ROTATE_90DEG; |
| 973 |
else |
| 974 |
#endif |
| 975 |
param->orient = PJMEDIA_ORIENT_NATURAL; |
| 976 |
} |
| 977 |
darwin_stream_set_cap(&strm->base, PJMEDIA_VID_DEV_CAP_ORIENTATION, |
| 978 |
¶m->orient); |
| 979 |
} |
| 980 |
|
| 981 |
} else if (param->dir & PJMEDIA_DIR_RENDER) { |
| 982 |
#if TARGET_OS_IPHONE |
| 983 |
/* Create renderer stream here */ |
| 984 |
|
| 985 |
dispatch_sync_on_main_queue(^{ |
| 986 |
darwin_init_view(strm); |
| 987 |
}); |
| 988 |
|
| 989 |
if (!strm->vout_delegate) { |
| 990 |
strm->vout_delegate = [VOutDelegate alloc]; |
| 991 |
strm->vout_delegate->stream = strm; |
| 992 |
} |
| 993 |
|
| 994 |
strm->render_buf = pj_pool_alloc(pool, strm->frame_size); |
| 995 |
strm->render_buf_size = strm->frame_size; |
| 996 |
strm->render_data_provider = CGDataProviderCreateWithData(NULL, |
| 997 |
strm->render_buf, strm->frame_size, |
| 998 |
NULL); |
| 999 |
#endif |
| 1000 |
} |
| 1001 |
|
| 1002 |
/* Done */ |
| 1003 |
strm->base.op = &stream_op; |
| 1004 |
*p_vid_strm = &strm->base; |
| 1005 |
|
| 1006 |
return PJ_SUCCESS; |
| 1007 |
|
| 1008 |
on_error: |
| 1009 |
darwin_stream_destroy((pjmedia_vid_dev_stream *)strm); |
| 1010 |
|
| 1011 |
return status; |
| 1012 |
} |
| 1013 |
|
| 1014 |
/* API: Get stream info. */ |
| 1015 |
static pj_status_t darwin_stream_get_param(pjmedia_vid_dev_stream *s, |
| 1016 |
pjmedia_vid_dev_param *pi) |
| 1017 |
{ |
| 1018 |
struct darwin_stream *strm = (struct darwin_stream*)s; |
| 1019 |
|
| 1020 |
PJ_ASSERT_RETURN(strm && pi, PJ_EINVAL); |
| 1021 |
|
| 1022 |
pj_memcpy(pi, &strm->param, sizeof(*pi)); |
| 1023 |
|
| 1024 |
return PJ_SUCCESS; |
| 1025 |
} |
| 1026 |
|
| 1027 |
/* API: get capability */ |
| 1028 |
static pj_status_t darwin_stream_get_cap(pjmedia_vid_dev_stream *s, |
| 1029 |
pjmedia_vid_dev_cap cap, |
| 1030 |
void *pval) |
| 1031 |
{ |
| 1032 |
struct darwin_stream *strm = (struct darwin_stream*)s; |
| 1033 |
|
| 1034 |
PJ_ASSERT_RETURN(s && pval, PJ_EINVAL); |
| 1035 |
PJ_UNUSED_ARG(strm); |
| 1036 |
|
| 1037 |
switch (cap) { |
| 1038 |
#if TARGET_OS_IPHONE |
| 1039 |
case PJMEDIA_VID_DEV_CAP_INPUT_PREVIEW: |
| 1040 |
{ |
| 1041 |
pjmedia_vid_dev_hwnd *hwnd = (pjmedia_vid_dev_hwnd *) pval; |
| 1042 |
hwnd->type = PJMEDIA_VID_DEV_HWND_TYPE_IOS; |
| 1043 |
hwnd->info.ios.window = (void *)strm->prev_view; |
| 1044 |
return PJ_SUCCESS; |
| 1045 |
} |
| 1046 |
case PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW: |
| 1047 |
{ |
| 1048 |
pjmedia_vid_dev_hwnd *hwnd = (pjmedia_vid_dev_hwnd *) pval; |
| 1049 |
hwnd->type = PJMEDIA_VID_DEV_HWND_TYPE_IOS; |
| 1050 |
hwnd->info.ios.window = (void *)strm->render_view; |
| 1051 |
return PJ_SUCCESS; |
| 1052 |
} |
| 1053 |
#endif /* TARGET_OS_IPHONE */ |
| 1054 |
default: |
| 1055 |
break; |
| 1056 |
} |
| 1057 |
|
| 1058 |
return PJMEDIA_EVID_INVCAP; |
| 1059 |
} |
| 1060 |
|
| 1061 |
static pj_bool_t set_orientation(struct darwin_stream *strm) |
| 1062 |
{ |
| 1063 |
#if (TARGET_OS_OSX && __MAC_OS_X_VERSION_MIN_REQUIRED < 140000) || \ |
| 1064 |
(TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MIN_REQUIRED < 170000) |
| 1065 |
|
| 1066 |
const AVCaptureVideoOrientation cap_ori[4] = |
| 1067 |
{ |
| 1068 |
AVCaptureVideoOrientationLandscapeLeft, /* NATURAL */ |
| 1069 |
AVCaptureVideoOrientationPortrait, /* 90DEG */ |
| 1070 |
AVCaptureVideoOrientationLandscapeRight, /* 180DEG */ |
| 1071 |
AVCaptureVideoOrientationPortraitUpsideDown, /* 270DEG */ |
| 1072 |
}; |
| 1073 |
AVCaptureConnection *vidcon; |
| 1074 |
|
| 1075 |
vidcon = [strm->video_output |
| 1076 |
connectionWithMediaType:AVMediaTypeVideo]; |
| 1077 |
if ([vidcon isVideoOrientationSupported]) { |
| 1078 |
vidcon.videoOrientation = cap_ori[strm->param.orient-1]; |
| 1079 |
return PJ_TRUE; |
| 1080 |
} |
| 1081 |
|
| 1082 |
#endif |
| 1083 |
|
| 1084 |
return PJ_FALSE; |
| 1085 |
} |
| 1086 |
|
| 1087 |
/* API: set capability */ |
| 1088 |
static pj_status_t darwin_stream_set_cap(pjmedia_vid_dev_stream *s, |
| 1089 |
pjmedia_vid_dev_cap cap, |
| 1090 |
const void *pval) |
| 1091 |
{ |
| 1092 |
struct darwin_stream *strm = (struct darwin_stream*)s; |
| 1093 |
|
| 1094 |
PJ_ASSERT_RETURN(s && pval, PJ_EINVAL); |
| 1095 |
|
| 1096 |
switch (cap) { |
| 1097 |
#if TARGET_OS_IPHONE |
| 1098 |
/* Native preview */ |
| 1099 |
case PJMEDIA_VID_DEV_CAP_INPUT_PREVIEW: |
| 1100 |
{ |
| 1101 |
pj_bool_t native_preview = *((pj_bool_t *)pval); |
| 1102 |
|
| 1103 |
/* Disable native preview */ |
| 1104 |
if (!native_preview) { |
| 1105 |
if (strm->prev_layer) { |
| 1106 |
CALayer *prev_layer = strm->prev_layer; |
| 1107 |
dispatch_sync_on_main_queue(^{ |
| 1108 |
if ([prev_layer superlayer]) |
| 1109 |
[prev_layer removeFromSuperlayer]; |
| 1110 |
[prev_layer release]; |
| 1111 |
}); |
| 1112 |
strm->prev_layer = nil; |
| 1113 |
PJ_LOG(4, (THIS_FILE, "Native preview deinitialized")); |
| 1114 |
} |
| 1115 |
return PJ_SUCCESS; |
| 1116 |
} |
| 1117 |
|
| 1118 |
/* Enable native preview */ |
| 1119 |
|
| 1120 |
/* Verify if it is already enabled */ |
| 1121 |
if (strm->prev_layer) |
| 1122 |
return PJ_SUCCESS; |
| 1123 |
|
| 1124 |
/* Verify capture session instance availability */ |
| 1125 |
if (!strm->cap_session) |
| 1126 |
return PJ_EINVALIDOP; |
| 1127 |
|
| 1128 |
/* Preview layer instantiation should be in main thread! */ |
| 1129 |
dispatch_sync_on_main_queue(^{ |
| 1130 |
/* Create preview layer */ |
| 1131 |
strm->prev_layer = [[AVCaptureVideoPreviewLayer alloc] |
| 1132 |
initWithSession:strm->cap_session]; |
| 1133 |
strm->prev_layer.videoGravity = |
| 1134 |
AVLayerVideoGravityResizeAspectFill; |
| 1135 |
|
| 1136 |
/* Create preview view and init it at smaller size than |
| 1137 |
* the actual video. |
| 1138 |
*/ |
| 1139 |
if (!strm->prev_view) { |
| 1140 |
CGRect rect; |
| 1141 |
rect = CGRectMake(0, 0, strm->param.fmt.det.vid.size.w/2, |
| 1142 |
strm->param.fmt.det.vid.size.h/2); |
| 1143 |
strm->prev_view = [[UIView alloc] initWithFrame:rect]; |
| 1144 |
} |
| 1145 |
|
| 1146 |
/* Add the preview layer as sublayer */ |
| 1147 |
strm->prev_layer.frame = strm->prev_view.bounds; |
| 1148 |
[strm->prev_view.layer addSublayer:strm->prev_layer]; |
| 1149 |
|
| 1150 |
}); |
| 1151 |
PJ_LOG(4, (THIS_FILE, "Native preview initialized")); |
| 1152 |
|
| 1153 |
return PJ_SUCCESS; |
| 1154 |
} |
| 1155 |
#endif |
| 1156 |
|
| 1157 |
/* Fast switch */ |
| 1158 |
case PJMEDIA_VID_DEV_CAP_SWITCH: |
| 1159 |
{ |
| 1160 |
if (!strm->cap_session) return PJ_EINVAL; |
| 1161 |
|
| 1162 |
NSError *error; |
| 1163 |
struct darwin_dev_info* di = strm->factory->dev_info; |
| 1164 |
pjmedia_vid_dev_switch_param *p = |
| 1165 |
(pjmedia_vid_dev_switch_param*)pval; |
| 1166 |
|
| 1167 |
/* Verify target capture ID */ |
| 1168 |
if (p->target_id < 0 || p->target_id >= strm->factory->dev_count) |
| 1169 |
return PJ_EINVAL; |
| 1170 |
|
| 1171 |
if (di[p->target_id].info.dir != PJMEDIA_DIR_CAPTURE || |
| 1172 |
!di[p->target_id].dev) |
| 1173 |
{ |
| 1174 |
return PJ_EINVAL; |
| 1175 |
} |
| 1176 |
|
| 1177 |
/* Just return if current and target device are the same */ |
| 1178 |
if (strm->param.cap_id == p->target_id) |
| 1179 |
return PJ_SUCCESS; |
| 1180 |
|
| 1181 |
/* Ok, let's do the switch */ |
| 1182 |
AVCaptureDeviceInput *cur_dev_input = strm->dev_input; |
| 1183 |
AVCaptureDeviceInput *new_dev_input = |
| 1184 |
[AVCaptureDeviceInput |
| 1185 |
deviceInputWithDevice:di[p->target_id].dev |
| 1186 |
error:&error]; |
| 1187 |
|
| 1188 |
[strm->cap_session beginConfiguration]; |
| 1189 |
[strm->cap_session removeInput:cur_dev_input]; |
| 1190 |
[strm->cap_session addInput:new_dev_input]; |
| 1191 |
[strm->cap_session commitConfiguration]; |
| 1192 |
|
| 1193 |
strm->dev_input = new_dev_input; |
| 1194 |
strm->param.cap_id = p->target_id; |
| 1195 |
|
| 1196 |
/* Set the orientation as well */ |
| 1197 |
darwin_stream_set_cap(s, PJMEDIA_VID_DEV_CAP_ORIENTATION, |
| 1198 |
&strm->param.orient); |
| 1199 |
|
| 1200 |
return PJ_SUCCESS; |
| 1201 |
} |
| 1202 |
|
| 1203 |
#if TARGET_OS_IPHONE |
| 1204 |
case PJMEDIA_VID_DEV_CAP_FORMAT: |
| 1205 |
{ |
| 1206 |
const pjmedia_video_format_info *vfi; |
| 1207 |
pjmedia_video_format_detail *vfd; |
| 1208 |
pjmedia_format *fmt = (pjmedia_format *)pval; |
| 1209 |
darwin_fmt_info *ifi; |
| 1210 |
|
| 1211 |
if (!(ifi = get_darwin_format_info(fmt->id))) |
| 1212 |
return PJMEDIA_EVID_BADFORMAT; |
| 1213 |
|
| 1214 |
vfi = pjmedia_get_video_format_info( |
| 1215 |
pjmedia_video_format_mgr_instance(), |
| 1216 |
fmt->id); |
| 1217 |
if (!vfi) |
| 1218 |
return PJMEDIA_EVID_BADFORMAT; |
| 1219 |
|
| 1220 |
pjmedia_format_copy(&strm->param.fmt, fmt); |
| 1221 |
|
| 1222 |
vfd = pjmedia_format_get_video_format_detail(fmt, PJ_TRUE); |
| 1223 |
pj_memcpy(&strm->size, &vfd->size, sizeof(vfd->size)); |
| 1224 |
strm->bytes_per_row = strm->size.w * vfi->bpp / 8; |
| 1225 |
strm->frame_size = strm->bytes_per_row * strm->size.h; |
| 1226 |
if (strm->render_buf_size < strm->frame_size) { |
| 1227 |
/* Realloc only when needed */ |
| 1228 |
strm->render_buf = pj_pool_alloc(strm->pool, strm->frame_size); |
| 1229 |
strm->render_buf_size = strm->frame_size; |
| 1230 |
CGDataProviderRelease(strm->render_data_provider); |
| 1231 |
strm->render_data_provider = CGDataProviderCreateWithData(NULL, |
| 1232 |
strm->render_buf, |
| 1233 |
strm->frame_size, |
| 1234 |
NULL); |
| 1235 |
} |
| 1236 |
|
| 1237 |
return PJ_SUCCESS; |
| 1238 |
} |
| 1239 |
|
| 1240 |
case PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW: |
| 1241 |
{ |
| 1242 |
UIView *view = (UIView *)pval; |
| 1243 |
strm->param.window.info.ios.window = (void *)pval; |
| 1244 |
dispatch_sync_on_main_queue(^{ |
| 1245 |
[view addSubview:strm->render_view]; |
| 1246 |
}); |
| 1247 |
return PJ_SUCCESS; |
| 1248 |
} |
| 1249 |
|
| 1250 |
case PJMEDIA_VID_DEV_CAP_OUTPUT_RESIZE: |
| 1251 |
{ |
| 1252 |
pj_memcpy(&strm->param.disp_size, pval, |
| 1253 |
sizeof(strm->param.disp_size)); |
| 1254 |
dispatch_sync_on_main_queue(^{ |
| 1255 |
CGRect r = strm->render_view.bounds; |
| 1256 |
r.size = CGSizeMake(strm->param.disp_size.w, |
| 1257 |
strm->param.disp_size.h); |
| 1258 |
strm->render_view.bounds = r; |
| 1259 |
}); |
| 1260 |
return PJ_SUCCESS; |
| 1261 |
} |
| 1262 |
|
| 1263 |
case PJMEDIA_VID_DEV_CAP_OUTPUT_POSITION: |
| 1264 |
{ |
| 1265 |
pj_memcpy(&strm->param.window_pos, pval, |
| 1266 |
sizeof(strm->param.window_pos)); |
| 1267 |
dispatch_sync_on_main_queue(^{ |
| 1268 |
strm->render_view.center = |
| 1269 |
CGPointMake(strm->param.window_pos.x + |
| 1270 |
strm->param.disp_size.w/2.0, |
| 1271 |
strm->param.window_pos.y + |
| 1272 |
strm->param.disp_size.h/2.0); |
| 1273 |
}); |
| 1274 |
return PJ_SUCCESS; |
| 1275 |
} |
| 1276 |
|
| 1277 |
case PJMEDIA_VID_DEV_CAP_OUTPUT_HIDE: |
| 1278 |
{ |
| 1279 |
dispatch_sync_on_main_queue(^{ |
| 1280 |
strm->render_view.hidden = (BOOL)(*((pj_bool_t *)pval)); |
| 1281 |
}); |
| 1282 |
return PJ_SUCCESS; |
| 1283 |
} |
| 1284 |
#endif /* TARGET_OS_IPHONE */ |
| 1285 |
|
| 1286 |
case PJMEDIA_VID_DEV_CAP_ORIENTATION: |
| 1287 |
{ |
| 1288 |
pjmedia_orient orient = *(pjmedia_orient *)pval; |
| 1289 |
pj_bool_t support_ori = PJ_FALSE; |
| 1290 |
|
| 1291 |
pj_assert(orient >= PJMEDIA_ORIENT_UNKNOWN && |
| 1292 |
orient <= PJMEDIA_ORIENT_ROTATE_270DEG); |
| 1293 |
|
| 1294 |
if (orient == PJMEDIA_ORIENT_UNKNOWN) |
| 1295 |
return PJ_EINVAL; |
| 1296 |
|
| 1297 |
pj_memcpy(&strm->param.orient, pval, |
| 1298 |
sizeof(strm->param.orient)); |
| 1299 |
|
| 1300 |
if (strm->param.dir == PJMEDIA_DIR_RENDER) { |
| 1301 |
#if TARGET_OS_IPHONE |
| 1302 |
dispatch_sync_on_main_queue(^{ |
| 1303 |
strm->render_view.transform = |
| 1304 |
CGAffineTransformMakeRotation( |
| 1305 |
((int)strm->param.orient-1) * -M_PI_2); |
| 1306 |
}); |
| 1307 |
#endif |
| 1308 |
|
| 1309 |
return PJ_SUCCESS; |
| 1310 |
} |
| 1311 |
|
| 1312 |
pj_assert(strm->param.dir == PJMEDIA_DIR_CAPTURE); |
| 1313 |
|
| 1314 |
if (!strm->video_output) |
| 1315 |
return PJMEDIA_EVID_NOTREADY; |
| 1316 |
|
| 1317 |
#if (TARGET_OS_IPHONE && defined(__IPHONE_17_0)) || \ |
| 1318 |
(TARGET_OS_OSX && defined(__MAC_14_0)) |
| 1319 |
if (@available(macOS 14.0, iOS 17.0, *)) { |
| 1320 |
AVCaptureConnection *vidcon; |
| 1321 |
const CGFloat cap_ori[4] = { 0, 90, 180, 270}; |
| 1322 |
int rotation = cap_ori[strm->param.orient-1]; |
| 1323 |
|
| 1324 |
#if (USE_HORIZON_LEVEL) |
| 1325 |
AVCaptureDeviceRotationCoordinator *coord = |
| 1326 |
[[AVCaptureDeviceRotationCoordinator alloc] |
| 1327 |
initWithDevice:strm->dev_input.device |
| 1328 |
previewLayer:nil]; |
| 1329 |
if (coord) { |
| 1330 |
rotation = coord.videoRotationAngleForHorizonLevelCapture; |
| 1331 |
[coord release]; |
| 1332 |
} |
| 1333 |
#endif |
| 1334 |
vidcon = [strm->video_output |
| 1335 |
connectionWithMediaType:AVMediaTypeVideo]; |
| 1336 |
if ([vidcon isVideoRotationAngleSupported:rotation]) |
| 1337 |
{ |
| 1338 |
vidcon.videoRotationAngle = rotation; |
| 1339 |
support_ori = PJ_TRUE; |
| 1340 |
} |
| 1341 |
|
| 1342 |
} else { |
| 1343 |
support_ori = set_orientation(strm); |
| 1344 |
} |
| 1345 |
#else |
| 1346 |
support_ori = set_orientation(strm); |
| 1347 |
#endif |
| 1348 |
|
| 1349 |
if (!strm->conv.conv) { |
| 1350 |
pj_status_t status; |
| 1351 |
pjmedia_rect_size orig_size; |
| 1352 |
|
| 1353 |
/* Original native size of device is landscape */ |
| 1354 |
orig_size.w = (strm->size.w > strm->size.h? strm->size.w : |
| 1355 |
strm->size.h); |
| 1356 |
orig_size.h = (strm->size.w > strm->size.h? strm->size.h : |
| 1357 |
strm->size.w); |
| 1358 |
|
| 1359 |
if (!support_ori) { |
| 1360 |
PJ_LOG(4, (THIS_FILE, "Native video capture orientation " |
| 1361 |
"unsupported, will use converter's " |
| 1362 |
"rotation.")); |
| 1363 |
} |
| 1364 |
|
| 1365 |
status = pjmedia_vid_dev_conv_create_converter( |
| 1366 |
&strm->conv, strm->pool, |
| 1367 |
&strm->param.fmt, |
| 1368 |
orig_size, strm->size, |
| 1369 |
(support_ori?PJ_FALSE:PJ_TRUE), |
| 1370 |
MAINTAIN_ASPECT_RATIO); |
| 1371 |
|
| 1372 |
if (status != PJ_SUCCESS) |
| 1373 |
return status; |
| 1374 |
} |
| 1375 |
|
| 1376 |
pjmedia_vid_dev_conv_set_rotation(&strm->conv, strm->param.orient); |
| 1377 |
|
| 1378 |
PJ_LOG(5, (THIS_FILE, "Video capture orientation set to %d", |
| 1379 |
strm->param.orient)); |
| 1380 |
|
| 1381 |
return PJ_SUCCESS; |
| 1382 |
} |
| 1383 |
|
| 1384 |
default: |
| 1385 |
break; |
| 1386 |
} |
| 1387 |
|
| 1388 |
return PJMEDIA_EVID_INVCAP; |
| 1389 |
} |
| 1390 |
|
| 1391 |
/* API: Start stream. */ |
| 1392 |
static pj_status_t darwin_stream_start(pjmedia_vid_dev_stream *strm) |
| 1393 |
{ |
| 1394 |
struct darwin_stream *stream = (struct darwin_stream*)strm; |
| 1395 |
|
| 1396 |
PJ_LOG(4, (THIS_FILE, "Starting Darwin video stream")); |
| 1397 |
|
| 1398 |
stream->is_running = PJ_TRUE; |
| 1399 |
|
| 1400 |
if (stream->cap_session) { |
| 1401 |
[stream->cap_session |
| 1402 |
performSelectorOnMainThread:@selector(startRunning) |
| 1403 |
withObject:nil waitUntilDone:YES]; |
| 1404 |
|
| 1405 |
if (![stream->cap_session isRunning]) { |
| 1406 |
/* More info about the error should be reported in |
| 1407 |
* VOutDelegate::session_runtime_error() |
| 1408 |
*/ |
| 1409 |
PJ_LOG(3, (THIS_FILE, "Unable to start AVFoundation capture " |
| 1410 |
"session")); |
| 1411 |
return PJ_EUNKNOWN; |
| 1412 |
} |
| 1413 |
} |
| 1414 |
|
| 1415 |
return PJ_SUCCESS; |
| 1416 |
} |
| 1417 |
|
| 1418 |
|
| 1419 |
/* API: Put frame from stream */ |
| 1420 |
static pj_status_t darwin_stream_put_frame(pjmedia_vid_dev_stream *strm, |
| 1421 |
const pjmedia_frame *frame) |
| 1422 |
{ |
| 1423 |
#if TARGET_OS_IPHONE |
| 1424 |
struct darwin_stream *stream = (struct darwin_stream*)strm; |
| 1425 |
|
| 1426 |
/* Video conference just trying to send heart beat for updating timestamp |
| 1427 |
* or keep-alive, this port doesn't need any, just ignore. |
| 1428 |
*/ |
| 1429 |
if (frame->size==0 || frame->buf==NULL) |
| 1430 |
return PJ_SUCCESS; |
| 1431 |
|
| 1432 |
if (!stream->is_running) |
| 1433 |
return PJ_EINVALIDOP; |
| 1434 |
|
| 1435 |
/* Prevent more than one async rendering task. */ |
| 1436 |
if (stream->is_rendering) |
| 1437 |
return PJ_EIGNORED; |
| 1438 |
|
| 1439 |
if (stream->frame_size >= frame->size) |
| 1440 |
pj_memcpy(stream->render_buf, frame->buf, frame->size); |
| 1441 |
else |
| 1442 |
pj_memcpy(stream->render_buf, frame->buf, stream->frame_size); |
| 1443 |
|
| 1444 |
/* Perform video display in the main thread */ |
| 1445 |
stream->is_rendering = PJ_TRUE; |
| 1446 |
[stream->vout_delegate performSelectorOnMainThread:@selector(update_image) |
| 1447 |
withObject:nil waitUntilDone:NO]; |
| 1448 |
#endif |
| 1449 |
|
| 1450 |
return PJ_SUCCESS; |
| 1451 |
} |
| 1452 |
|
| 1453 |
/* API: Stop stream. */ |
| 1454 |
static pj_status_t darwin_stream_stop(pjmedia_vid_dev_stream *strm) |
| 1455 |
{ |
| 1456 |
struct darwin_stream *stream = (struct darwin_stream*)strm; |
| 1457 |
|
| 1458 |
if (!stream->cap_session || ![stream->cap_session isRunning]) |
| 1459 |
return PJ_SUCCESS; |
| 1460 |
|
| 1461 |
PJ_LOG(4, (THIS_FILE, "Stopping Darwin video stream")); |
| 1462 |
|
| 1463 |
[stream->cap_session performSelectorOnMainThread:@selector(stopRunning) |
| 1464 |
withObject:nil waitUntilDone:YES]; |
| 1465 |
stream->has_image = PJ_FALSE; |
| 1466 |
stream->is_running = PJ_FALSE; |
| 1467 |
|
| 1468 |
#if TARGET_OS_IPHONE |
| 1469 |
/* Wait until the rendering finishes */ |
| 1470 |
[stream->vout_delegate performSelectorOnMainThread:@selector(finish_render) |
| 1471 |
withObject:nil waitUntilDone:YES]; |
| 1472 |
#endif |
| 1473 |
|
| 1474 |
return PJ_SUCCESS; |
| 1475 |
} |
| 1476 |
|
| 1477 |
|
| 1478 |
/* API: Destroy stream. */ |
| 1479 |
static pj_status_t darwin_stream_destroy(pjmedia_vid_dev_stream *strm) |
| 1480 |
{ |
| 1481 |
struct darwin_stream *stream = (struct darwin_stream*)strm; |
| 1482 |
|
| 1483 |
PJ_ASSERT_RETURN(stream != NULL, PJ_EINVAL); |
| 1484 |
|
| 1485 |
darwin_stream_stop(strm); |
| 1486 |
|
| 1487 |
if (stream->cap_session) { |
| 1488 |
if (stream->dev_input) { |
| 1489 |
[stream->cap_session removeInput:stream->dev_input]; |
| 1490 |
stream->dev_input = nil; |
| 1491 |
} |
| 1492 |
[stream->cap_session removeOutput:stream->video_output]; |
| 1493 |
[stream->cap_session release]; |
| 1494 |
stream->cap_session = nil; |
| 1495 |
} |
| 1496 |
|
| 1497 |
if (stream->video_output) { |
| 1498 |
[stream->video_output release]; |
| 1499 |
stream->video_output = nil; |
| 1500 |
} |
| 1501 |
|
| 1502 |
if (stream->vout_delegate) { |
| 1503 |
[stream->vout_delegate release]; |
| 1504 |
stream->vout_delegate = nil; |
| 1505 |
} |
| 1506 |
|
| 1507 |
#if TARGET_OS_IPHONE |
| 1508 |
if (stream->prev_layer) { |
| 1509 |
[stream->prev_layer |
| 1510 |
performSelectorOnMainThread:@selector(removeFromSuperlayer) |
| 1511 |
withObject:nil waitUntilDone:YES]; |
| 1512 |
[stream->prev_layer release]; |
| 1513 |
stream->prev_layer = nil; |
| 1514 |
} |
| 1515 |
|
| 1516 |
if (stream->render_view) { |
| 1517 |
[stream->render_view |
| 1518 |
performSelectorOnMainThread:@selector(removeFromSuperview) |
| 1519 |
withObject:nil waitUntilDone:YES]; |
| 1520 |
|
| 1521 |
[stream->render_view release]; |
| 1522 |
stream->render_view = nil; |
| 1523 |
} |
| 1524 |
|
| 1525 |
if (stream->prev_view) { |
| 1526 |
[stream->prev_view |
| 1527 |
performSelectorOnMainThread:@selector(removeFromSuperview) |
| 1528 |
withObject:nil waitUntilDone:YES]; |
| 1529 |
|
| 1530 |
[stream->prev_view release]; |
| 1531 |
stream->prev_view = nil; |
| 1532 |
} |
| 1533 |
|
| 1534 |
if (stream->render_data_provider) { |
| 1535 |
CGDataProviderRelease(stream->render_data_provider); |
| 1536 |
stream->render_data_provider = nil; |
| 1537 |
} |
| 1538 |
#endif /* TARGET_OS_IPHONE */ |
| 1539 |
|
| 1540 |
if (stream->queue) { |
| 1541 |
dispatch_release(stream->queue); |
| 1542 |
stream->queue = nil; |
| 1543 |
} |
| 1544 |
|
| 1545 |
if (stream->frame_lock) { |
| 1546 |
[stream->frame_lock release]; |
| 1547 |
stream->frame_lock = nil; |
| 1548 |
} |
| 1549 |
|
| 1550 |
pjmedia_vid_dev_conv_destroy_converter(&stream->conv); |
| 1551 |
|
| 1552 |
pj_pool_release(stream->pool); |
| 1553 |
|
| 1554 |
return PJ_SUCCESS; |
| 1555 |
} |
| 1556 |
|
| 1557 |
#endif /* PJMEDIA_VIDEO_DEV_HAS_DARWIN */ |