| 1 | /* |
| 2 | * Copyright (C) 2013 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 <pj/ctype.h> |
| 19 | #include <pjsua2/media.hpp> |
| 20 | #include <pjsua2/types.hpp> |
| 21 | #include <pjsua2/endpoint.hpp> |
| 22 | #include "util.hpp" |
| 23 | |
| 24 | using namespace pj; |
| 25 | using namespace std; |
| 26 | |
| 27 | #include <pjsua-lib/pjsua_internal.h> |
| 28 | |
| 29 | #define THIS_FILE "media.cpp" |
| 30 | #define MAX_FILE_NAMES 64 |
| 31 | #define MAX_DEV_COUNT 64 |
| 32 | |
| 33 | /////////////////////////////////////////////////////////////////////////////// |
| 34 | void MediaFormatAudio::fromPj(const pjmedia_format &format) |
| 35 | { |
| 36 | if ((format.type != PJMEDIA_TYPE_AUDIO) && |
| 37 | (format.detail_type != PJMEDIA_FORMAT_DETAIL_AUDIO)) |
| 38 | { |
| 39 | type = PJMEDIA_TYPE_UNKNOWN; |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | id = format.id; |
| 44 | type = format.type; |
| 45 | |
| 46 | /* Detail. */ |
| 47 | clockRate = format.det.aud.clock_rate; |
| 48 | channelCount = format.det.aud.channel_count; |
| 49 | frameTimeUsec = format.det.aud.frame_time_usec; |
| 50 | bitsPerSample = format.det.aud.bits_per_sample; |
| 51 | avgBps = format.det.aud.avg_bps; |
| 52 | maxBps = format.det.aud.max_bps; |
| 53 | } |
| 54 | |
| 55 | pjmedia_format MediaFormatAudio::toPj() const |
| 56 | { |
| 57 | pjmedia_format pj_format; |
| 58 | |
| 59 | pj_format.id = id; |
| 60 | pj_format.type = type; |
| 61 | |
| 62 | pj_format.detail_type = PJMEDIA_FORMAT_DETAIL_AUDIO; |
| 63 | pj_format.det.aud.clock_rate = clockRate; |
| 64 | pj_format.det.aud.channel_count = channelCount; |
| 65 | pj_format.det.aud.frame_time_usec = frameTimeUsec; |
| 66 | pj_format.det.aud.bits_per_sample = bitsPerSample; |
| 67 | pj_format.det.aud.avg_bps = avgBps; |
| 68 | pj_format.det.aud.max_bps = maxBps; |
| 69 | |
| 70 | return pj_format; |
| 71 | } |
| 72 | |
| 73 | void MediaFormatAudio::init(pj_uint32_t formatId, |
| 74 | unsigned clockRate_, unsigned channelCount_, |
| 75 | int frameTimeUsec_, int bitsPerSample_, |
| 76 | pj_uint32_t avgBps_, pj_uint32_t maxBps_) |
| 77 | { |
| 78 | type = PJMEDIA_TYPE_AUDIO; |
| 79 | id = formatId; |
| 80 | |
| 81 | this->clockRate = clockRate_; |
| 82 | this->channelCount = channelCount_; |
| 83 | this->frameTimeUsec = frameTimeUsec_; |
| 84 | this->bitsPerSample = bitsPerSample_; |
| 85 | this->avgBps = avgBps_; |
| 86 | this->maxBps = maxBps_; |
| 87 | } |
| 88 | |
| 89 | /////////////////////////////////////////////////////////////////////////////// |
| 90 | /* Audio Media operations. */ |
| 91 | void ConfPortInfo::fromPj(const pjsua_conf_port_info &port_info) |
| 92 | { |
| 93 | portId = port_info.slot_id; |
| 94 | name = pj2Str(port_info.name); |
| 95 | format.fromPj(port_info.format); |
| 96 | txLevelAdj = port_info.rx_level_adj; |
| 97 | rxLevelAdj = port_info.tx_level_adj; |
| 98 | |
| 99 | /* |
| 100 | format.id = PJMEDIA_FORMAT_PCM; |
| 101 | format.type = PJMEDIA_TYPE_AUDIO; |
| 102 | format.clockRate = port_info.clock_rate; |
| 103 | format.channelCount = port_info.channel_count; |
| 104 | format.bitsPerSample = port_info.bits_per_sample; |
| 105 | format.frameTimeUsec = (port_info.samples_per_frame * |
| 106 | 1000000) / |
| 107 | (port_info.clock_rate * |
| 108 | port_info.channel_count); |
| 109 | |
| 110 | format.avgBps = format.maxBps = port_info.clock_rate * |
| 111 | port_info.channel_count * |
| 112 | port_info.bits_per_sample; |
| 113 | */ |
| 114 | listeners.clear(); |
| 115 | for (unsigned i=0; i<port_info.listener_cnt; ++i) { |
| 116 | listeners.push_back(port_info.listeners[i]); |
| 117 | } |
| 118 | } |
| 119 | /////////////////////////////////////////////////////////////////////////////// |
| 120 | Media::Media(pjmedia_type med_type) |
| 121 | : type(med_type) |
| 122 | { |
| 123 | |
| 124 | } |
| 125 | |
| 126 | Media::~Media() |
| 127 | { |
| 128 | |
| 129 | } |
| 130 | |
| 131 | pjmedia_type Media::getType() const |
| 132 | { |
| 133 | return type; |
| 134 | } |
| 135 | |
| 136 | /////////////////////////////////////////////////////////////////////////////// |
| 137 | AudioMediaTransmitParam::AudioMediaTransmitParam() |
| 138 | : level(1.0) |
| 139 | { |
| 140 | } |
| 141 | |
| 142 | AudioMedia::AudioMedia() |
| 143 | : Media(PJMEDIA_TYPE_AUDIO), id(PJSUA_INVALID_ID), mediaCachingPool({{{0}}}), |
| 144 | mediaPool(NULL) |
| 145 | { |
| 146 | } |
| 147 | |
| 148 | void AudioMedia::registerMediaPort(MediaPort port) PJSUA2_THROW(Error) |
| 149 | { |
| 150 | /* Check if media already added to Conf bridge. */ |
| 151 | if (!Endpoint::instance().mediaExists(*this) && port != NULL) { |
| 152 | pj_assert(id == PJSUA_INVALID_ID); |
| 153 | |
| 154 | pj_caching_pool_init(&mediaCachingPool, NULL, 0); |
| 155 | |
| 156 | mediaPool = pj_pool_create(&mediaCachingPool.factory, |
| 157 | "media", |
| 158 | 512, |
| 159 | 512, |
| 160 | NULL); |
| 161 | |
| 162 | if (!mediaPool) { |
| 163 | pj_caching_pool_destroy(&mediaCachingPool); |
| 164 | PJSUA2_RAISE_ERROR(PJ_ENOMEM); |
| 165 | } |
| 166 | |
| 167 | PJSUA2_CHECK_EXPR( pjsua_conf_add_port(mediaPool, |
| 168 | (pjmedia_port *)port, |
| 169 | &id) ); |
| 170 | } |
| 171 | |
| 172 | Endpoint::instance().mediaAdd(*this); |
| 173 | } |
| 174 | |
| 175 | void AudioMedia::registerMediaPort2(MediaPort port, pj_pool_t *pool) |
| 176 | PJSUA2_THROW(Error) |
| 177 | { |
| 178 | /* Check if media already added to Conf bridge. */ |
| 179 | pj_assert(!Endpoint::instance().mediaExists(*this)); |
| 180 | |
| 181 | if (port != NULL) { |
| 182 | pj_assert(id == PJSUA_INVALID_ID); |
| 183 | pj_assert(pool); |
| 184 | |
| 185 | PJSUA2_CHECK_EXPR( pjsua_conf_add_port(pool, |
| 186 | (pjmedia_port *)port, |
| 187 | &id) ); |
| 188 | } |
| 189 | |
| 190 | Endpoint::instance().mediaAdd(*this); |
| 191 | } |
| 192 | |
| 193 | void AudioMedia::unregisterMediaPort() PJSUA2_THROW(Error) |
| 194 | { |
| 195 | if (id != PJSUA_INVALID_ID) { |
| 196 | pjsua_conf_remove_port(id); |
| 197 | id = PJSUA_INVALID_ID; |
| 198 | } |
| 199 | |
| 200 | if (mediaPool) { |
| 201 | pj_pool_release(mediaPool); |
| 202 | mediaPool = NULL; |
| 203 | pj_caching_pool_destroy(&mediaCachingPool); |
| 204 | } |
| 205 | |
| 206 | Endpoint::instance().mediaRemove(*this); |
| 207 | } |
| 208 | |
| 209 | AudioMedia::~AudioMedia() |
| 210 | { |
| 211 | } |
| 212 | |
| 213 | ConfPortInfo AudioMedia::getPortInfo() const PJSUA2_THROW(Error) |
| 214 | { |
| 215 | return AudioMedia::getPortInfoFromId(id); |
| 216 | } |
| 217 | |
| 218 | int AudioMedia::getPortId() const |
| 219 | { |
| 220 | return id; |
| 221 | } |
| 222 | |
| 223 | ConfPortInfo AudioMedia::getPortInfoFromId(int port_id) PJSUA2_THROW(Error) |
| 224 | { |
| 225 | pjsua_conf_port_info pj_info; |
| 226 | ConfPortInfo pi; |
| 227 | |
| 228 | PJSUA2_CHECK_EXPR( pjsua_conf_get_port_info(port_id, &pj_info) ); |
| 229 | pi.fromPj(pj_info); |
| 230 | return pi; |
| 231 | } |
| 232 | |
| 233 | void AudioMedia::startTransmit(const AudioMedia &sink) const PJSUA2_THROW(Error) |
| 234 | { |
| 235 | PJSUA2_CHECK_EXPR( pjsua_conf_connect(id, sink.id) ); |
| 236 | } |
| 237 | |
| 238 | void AudioMedia::startTransmit2(const AudioMedia &sink, |
| 239 | const AudioMediaTransmitParam ¶m) const |
| 240 | PJSUA2_THROW(Error) |
| 241 | { |
| 242 | pjsua_conf_connect_param pj_param; |
| 243 | |
| 244 | pjsua_conf_connect_param_default(&pj_param); |
| 245 | pj_param.level = param.level; |
| 246 | PJSUA2_CHECK_EXPR( pjsua_conf_connect2(id, sink.id, &pj_param) ); |
| 247 | } |
| 248 | |
| 249 | void AudioMedia::stopTransmit(const AudioMedia &sink) const PJSUA2_THROW(Error) |
| 250 | { |
| 251 | PJSUA2_CHECK_EXPR( pjsua_conf_disconnect(id, sink.id) ); |
| 252 | } |
| 253 | |
| 254 | void AudioMedia::adjustRxLevel(float level) PJSUA2_THROW(Error) |
| 255 | { |
| 256 | PJSUA2_CHECK_EXPR( pjsua_conf_adjust_tx_level(id, level) ); |
| 257 | } |
| 258 | |
| 259 | void AudioMedia::adjustTxLevel(float level) PJSUA2_THROW(Error) |
| 260 | { |
| 261 | PJSUA2_CHECK_EXPR( pjsua_conf_adjust_rx_level(id, level) ); |
| 262 | } |
| 263 | |
| 264 | unsigned AudioMedia::getRxLevel() const PJSUA2_THROW(Error) |
| 265 | { |
| 266 | unsigned level; |
| 267 | PJSUA2_CHECK_EXPR( pjsua_conf_get_signal_level(id, &level, NULL) ); |
| 268 | return level * 100 / 255; |
| 269 | } |
| 270 | |
| 271 | unsigned AudioMedia::getTxLevel() const PJSUA2_THROW(Error) |
| 272 | { |
| 273 | unsigned level; |
| 274 | PJSUA2_CHECK_EXPR( pjsua_conf_get_signal_level(id, NULL, &level) ); |
| 275 | return level * 100 / 255; |
| 276 | } |
| 277 | |
| 278 | AudioMedia* AudioMedia::typecastFromMedia(Media *media) |
| 279 | { |
| 280 | return static_cast<AudioMedia*>(media); |
| 281 | } |
| 282 | |
| 283 | /////////////////////////////////////////////////////////////////////////////// |
| 284 | |
| 285 | struct port_data { |
| 286 | AudioMediaPort *mport; |
| 287 | pj_pool_t *pool; |
| 288 | }; |
| 289 | |
| 290 | AudioMediaPort::AudioMediaPort() |
| 291 | : pool(NULL), port(NULL) |
| 292 | { |
| 293 | } |
| 294 | |
| 295 | AudioMediaPort::~AudioMediaPort() |
| 296 | { |
| 297 | PJSUA2_CATCH_IGNORE( unregisterMediaPort() ); |
| 298 | if (port) { |
| 299 | struct port_data *pdata = static_cast<struct port_data *> |
| 300 | (port->port_data.pdata); |
| 301 | |
| 302 | /* Make sure port no longer accesses this object in its |
| 303 | * get/put_frame() callback. |
| 304 | */ |
| 305 | if (port->grp_lock) { |
| 306 | pj_grp_lock_acquire(port->grp_lock); |
| 307 | pdata->mport = NULL; |
| 308 | pj_grp_lock_release(port->grp_lock); |
| 309 | } |
| 310 | |
| 311 | pjmedia_port_destroy(port); |
| 312 | /* We release the pool later in port.on_destroy since |
| 313 | * the unregistration is async and may not have completed yet. |
| 314 | */ |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | static pj_status_t get_frame(pjmedia_port *port, pjmedia_frame *frame) |
| 319 | { |
| 320 | struct port_data *pdata = static_cast<struct port_data *> |
| 321 | (port->port_data.pdata); |
| 322 | AudioMediaPort *mport; |
| 323 | MediaFrame frame_; |
| 324 | |
| 325 | pj_grp_lock_acquire(port->grp_lock); |
| 326 | if ((mport = pdata->mport) == NULL) |
| 327 | goto on_return; |
| 328 | |
| 329 | frame_.size = (unsigned)frame->size; |
| 330 | mport->onFrameRequested(frame_); |
| 331 | frame->type = frame_.type; |
| 332 | frame->size = PJ_MIN(frame_.buf.size(), frame_.size); |
| 333 | |
| 334 | #if ((defined(_MSVC_LANG) && _MSVC_LANG <= 199711L) || __cplusplus <= 199711L) |
| 335 | /* C++98 does not have Vector::data() */ |
| 336 | if (frame->size > 0) |
| 337 | pj_memcpy(frame->buf, &frame_.buf[0], frame->size); |
| 338 | #else |
| 339 | /* Newer than C++98 */ |
| 340 | pj_memcpy(frame->buf, frame_.buf.data(), frame->size); |
| 341 | #endif |
| 342 | |
| 343 | on_return: |
| 344 | pj_grp_lock_release(port->grp_lock); |
| 345 | return PJ_SUCCESS; |
| 346 | } |
| 347 | |
| 348 | static pj_status_t put_frame(pjmedia_port *port, pjmedia_frame *frame) |
| 349 | { |
| 350 | struct port_data *pdata = static_cast<struct port_data *> |
| 351 | (port->port_data.pdata); |
| 352 | AudioMediaPort *mport; |
| 353 | MediaFrame frame_; |
| 354 | |
| 355 | pj_grp_lock_acquire(port->grp_lock); |
| 356 | if ((mport = pdata->mport) == NULL) |
| 357 | goto on_return; |
| 358 | |
| 359 | frame_.type = frame->type; |
| 360 | frame_.buf.assign((char *)frame->buf, ((char *)frame->buf) + frame->size); |
| 361 | frame_.size = (unsigned)frame->size; |
| 362 | mport->onFrameReceived(frame_); |
| 363 | |
| 364 | on_return: |
| 365 | pj_grp_lock_release(port->grp_lock); |
| 366 | return PJ_SUCCESS; |
| 367 | } |
| 368 | |
| 369 | static pj_status_t port_on_destroy(pjmedia_port *port) |
| 370 | { |
| 371 | struct port_data *pdata = static_cast<struct port_data *> |
| 372 | (port->port_data.pdata); |
| 373 | pj_pool_t *pool = pdata->pool; |
| 374 | |
| 375 | if (pool) { |
| 376 | pj_pool_release(pool); |
| 377 | } |
| 378 | |
| 379 | return PJ_SUCCESS; |
| 380 | } |
| 381 | |
| 382 | void AudioMediaPort::createPort(const string &name, MediaFormatAudio &fmt) |
| 383 | PJSUA2_THROW(Error) |
| 384 | { |
| 385 | pj_str_t name_; |
| 386 | pjmedia_format fmt_; |
| 387 | struct port_data *pdata; |
| 388 | pj_status_t status; |
| 389 | |
| 390 | if (pool) { |
| 391 | PJSUA2_RAISE_ERROR(PJ_EEXISTS); |
| 392 | } |
| 393 | |
| 394 | pool = pjsua_pool_create( "amport%p", 512, 512); |
| 395 | if (!pool) { |
| 396 | PJSUA2_RAISE_ERROR(PJ_ENOMEM); |
| 397 | } |
| 398 | |
| 399 | /* Init port. */ |
| 400 | port = PJ_POOL_ZALLOC_T(pool, pjmedia_port); |
| 401 | pj_strdup2_with_null(pool, &name_, name.c_str()); |
| 402 | fmt_ = fmt.toPj(); |
| 403 | pjmedia_port_info_init2(&port->info, &name_, |
| 404 | PJMEDIA_SIG_CLASS_APP ('A', 'M', 'P'), |
| 405 | PJMEDIA_DIR_ENCODING_DECODING, &fmt_); |
| 406 | |
| 407 | pdata = PJ_POOL_ZALLOC_T(pool, struct port_data); |
| 408 | pdata->mport = this; |
| 409 | pdata->pool = pool; |
| 410 | port->port_data.pdata = pdata; |
| 411 | port->put_frame = &put_frame; |
| 412 | port->get_frame = &get_frame; |
| 413 | |
| 414 | /* Must implement on_destroy() and create group lock to avoid |
| 415 | * race and premature destroy. |
| 416 | */ |
| 417 | port->on_destroy = &port_on_destroy; |
| 418 | status = pjmedia_port_init_grp_lock(port, pool, NULL); |
| 419 | if (status != PJ_SUCCESS) { |
| 420 | PJSUA2_RAISE_ERROR(status); |
| 421 | } |
| 422 | |
| 423 | registerMediaPort2(port, pool); |
| 424 | } |
| 425 | |
| 426 | /////////////////////////////////////////////////////////////////////////////// |
| 427 | |
| 428 | static void ai_on_event_cb(pjmedia_ai_port *ai_port, |
| 429 | const pjmedia_ai_event *event) |
| 430 | { |
| 431 | pjmedia_port *port = pjmedia_ai_port_get_port(ai_port); |
| 432 | AudioMediaAiPort *amp; |
| 433 | |
| 434 | /* Protect against use-after-free: check the back-pointer under |
| 435 | * grp_lock so the callback sees NULL if destructor has run. |
| 436 | */ |
| 437 | if (!port || !port->grp_lock) |
| 438 | return; |
| 439 | |
| 440 | pj_grp_lock_acquire(port->grp_lock); |
| 441 | amp = static_cast<AudioMediaAiPort *>( |
| 442 | pjmedia_ai_port_get_user_data(ai_port)); |
| 443 | if (!amp) { |
| 444 | pj_grp_lock_release(port->grp_lock); |
| 445 | return; |
| 446 | } |
| 447 | |
| 448 | { |
| 449 | AiMediaEvent ev; |
| 450 | ev.type = event->type; |
| 451 | ev.status = event->status; |
| 452 | if (event->text.slen > 0) |
| 453 | ev.text.assign(event->text.ptr, event->text.slen); |
| 454 | |
| 455 | pj_grp_lock_release(port->grp_lock); |
| 456 | amp->onEvent(ev); |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | |
| 461 | AudioMediaAiPort::AudioMediaAiPort() |
| 462 | : pool(NULL), aiPort(NULL), backend(NULL) |
| 463 | { |
| 464 | } |
| 465 | |
| 466 | static void ai_wrapper_on_destroy(void *member) |
| 467 | { |
| 468 | pj_pool_t *pool = static_cast<pj_pool_t *>(member); |
| 469 | if (pool) |
| 470 | pj_pool_release(pool); |
| 471 | } |
| 472 | |
| 473 | AudioMediaAiPort::~AudioMediaAiPort() |
| 474 | { |
| 475 | if (aiPort) { |
| 476 | /* Null the back-pointer under grp_lock so ai_on_event_cb |
| 477 | * sees NULL and bails out, preventing use-after-free. |
| 478 | */ |
| 479 | pjmedia_port *port = pjmedia_ai_port_get_port(aiPort); |
| 480 | if (port && port->grp_lock) { |
| 481 | pj_grp_lock_acquire(port->grp_lock); |
| 482 | pjmedia_ai_port_set_user_data(aiPort, NULL); |
| 483 | pj_grp_lock_release(port->grp_lock); |
| 484 | } |
| 485 | |
| 486 | PJSUA2_CATCH_IGNORE( disconnect() ); |
| 487 | } |
| 488 | |
| 489 | PJSUA2_CATCH_IGNORE( unregisterMediaPort() ); |
| 490 | |
| 491 | if (aiPort) { |
| 492 | pjmedia_port_destroy(pjmedia_ai_port_get_port(aiPort)); |
| 493 | aiPort = NULL; |
| 494 | } |
| 495 | |
| 496 | /* Note: pool is released by ai_wrapper_on_destroy() via |
| 497 | * grp_lock destroy handler, not here. This ensures the pool |
| 498 | * (which holds the backend struct) outlives the port. |
| 499 | */ |
| 500 | } |
| 501 | |
| 502 | void AudioMediaAiPort::createPort(const AiMediaPortParam &prm) |
| 503 | PJSUA2_THROW(Error) |
| 504 | { |
| 505 | pjmedia_ai_port_param ai_param; |
| 506 | pjmedia_port *media_port; |
| 507 | pjsip_endpoint *endpt; |
| 508 | pj_status_t status; |
| 509 | |
| 510 | if (pool) { |
| 511 | PJSUA2_RAISE_ERROR(PJ_EEXISTS); |
| 512 | } |
| 513 | |
| 514 | pool = pjsua_pool_create("aiamp%p", 4096, 4096); |
| 515 | if (!pool) { |
| 516 | PJSUA2_RAISE_ERROR(PJ_ENOMEM); |
| 517 | } |
| 518 | |
| 519 | /* Use pjsua's SIP endpoint ioqueue and timer heap so that |
| 520 | * WebSocket I/O is polled by pjsua's existing worker threads. |
| 521 | */ |
| 522 | endpt = pjsua_get_pjsip_endpt(); |
| 523 | |
| 524 | /* Create OpenAI backend */ |
| 525 | status = pjmedia_ai_openai_backend_create(pool, &backend); |
| 526 | if (status != PJ_SUCCESS) { |
| 527 | pj_pool_release(pool); |
| 528 | pool = NULL; |
| 529 | PJSUA2_RAISE_ERROR(status); |
| 530 | } |
| 531 | |
| 532 | /* Create AI port */ |
| 533 | pjmedia_ai_port_param_default(&ai_param); |
| 534 | ai_param.ioqueue = pjsip_endpt_get_ioqueue(endpt); |
| 535 | ai_param.timer_heap = pjsip_endpt_get_timer_heap(endpt); |
| 536 | ai_param.cb.on_event = &ai_on_event_cb; |
| 537 | ai_param.user_data = this; |
| 538 | ai_param.backend = backend; |
| 539 | ai_param.vad_enabled = prm.vadEnabled ? PJ_TRUE : PJ_FALSE; |
| 540 | ai_param.ptime_msec = prm.ptimeMsec; |
| 541 | |
| 542 | status = pjmedia_ai_port_create(pool, &ai_param, &aiPort); |
| 543 | if (status != PJ_SUCCESS) { |
| 544 | pj_pool_release(pool); |
| 545 | pool = NULL; |
| 546 | PJSUA2_RAISE_ERROR(status); |
| 547 | } |
| 548 | |
| 549 | media_port = pjmedia_ai_port_get_port(aiPort); |
| 550 | |
| 551 | /* Register a grp_lock destroy handler so the wrapper pool (which |
| 552 | * holds the backend struct) is released only after the port's |
| 553 | * grp_lock ref-count reaches zero and all callbacks have completed. |
| 554 | */ |
| 555 | pj_grp_lock_add_handler(media_port->grp_lock, pool, |
| 556 | pool, &ai_wrapper_on_destroy); |
| 557 | |
| 558 | registerMediaPort2(media_port, pool); |
| 559 | } |
| 560 | |
| 561 | void AudioMediaAiPort::connect(const string &url, const string &authToken) |
| 562 | PJSUA2_THROW(Error) |
| 563 | { |
| 564 | pj_str_t url_str, token_str; |
| 565 | pj_status_t status; |
| 566 | |
| 567 | if (!aiPort) { |
| 568 | PJSUA2_RAISE_ERROR(PJ_EINVALIDOP); |
| 569 | } |
| 570 | |
| 571 | url_str = pj_str((char*)url.c_str()); |
| 572 | token_str = pj_str((char*)authToken.c_str()); |
| 573 | |
| 574 | status = pjmedia_ai_port_connect(aiPort, &url_str, &token_str); |
| 575 | PJSUA2_CHECK_RAISE_ERROR(status); |
| 576 | } |
| 577 | |
| 578 | void AudioMediaAiPort::disconnect() PJSUA2_THROW(Error) |
| 579 | { |
| 580 | pj_status_t status; |
| 581 | |
| 582 | if (!aiPort) { |
| 583 | return; |
| 584 | } |
| 585 | |
| 586 | status = pjmedia_ai_port_disconnect(aiPort); |
| 587 | /* Ignore PJ_EINVALIDOP (not connected) */ |
| 588 | if (status != PJ_SUCCESS && status != PJ_EINVALIDOP) { |
| 589 | PJSUA2_CHECK_RAISE_ERROR(status); |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | /////////////////////////////////////////////////////////////////////////////// |
| 594 | |
| 595 | AudioMediaPlayer::AudioMediaPlayer() |
| 596 | : playerId(PJSUA_INVALID_ID) |
| 597 | { |
| 598 | |
| 599 | } |
| 600 | |
| 601 | AudioMediaPlayer::~AudioMediaPlayer() |
| 602 | { |
| 603 | if (playerId != PJSUA_INVALID_ID) { |
| 604 | PJSUA2_CATCH_IGNORE( unregisterMediaPort() ); |
| 605 | pjsua_player_destroy(playerId); |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | void AudioMediaPlayer::createPlayer(const string &file_name, |
| 610 | unsigned options) |
| 611 | PJSUA2_THROW(Error) |
| 612 | { |
| 613 | if (playerId != PJSUA_INVALID_ID) { |
| 614 | PJSUA2_RAISE_ERROR(PJ_EEXISTS); |
| 615 | } |
| 616 | |
| 617 | pj_str_t pj_name = str2Pj(file_name); |
| 618 | |
| 619 | PJSUA2_CHECK_EXPR( pjsua_player_create(&pj_name, |
| 620 | options, |
| 621 | &playerId) ); |
| 622 | |
| 623 | /* Register EOF callback */ |
| 624 | pjmedia_port *port; |
| 625 | pj_status_t status; |
| 626 | |
| 627 | status = pjsua_player_get_port(playerId, &port); |
| 628 | if (status != PJ_SUCCESS) { |
| 629 | pjsua_player_destroy(playerId); |
| 630 | PJSUA2_RAISE_ERROR2(status, "AudioMediaPlayer::createPlayer()"); |
| 631 | } |
| 632 | status = pjmedia_wav_player_set_eof_cb2(port, this, &eof_cb); |
| 633 | if (status != PJ_SUCCESS) { |
| 634 | pjsua_player_destroy(playerId); |
| 635 | PJSUA2_RAISE_ERROR2(status, "AudioMediaPlayer::createPlayer()"); |
| 636 | } |
| 637 | |
| 638 | /* Get media port id. */ |
| 639 | id = pjsua_player_get_conf_port(playerId); |
| 640 | |
| 641 | registerMediaPort(NULL); |
| 642 | } |
| 643 | |
| 644 | void AudioMediaPlayer::createPlaylist(const StringVector &file_names, |
| 645 | const string &label, |
| 646 | unsigned options) |
| 647 | PJSUA2_THROW(Error) |
| 648 | { |
| 649 | if (playerId != PJSUA_INVALID_ID) { |
| 650 | PJSUA2_RAISE_ERROR(PJ_EEXISTS); |
| 651 | } |
| 652 | |
| 653 | pj_str_t pj_files[MAX_FILE_NAMES]; |
| 654 | unsigned i, count = 0; |
| 655 | pj_str_t pj_lbl = str2Pj(label); |
| 656 | pj_status_t status; |
| 657 | |
| 658 | count = PJ_ARRAY_SIZE(pj_files); |
| 659 | |
| 660 | for(i=0; i<file_names.size() && i<count;++i) |
| 661 | { |
| 662 | const string &file_name = file_names[i]; |
| 663 | |
| 664 | pj_files[i] = str2Pj(file_name); |
| 665 | } |
| 666 | |
| 667 | PJSUA2_CHECK_EXPR( pjsua_playlist_create(pj_files, |
| 668 | i, |
| 669 | &pj_lbl, |
| 670 | options, |
| 671 | &playerId) ); |
| 672 | |
| 673 | /* Register EOF callback */ |
| 674 | pjmedia_port *port; |
| 675 | status = pjsua_player_get_port(playerId, &port); |
| 676 | if (status != PJ_SUCCESS) { |
| 677 | pjsua_player_destroy(playerId); |
| 678 | PJSUA2_RAISE_ERROR2(status, "AudioMediaPlayer::createPlaylist()"); |
| 679 | } |
| 680 | status = pjmedia_wav_playlist_set_eof_cb2(port, this, &eof_cb); |
| 681 | if (status != PJ_SUCCESS) { |
| 682 | pjsua_player_destroy(playerId); |
| 683 | PJSUA2_RAISE_ERROR2(status, "AudioMediaPlayer::createPlaylist()"); |
| 684 | } |
| 685 | |
| 686 | /* Get media port id. */ |
| 687 | id = pjsua_player_get_conf_port(playerId); |
| 688 | |
| 689 | registerMediaPort(NULL); |
| 690 | } |
| 691 | |
| 692 | AudioMediaPlayerInfo AudioMediaPlayer::getInfo() const PJSUA2_THROW(Error) |
| 693 | { |
| 694 | AudioMediaPlayerInfo info; |
| 695 | pjmedia_wav_player_info pj_info; |
| 696 | |
| 697 | PJSUA2_CHECK_EXPR( pjsua_player_get_info(playerId, &pj_info) ); |
| 698 | |
| 699 | pj_bzero(&info, sizeof(info)); |
| 700 | info.formatId = pj_info.fmt_id; |
| 701 | info.payloadBitsPerSample = pj_info.payload_bits_per_sample; |
| 702 | info.sizeBytes = pj_info.size_bytes; |
| 703 | info.sizeSamples = pj_info.size_samples; |
| 704 | |
| 705 | return info; |
| 706 | } |
| 707 | |
| 708 | pj_uint32_t AudioMediaPlayer::getPos() const PJSUA2_THROW(Error) |
| 709 | { |
| 710 | pj_ssize_t pos = pjsua_player_get_pos(playerId); |
| 711 | if (pos < 0) { |
| 712 | PJSUA2_RAISE_ERROR2((pj_status_t)-pos, "AudioMediaPlayer::getPos()"); |
| 713 | } |
| 714 | return (pj_uint32_t)pos; |
| 715 | } |
| 716 | |
| 717 | void AudioMediaPlayer::setPos(pj_uint32_t samples) PJSUA2_THROW(Error) |
| 718 | { |
| 719 | PJSUA2_CHECK_EXPR( pjsua_player_set_pos(playerId, samples) ); |
| 720 | } |
| 721 | |
| 722 | AudioMediaPlayer* AudioMediaPlayer::typecastFromAudioMedia( |
| 723 | AudioMedia *media) |
| 724 | { |
| 725 | return static_cast<AudioMediaPlayer*>(media); |
| 726 | } |
| 727 | |
| 728 | void AudioMediaPlayer::eof_cb(pjmedia_port *port, |
| 729 | void *usr_data) |
| 730 | { |
| 731 | PJ_UNUSED_ARG(port); |
| 732 | AudioMediaPlayer *player = (AudioMediaPlayer*)usr_data; |
| 733 | |
| 734 | player->onEof2(); |
| 735 | } |
| 736 | |
| 737 | /////////////////////////////////////////////////////////////////////////////// |
| 738 | AudioMediaRecorder::AudioMediaRecorder() |
| 739 | : recorderId(PJSUA_INVALID_ID) |
| 740 | { |
| 741 | |
| 742 | } |
| 743 | |
| 744 | AudioMediaRecorder::~AudioMediaRecorder() |
| 745 | { |
| 746 | if (recorderId != PJSUA_INVALID_ID) { |
| 747 | PJSUA2_CATCH_IGNORE( unregisterMediaPort() ); |
| 748 | pjsua_recorder_destroy(recorderId); |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | void AudioMediaRecorder::createRecorder(const string &file_name, |
| 753 | unsigned enc_type, |
| 754 | long max_size, |
| 755 | unsigned options) |
| 756 | PJSUA2_THROW(Error) |
| 757 | { |
| 758 | PJ_UNUSED_ARG(max_size); |
| 759 | |
| 760 | if (recorderId != PJSUA_INVALID_ID) { |
| 761 | PJSUA2_RAISE_ERROR(PJ_EEXISTS); |
| 762 | } |
| 763 | |
| 764 | pj_str_t pj_name = str2Pj(file_name); |
| 765 | |
| 766 | PJSUA2_CHECK_EXPR( pjsua_recorder_create(&pj_name, |
| 767 | enc_type, |
| 768 | NULL, |
| 769 | -1, |
| 770 | options, |
| 771 | &recorderId) ); |
| 772 | |
| 773 | /* Get media port id. */ |
| 774 | id = pjsua_recorder_get_conf_port(recorderId); |
| 775 | |
| 776 | registerMediaPort(NULL); |
| 777 | } |
| 778 | |
| 779 | AudioMediaRecorder* AudioMediaRecorder::typecastFromAudioMedia( |
| 780 | AudioMedia *media) |
| 781 | { |
| 782 | return static_cast<AudioMediaRecorder*>(media); |
| 783 | } |
| 784 | |
| 785 | /////////////////////////////////////////////////////////////////////////////// |
| 786 | |
| 787 | ToneGenerator::ToneGenerator() |
| 788 | : pool(NULL), tonegen(NULL) |
| 789 | { |
| 790 | } |
| 791 | |
| 792 | ToneGenerator::~ToneGenerator() |
| 793 | { |
| 794 | if (tonegen) { |
| 795 | PJSUA2_CATCH_IGNORE( unregisterMediaPort() ); |
| 796 | pjmedia_port_destroy(tonegen); |
| 797 | tonegen = NULL; |
| 798 | } |
| 799 | if (pool) { |
| 800 | pj_pool_release(pool); |
| 801 | pool = NULL; |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | void ToneGenerator::createToneGenerator(unsigned clock_rate, |
| 806 | unsigned channel_count) |
| 807 | PJSUA2_THROW(Error) |
| 808 | { |
| 809 | pj_status_t status; |
| 810 | |
| 811 | if (pool) { |
| 812 | PJSUA2_RAISE_ERROR(PJ_EEXISTS); |
| 813 | } |
| 814 | |
| 815 | pool = pjsua_pool_create( "tonegen%p", 512, 512); |
| 816 | if (!pool) { |
| 817 | PJSUA2_RAISE_ERROR(PJ_ENOMEM); |
| 818 | } |
| 819 | |
| 820 | status = pjmedia_tonegen_create( pool, clock_rate, channel_count, |
| 821 | clock_rate * 20 / 1000, 16, |
| 822 | 0, &tonegen); |
| 823 | if (status != PJ_SUCCESS) { |
| 824 | PJSUA2_RAISE_ERROR(status); |
| 825 | } |
| 826 | |
| 827 | registerMediaPort2(tonegen, pool); |
| 828 | } |
| 829 | |
| 830 | bool ToneGenerator::isBusy() const |
| 831 | { |
| 832 | return tonegen && pjmedia_tonegen_is_busy(tonegen) != 0; |
| 833 | } |
| 834 | |
| 835 | void ToneGenerator::stop() PJSUA2_THROW(Error) |
| 836 | { |
| 837 | pj_status_t status; |
| 838 | |
| 839 | if (!tonegen) { |
| 840 | PJSUA2_RAISE_ERROR(PJ_EINVALIDOP); |
| 841 | } |
| 842 | |
| 843 | status = pjmedia_tonegen_stop(tonegen); |
| 844 | PJSUA2_CHECK_RAISE_ERROR2(status, "ToneGenerator::stop()"); |
| 845 | } |
| 846 | |
| 847 | void ToneGenerator::rewind() PJSUA2_THROW(Error) |
| 848 | { |
| 849 | pj_status_t status; |
| 850 | |
| 851 | if (!tonegen) { |
| 852 | PJSUA2_RAISE_ERROR(PJ_EINVALIDOP); |
| 853 | } |
| 854 | |
| 855 | status = pjmedia_tonegen_rewind(tonegen); |
| 856 | PJSUA2_CHECK_RAISE_ERROR2(status, "ToneGenerator::rewind()"); |
| 857 | } |
| 858 | |
| 859 | void ToneGenerator::play(const ToneDescVector &tones, |
| 860 | bool loop) PJSUA2_THROW(Error) |
| 861 | { |
| 862 | pj_status_t status; |
| 863 | |
| 864 | if (!tonegen) { |
| 865 | PJSUA2_RAISE_ERROR(PJ_EINVALIDOP); |
| 866 | } |
| 867 | if (tones.size() == 0) { |
| 868 | PJSUA2_RAISE_ERROR(PJ_EINVAL); |
| 869 | } |
| 870 | |
| 871 | status = pjmedia_tonegen_play(tonegen, (unsigned)tones.size(), &tones[0], |
| 872 | loop? PJMEDIA_TONEGEN_LOOP : 0); |
| 873 | PJSUA2_CHECK_RAISE_ERROR2(status, "ToneGenerator::play()"); |
| 874 | } |
| 875 | |
| 876 | void ToneGenerator::playDigits(const ToneDigitVector &digits, |
| 877 | bool loop) PJSUA2_THROW(Error) |
| 878 | { |
| 879 | pj_status_t status; |
| 880 | |
| 881 | if (!tonegen) { |
| 882 | PJSUA2_RAISE_ERROR(PJ_EINVALIDOP); |
| 883 | } |
| 884 | if (digits.size() == 0) { |
| 885 | PJSUA2_RAISE_ERROR(PJ_EINVAL); |
| 886 | } |
| 887 | |
| 888 | status = pjmedia_tonegen_play_digits(tonegen, |
| 889 | (unsigned)digits.size(), |
| 890 | &digits[0], |
| 891 | loop? PJMEDIA_TONEGEN_LOOP : 0); |
| 892 | PJSUA2_CHECK_RAISE_ERROR2(status, "ToneGenerator::playDigits()"); |
| 893 | } |
| 894 | |
| 895 | ToneDigitMapVector ToneGenerator::getDigitMap() const PJSUA2_THROW(Error) |
| 896 | { |
| 897 | const pjmedia_tone_digit_map *pdm; |
| 898 | ToneDigitMapVector tdm; |
| 899 | unsigned i; |
| 900 | pj_status_t status; |
| 901 | |
| 902 | if (!tonegen) { |
| 903 | PJSUA2_RAISE_ERROR(PJ_EINVALIDOP); |
| 904 | } |
| 905 | |
| 906 | status = pjmedia_tonegen_get_digit_map(tonegen, &pdm); |
| 907 | PJSUA2_CHECK_RAISE_ERROR2(status, "ToneGenerator::getDigitMap()"); |
| 908 | |
| 909 | for (i=0; i<pdm->count; ++i) { |
| 910 | ToneDigitMapDigit d; |
| 911 | char str_digit[2]; |
| 912 | |
| 913 | str_digit[0] = pdm->digits[i].digit; |
| 914 | str_digit[1] = '\0'; |
| 915 | |
| 916 | d.digit = str_digit; |
| 917 | d.freq1 = pdm->digits[i].freq1; |
| 918 | d.freq2 = pdm->digits[i].freq2; |
| 919 | |
| 920 | tdm.push_back(PJSUA2_MOVE(d)); |
| 921 | } |
| 922 | |
| 923 | return tdm; |
| 924 | } |
| 925 | |
| 926 | void ToneGenerator::setDigitMap(const ToneDigitMapVector &digit_map) |
| 927 | PJSUA2_THROW(Error) |
| 928 | { |
| 929 | unsigned i; |
| 930 | pj_status_t status; |
| 931 | |
| 932 | if (!tonegen) { |
| 933 | PJSUA2_RAISE_ERROR(PJ_EINVALIDOP); |
| 934 | } |
| 935 | |
| 936 | digitMap.count = (unsigned)digit_map.size(); |
| 937 | if (digitMap.count > PJ_ARRAY_SIZE(digitMap.digits)) |
| 938 | digitMap.count = PJ_ARRAY_SIZE(digitMap.digits); |
| 939 | |
| 940 | for (i=0; i<digitMap.count; ++i) { |
| 941 | digitMap.digits[i].digit = digit_map[i].digit.c_str()[0]; |
| 942 | digitMap.digits[i].freq1 = (short)digit_map[i].freq1; |
| 943 | digitMap.digits[i].freq2 = (short)digit_map[i].freq2; |
| 944 | } |
| 945 | |
| 946 | status = pjmedia_tonegen_set_digit_map(tonegen, &digitMap); |
| 947 | PJSUA2_CHECK_RAISE_ERROR2(status, "ToneGenerator::setDigitMap()"); |
| 948 | } |
| 949 | |
| 950 | |
| 951 | /////////////////////////////////////////////////////////////////////////////// |
| 952 | void AudioDevInfo::fromPj(const pjmedia_aud_dev_info &dev_info) |
| 953 | { |
| 954 | id = dev_info.id; |
| 955 | name = dev_info.name; |
| 956 | inputCount = dev_info.input_count; |
| 957 | outputCount = dev_info.output_count; |
| 958 | defaultSamplesPerSec = dev_info.default_samples_per_sec; |
| 959 | driver = dev_info.driver; |
| 960 | caps = dev_info.caps; |
| 961 | routes = dev_info.routes; |
| 962 | |
| 963 | for (unsigned i=0; i<dev_info.ext_fmt_cnt;++i) { |
| 964 | MediaFormatAudio format; |
| 965 | format.fromPj(dev_info.ext_fmt[i]); |
| 966 | if (format.type == PJMEDIA_TYPE_AUDIO) |
| 967 | extFmt.push_back(PJSUA2_MOVE(format)); |
| 968 | } |
| 969 | } |
| 970 | |
| 971 | AudioDevInfo::~AudioDevInfo() |
| 972 | { |
| 973 | } |
| 974 | |
| 975 | /////////////////////////////////////////////////////////////////////////////// |
| 976 | /* |
| 977 | * Simple AudioMedia class for sound device. |
| 978 | */ |
| 979 | class DevAudioMedia : public AudioMedia |
| 980 | { |
| 981 | public: |
| 982 | DevAudioMedia(); |
| 983 | virtual ~DevAudioMedia(); |
| 984 | }; |
| 985 | |
| 986 | DevAudioMedia::DevAudioMedia() |
| 987 | { |
| 988 | this->id = 0; |
| 989 | registerMediaPort(NULL); |
| 990 | } |
| 991 | |
| 992 | DevAudioMedia::~DevAudioMedia() |
| 993 | { |
| 994 | /* Avoid removing this port (conf port id=0) from conference */ |
| 995 | this->id = PJSUA_INVALID_ID; |
| 996 | PJSUA2_CATCH_IGNORE( unregisterMediaPort() ); |
| 997 | } |
| 998 | |
| 999 | /////////////////////////////////////////////////////////////////////////////// |
| 1000 | /* Audio device operations. */ |
| 1001 | |
| 1002 | AudDevManager::AudDevManager() |
| 1003 | : devMedia(NULL) |
| 1004 | { |
| 1005 | } |
| 1006 | |
| 1007 | AudDevManager::~AudDevManager() |
| 1008 | { |
| 1009 | // At this point, devMedia should have been cleaned up by Endpoint, |
| 1010 | // as AudDevManager destructor is called after Endpoint destructor. |
| 1011 | #if DEPRECATED_FOR_TICKET_2232 |
| 1012 | // Starting from ticket #2232, Endpoint no longer cleans up media. |
| 1013 | if (devMedia) { |
| 1014 | delete devMedia; |
| 1015 | devMedia = NULL; |
| 1016 | } |
| 1017 | #endif |
| 1018 | |
| 1019 | clearAudioDevList(); |
| 1020 | } |
| 1021 | |
| 1022 | int AudDevManager::getCaptureDev() const PJSUA2_THROW(Error) |
| 1023 | { |
| 1024 | return getActiveDev(true); |
| 1025 | } |
| 1026 | |
| 1027 | AudioMedia &AudDevManager::getCaptureDevMedia() PJSUA2_THROW(Error) |
| 1028 | { |
| 1029 | if (!devMedia) |
| 1030 | devMedia = new DevAudioMedia; |
| 1031 | return *devMedia; |
| 1032 | } |
| 1033 | |
| 1034 | int AudDevManager::getPlaybackDev() const PJSUA2_THROW(Error) |
| 1035 | { |
| 1036 | return getActiveDev(false); |
| 1037 | } |
| 1038 | |
| 1039 | AudioMedia &AudDevManager::getPlaybackDevMedia() PJSUA2_THROW(Error) |
| 1040 | { |
| 1041 | if (!devMedia) |
| 1042 | devMedia = new DevAudioMedia; |
| 1043 | return *devMedia; |
| 1044 | } |
| 1045 | |
| 1046 | void AudDevManager::setCaptureDev(int capture_dev) const PJSUA2_THROW(Error) |
| 1047 | { |
| 1048 | pjsua_snd_dev_param param; |
| 1049 | |
| 1050 | pjsua_snd_dev_param_default(¶m); |
| 1051 | PJSUA2_CHECK_EXPR(pjsua_get_snd_dev2(¶m)); |
| 1052 | param.capture_dev = capture_dev; |
| 1053 | |
| 1054 | /* Normalize invalid ID or null device to default device */ |
| 1055 | if (param.playback_dev == PJMEDIA_AUD_INVALID_DEV || |
| 1056 | param.playback_dev == PJSUA_SND_NULL_DEV) |
| 1057 | { |
| 1058 | param.playback_dev = PJMEDIA_AUD_DEFAULT_PLAYBACK_DEV; |
| 1059 | } |
| 1060 | |
| 1061 | param.mode |= PJSUA_SND_DEV_NO_IMMEDIATE_OPEN; |
| 1062 | |
| 1063 | PJSUA2_CHECK_EXPR( pjsua_set_snd_dev2(¶m) ); |
| 1064 | } |
| 1065 | |
| 1066 | void AudDevManager::setPlaybackDev(int playback_dev) const PJSUA2_THROW(Error) |
| 1067 | { |
| 1068 | pjsua_snd_dev_param param; |
| 1069 | |
| 1070 | pjsua_snd_dev_param_default(¶m); |
| 1071 | PJSUA2_CHECK_EXPR(pjsua_get_snd_dev2(¶m)); |
| 1072 | param.playback_dev = playback_dev; |
| 1073 | |
| 1074 | /* Normalize invalid ID or null device to default device */ |
| 1075 | if (param.capture_dev == PJMEDIA_AUD_INVALID_DEV || |
| 1076 | param.capture_dev == PJSUA_SND_NULL_DEV) |
| 1077 | { |
| 1078 | param.capture_dev = PJMEDIA_AUD_DEFAULT_CAPTURE_DEV; |
| 1079 | } |
| 1080 | |
| 1081 | param.mode |= PJSUA_SND_DEV_NO_IMMEDIATE_OPEN; |
| 1082 | |
| 1083 | PJSUA2_CHECK_EXPR( pjsua_set_snd_dev2(¶m) ); |
| 1084 | } |
| 1085 | |
| 1086 | #if !DEPRECATED_FOR_TICKET_2232 |
| 1087 | const AudioDevInfoVector &AudDevManager::enumDev() PJSUA2_THROW(Error) |
| 1088 | { |
| 1089 | pjmedia_aud_dev_info pj_info[MAX_DEV_COUNT]; |
| 1090 | unsigned count = MAX_DEV_COUNT; |
| 1091 | |
| 1092 | PJSUA2_CHECK_EXPR( pjsua_enum_aud_devs(pj_info, &count) ); |
| 1093 | |
| 1094 | pj_enter_critical_section(); |
| 1095 | clearAudioDevList(); |
| 1096 | for (unsigned i = 0; i<count ;++i) { |
| 1097 | AudioDevInfo *dev_info = new AudioDevInfo; |
| 1098 | dev_info->fromPj(pj_info[i]); |
| 1099 | audioDevList.push_back(dev_info); |
| 1100 | } |
| 1101 | pj_leave_critical_section(); |
| 1102 | return audioDevList; |
| 1103 | } |
| 1104 | #endif |
| 1105 | |
| 1106 | AudioDevInfoVector2 AudDevManager::enumDev2() const PJSUA2_THROW(Error) |
| 1107 | { |
| 1108 | pjmedia_aud_dev_info pj_info[MAX_DEV_COUNT]; |
| 1109 | unsigned count = MAX_DEV_COUNT; |
| 1110 | AudioDevInfoVector2 adiv2; |
| 1111 | |
| 1112 | PJSUA2_CHECK_EXPR( pjsua_enum_aud_devs(pj_info, &count) ); |
| 1113 | |
| 1114 | for (unsigned i = 0; i<count ;++i) { |
| 1115 | AudioDevInfo di; |
| 1116 | di.fromPj(pj_info[i]); |
| 1117 | adiv2.push_back(PJSUA2_MOVE(di)); |
| 1118 | } |
| 1119 | |
| 1120 | return adiv2; |
| 1121 | } |
| 1122 | |
| 1123 | void AudDevManager::setNullDev() PJSUA2_THROW(Error) |
| 1124 | { |
| 1125 | PJSUA2_CHECK_EXPR( pjsua_set_null_snd_dev() ); |
| 1126 | } |
| 1127 | |
| 1128 | MediaPort *AudDevManager::setNoDev() |
| 1129 | { |
| 1130 | return (MediaPort*)pjsua_set_no_snd_dev(); |
| 1131 | } |
| 1132 | |
| 1133 | void AudDevManager::setSndDevMode(unsigned mode) const PJSUA2_THROW(Error) |
| 1134 | { |
| 1135 | pjsua_snd_dev_param param; |
| 1136 | |
| 1137 | pjsua_snd_dev_param_default(¶m); |
| 1138 | PJSUA2_CHECK_EXPR(pjsua_get_snd_dev2(¶m)); |
| 1139 | param.mode = mode; |
| 1140 | PJSUA2_CHECK_EXPR( pjsua_set_snd_dev2(¶m) ); |
| 1141 | } |
| 1142 | |
| 1143 | void AudDevManager::setEcOptions(unsigned tail_msec, |
| 1144 | unsigned options) PJSUA2_THROW(Error) |
| 1145 | { |
| 1146 | PJSUA2_CHECK_EXPR( pjsua_set_ec(tail_msec, options) ); |
| 1147 | } |
| 1148 | |
| 1149 | unsigned AudDevManager::getEcTail() const PJSUA2_THROW(Error) |
| 1150 | { |
| 1151 | unsigned tail_msec = 0; |
| 1152 | |
| 1153 | PJSUA2_CHECK_EXPR( pjsua_get_ec_tail(&tail_msec) ); |
| 1154 | |
| 1155 | return tail_msec; |
| 1156 | } |
| 1157 | |
| 1158 | bool AudDevManager::sndIsActive() const |
| 1159 | { |
| 1160 | return PJ2BOOL(pjsua_snd_is_active()); |
| 1161 | } |
| 1162 | |
| 1163 | void AudDevManager::refreshDevs() PJSUA2_THROW(Error) |
| 1164 | { |
| 1165 | PJSUA2_CHECK_EXPR( pjmedia_aud_dev_refresh() ); |
| 1166 | } |
| 1167 | |
| 1168 | unsigned AudDevManager::getDevCount() const |
| 1169 | { |
| 1170 | return pjmedia_aud_dev_count(); |
| 1171 | } |
| 1172 | |
| 1173 | AudioDevInfo |
| 1174 | AudDevManager::getDevInfo(int id) const PJSUA2_THROW(Error) |
| 1175 | { |
| 1176 | AudioDevInfo dev_info; |
| 1177 | pjmedia_aud_dev_info pj_info; |
| 1178 | |
| 1179 | PJSUA2_CHECK_EXPR( pjmedia_aud_dev_get_info(id, &pj_info) ); |
| 1180 | |
| 1181 | dev_info.fromPj(pj_info); |
| 1182 | return dev_info; |
| 1183 | } |
| 1184 | |
| 1185 | int AudDevManager::lookupDev(const string &drv_name, |
| 1186 | const string &dev_name) const PJSUA2_THROW(Error) |
| 1187 | { |
| 1188 | pjmedia_aud_dev_index pj_idx = 0; |
| 1189 | |
| 1190 | PJSUA2_CHECK_EXPR( pjmedia_aud_dev_lookup(drv_name.c_str(), |
| 1191 | dev_name.c_str(), |
| 1192 | &pj_idx) ); |
| 1193 | |
| 1194 | return pj_idx; |
| 1195 | } |
| 1196 | |
| 1197 | |
| 1198 | string AudDevManager::capName(pjmedia_aud_dev_cap cap) const |
| 1199 | { |
| 1200 | return pjmedia_aud_dev_cap_name(cap, NULL); |
| 1201 | } |
| 1202 | |
| 1203 | void |
| 1204 | AudDevManager::setExtFormat(const MediaFormatAudio &format, |
| 1205 | bool keep) PJSUA2_THROW(Error) |
| 1206 | { |
| 1207 | pjmedia_format pj_format = format.toPj(); |
| 1208 | |
| 1209 | PJSUA2_CHECK_EXPR( pjsua_snd_set_setting(PJMEDIA_AUD_DEV_CAP_EXT_FORMAT, |
| 1210 | &pj_format, |
| 1211 | keep) ); |
| 1212 | } |
| 1213 | |
| 1214 | MediaFormatAudio AudDevManager::getExtFormat() const PJSUA2_THROW(Error) |
| 1215 | { |
| 1216 | pjmedia_format pj_format; |
| 1217 | MediaFormatAudio format; |
| 1218 | |
| 1219 | PJSUA2_CHECK_EXPR( pjsua_snd_get_setting(PJMEDIA_AUD_DEV_CAP_EXT_FORMAT, |
| 1220 | &pj_format) ); |
| 1221 | |
| 1222 | format.fromPj(pj_format); |
| 1223 | |
| 1224 | return format; |
| 1225 | } |
| 1226 | |
| 1227 | void AudDevManager::setInputLatency(unsigned latency_msec, |
| 1228 | bool keep) PJSUA2_THROW(Error) |
| 1229 | { |
| 1230 | PJSUA2_CHECK_EXPR( pjsua_snd_set_setting(PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY, |
| 1231 | &latency_msec, |
| 1232 | keep) ); |
| 1233 | } |
| 1234 | |
| 1235 | unsigned AudDevManager::getInputLatency() const PJSUA2_THROW(Error) |
| 1236 | { |
| 1237 | unsigned latency_msec = 0; |
| 1238 | |
| 1239 | PJSUA2_CHECK_EXPR( pjsua_snd_get_setting(PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY, |
| 1240 | &latency_msec) ); |
| 1241 | |
| 1242 | return latency_msec; |
| 1243 | } |
| 1244 | |
| 1245 | void |
| 1246 | AudDevManager::setOutputLatency(unsigned latency_msec, |
| 1247 | bool keep) PJSUA2_THROW(Error) |
| 1248 | { |
| 1249 | PJSUA2_CHECK_EXPR( pjsua_snd_set_setting(PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY, |
| 1250 | &latency_msec, |
| 1251 | keep) ); |
| 1252 | } |
| 1253 | |
| 1254 | unsigned AudDevManager::getOutputLatency() const PJSUA2_THROW(Error) |
| 1255 | { |
| 1256 | unsigned latency_msec = 0; |
| 1257 | |
| 1258 | PJSUA2_CHECK_EXPR( pjsua_snd_get_setting(PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY, |
| 1259 | &latency_msec) ); |
| 1260 | |
| 1261 | return latency_msec; |
| 1262 | } |
| 1263 | |
| 1264 | void AudDevManager::setInputVolume(unsigned volume, bool keep) |
| 1265 | PJSUA2_THROW(Error) |
| 1266 | { |
| 1267 | PJSUA2_CHECK_EXPR( |
| 1268 | pjsua_snd_set_setting(PJMEDIA_AUD_DEV_CAP_INPUT_VOLUME_SETTING, |
| 1269 | &volume, |
| 1270 | keep) ); |
| 1271 | } |
| 1272 | |
| 1273 | unsigned AudDevManager::getInputVolume() const PJSUA2_THROW(Error) |
| 1274 | { |
| 1275 | unsigned volume = 0; |
| 1276 | |
| 1277 | PJSUA2_CHECK_EXPR( |
| 1278 | pjsua_snd_get_setting(PJMEDIA_AUD_DEV_CAP_INPUT_VOLUME_SETTING, |
| 1279 | &volume) ); |
| 1280 | |
| 1281 | return volume; |
| 1282 | } |
| 1283 | |
| 1284 | void AudDevManager::setOutputVolume(unsigned volume, bool keep) |
| 1285 | PJSUA2_THROW(Error) |
| 1286 | { |
| 1287 | PJSUA2_CHECK_EXPR( |
| 1288 | pjsua_snd_set_setting(PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING, |
| 1289 | &volume, |
| 1290 | keep) ); |
| 1291 | } |
| 1292 | |
| 1293 | unsigned AudDevManager::getOutputVolume() const PJSUA2_THROW(Error) |
| 1294 | { |
| 1295 | unsigned volume = 0; |
| 1296 | |
| 1297 | PJSUA2_CHECK_EXPR( |
| 1298 | pjsua_snd_get_setting(PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING, |
| 1299 | &volume) ); |
| 1300 | |
| 1301 | return volume; |
| 1302 | } |
| 1303 | |
| 1304 | unsigned AudDevManager::getInputSignal() const PJSUA2_THROW(Error) |
| 1305 | { |
| 1306 | unsigned signal = 0; |
| 1307 | |
| 1308 | PJSUA2_CHECK_EXPR( |
| 1309 | pjsua_snd_get_setting(PJMEDIA_AUD_DEV_CAP_INPUT_SIGNAL_METER, |
| 1310 | &signal) ); |
| 1311 | |
| 1312 | return signal; |
| 1313 | } |
| 1314 | |
| 1315 | unsigned AudDevManager::getOutputSignal() const PJSUA2_THROW(Error) |
| 1316 | { |
| 1317 | unsigned signal = 0; |
| 1318 | |
| 1319 | PJSUA2_CHECK_EXPR( |
| 1320 | pjsua_snd_get_setting(PJMEDIA_AUD_DEV_CAP_OUTPUT_SIGNAL_METER, |
| 1321 | &signal) ); |
| 1322 | |
| 1323 | return signal; |
| 1324 | } |
| 1325 | |
| 1326 | void |
| 1327 | AudDevManager::setInputRoute(pjmedia_aud_dev_route route, |
| 1328 | bool keep) PJSUA2_THROW(Error) |
| 1329 | { |
| 1330 | PJSUA2_CHECK_EXPR( pjsua_snd_set_setting(PJMEDIA_AUD_DEV_CAP_INPUT_ROUTE, |
| 1331 | &route, |
| 1332 | keep) ); |
| 1333 | } |
| 1334 | |
| 1335 | pjmedia_aud_dev_route AudDevManager::getInputRoute() const PJSUA2_THROW(Error) |
| 1336 | { |
| 1337 | pjmedia_aud_dev_route route = PJMEDIA_AUD_DEV_ROUTE_DEFAULT; |
| 1338 | |
| 1339 | PJSUA2_CHECK_EXPR( pjsua_snd_get_setting(PJMEDIA_AUD_DEV_CAP_INPUT_ROUTE, |
| 1340 | &route) ); |
| 1341 | |
| 1342 | return route; |
| 1343 | } |
| 1344 | |
| 1345 | void |
| 1346 | AudDevManager::setOutputRoute(pjmedia_aud_dev_route route, |
| 1347 | bool keep) PJSUA2_THROW(Error) |
| 1348 | { |
| 1349 | PJSUA2_CHECK_EXPR( pjsua_snd_set_setting(PJMEDIA_AUD_DEV_CAP_OUTPUT_ROUTE, |
| 1350 | &route, |
| 1351 | keep) ); |
| 1352 | } |
| 1353 | |
| 1354 | pjmedia_aud_dev_route AudDevManager::getOutputRoute() const |
| 1355 | PJSUA2_THROW(Error) |
| 1356 | { |
| 1357 | pjmedia_aud_dev_route route = PJMEDIA_AUD_DEV_ROUTE_DEFAULT; |
| 1358 | |
| 1359 | PJSUA2_CHECK_EXPR( pjsua_snd_get_setting(PJMEDIA_AUD_DEV_CAP_OUTPUT_ROUTE, |
| 1360 | &route) ); |
| 1361 | |
| 1362 | return route; |
| 1363 | } |
| 1364 | |
| 1365 | void AudDevManager::setVad(bool enable, bool keep) PJSUA2_THROW(Error) |
| 1366 | { |
| 1367 | PJSUA2_CHECK_EXPR( pjsua_snd_set_setting(PJMEDIA_AUD_DEV_CAP_VAD, |
| 1368 | &enable, |
| 1369 | keep) ); |
| 1370 | } |
| 1371 | |
| 1372 | bool AudDevManager::getVad() const PJSUA2_THROW(Error) |
| 1373 | { |
| 1374 | bool enable = false; |
| 1375 | |
| 1376 | PJSUA2_CHECK_EXPR( pjsua_snd_get_setting(PJMEDIA_AUD_DEV_CAP_VAD, |
| 1377 | &enable) ); |
| 1378 | |
| 1379 | return enable; |
| 1380 | } |
| 1381 | |
| 1382 | void AudDevManager::setCng(bool enable, bool keep) PJSUA2_THROW(Error) |
| 1383 | { |
| 1384 | PJSUA2_CHECK_EXPR( pjsua_snd_set_setting(PJMEDIA_AUD_DEV_CAP_CNG, |
| 1385 | &enable, |
| 1386 | keep) ); |
| 1387 | } |
| 1388 | |
| 1389 | bool AudDevManager::getCng() const PJSUA2_THROW(Error) |
| 1390 | { |
| 1391 | bool enable = false; |
| 1392 | |
| 1393 | PJSUA2_CHECK_EXPR( pjsua_snd_get_setting(PJMEDIA_AUD_DEV_CAP_CNG, |
| 1394 | &enable) ); |
| 1395 | |
| 1396 | return enable; |
| 1397 | } |
| 1398 | |
| 1399 | void AudDevManager::setPlc(bool enable, bool keep) PJSUA2_THROW(Error) |
| 1400 | { |
| 1401 | PJSUA2_CHECK_EXPR( pjsua_snd_set_setting(PJMEDIA_AUD_DEV_CAP_PLC, |
| 1402 | &enable, |
| 1403 | keep) ); |
| 1404 | } |
| 1405 | |
| 1406 | bool AudDevManager::getPlc() const PJSUA2_THROW(Error) |
| 1407 | { |
| 1408 | bool enable = false; |
| 1409 | |
| 1410 | PJSUA2_CHECK_EXPR( pjsua_snd_get_setting(PJMEDIA_AUD_DEV_CAP_PLC, |
| 1411 | &enable) ); |
| 1412 | |
| 1413 | return enable; |
| 1414 | } |
| 1415 | |
| 1416 | void AudDevManager::clearAudioDevList() |
| 1417 | { |
| 1418 | #if !DEPRECATED_FOR_TICKET_2232 |
| 1419 | for(unsigned i=0;i<audioDevList.size();++i) { |
| 1420 | delete audioDevList[i]; |
| 1421 | } |
| 1422 | audioDevList.clear(); |
| 1423 | #endif |
| 1424 | } |
| 1425 | |
| 1426 | int AudDevManager::getActiveDev(bool is_capture) const PJSUA2_THROW(Error) |
| 1427 | { |
| 1428 | int capture_dev = 0, playback_dev = 0; |
| 1429 | PJSUA2_CHECK_EXPR( pjsua_get_snd_dev(&capture_dev, &playback_dev) ); |
| 1430 | |
| 1431 | return is_capture?capture_dev:playback_dev; |
| 1432 | } |
| 1433 | |
| 1434 | |
| 1435 | /////////////////////////////////////////////////////////////////////////////// |
| 1436 | ExtraAudioDevice::ExtraAudioDevice (int playdev, int recdev) : |
| 1437 | playDev(playdev), recDev(recdev), ext_snd_dev(NULL) |
| 1438 | { |
| 1439 | } |
| 1440 | |
| 1441 | ExtraAudioDevice::~ExtraAudioDevice() |
| 1442 | { |
| 1443 | PJSUA2_CATCH_IGNORE( close() ); |
| 1444 | } |
| 1445 | |
| 1446 | void ExtraAudioDevice::open() |
| 1447 | { |
| 1448 | pj_status_t status; |
| 1449 | |
| 1450 | /* Opened already? */ |
| 1451 | if (isOpened()) |
| 1452 | return; |
| 1453 | |
| 1454 | /* Get port info of conference bridge master port */ |
| 1455 | pjsua_conf_port_info master_info; |
| 1456 | status = pjsua_conf_get_port_info(0, &master_info); |
| 1457 | PJSUA2_CHECK_RAISE_ERROR(status); |
| 1458 | |
| 1459 | /* Generate sound device port param */ |
| 1460 | pjmedia_snd_port_param param; |
| 1461 | pjmedia_snd_port_param_default(¶m); |
| 1462 | |
| 1463 | status = pjmedia_aud_dev_default_param(recDev, ¶m.base); |
| 1464 | PJSUA2_CHECK_RAISE_ERROR(status); |
| 1465 | |
| 1466 | param.base.dir = PJMEDIA_DIR_CAPTURE_PLAYBACK; |
| 1467 | param.base.play_id = playDev; |
| 1468 | param.base.rec_id = recDev; |
| 1469 | param.base.clock_rate = master_info.clock_rate; |
| 1470 | param.base.channel_count = 1; |
| 1471 | param.base.samples_per_frame = master_info.samples_per_frame / |
| 1472 | master_info.channel_count; |
| 1473 | param.base.bits_per_sample = master_info.bits_per_sample; |
| 1474 | |
| 1475 | /* Create the extra sound device */ |
| 1476 | pjsua_ext_snd_dev *snd_dev; |
| 1477 | status = pjsua_ext_snd_dev_create(¶m, &snd_dev); |
| 1478 | PJSUA2_CHECK_RAISE_ERROR(status); |
| 1479 | ext_snd_dev = snd_dev; |
| 1480 | |
| 1481 | /* Register to the conference bridge */ |
| 1482 | registerMediaPort(NULL); |
| 1483 | id = pjsua_ext_snd_dev_get_conf_port(snd_dev); |
| 1484 | } |
| 1485 | |
| 1486 | bool ExtraAudioDevice::isOpened() |
| 1487 | { |
| 1488 | return (id != PJSUA_INVALID_ID); |
| 1489 | } |
| 1490 | |
| 1491 | void ExtraAudioDevice::close() PJSUA2_THROW(Error) |
| 1492 | { |
| 1493 | /* Unregister from the conference bridge */ |
| 1494 | id = PJSUA_INVALID_ID; |
| 1495 | unregisterMediaPort(); |
| 1496 | |
| 1497 | /* Destroy the extra sound device */ |
| 1498 | if (ext_snd_dev) { |
| 1499 | pjsua_ext_snd_dev *snd_dev = (pjsua_ext_snd_dev*)ext_snd_dev; |
| 1500 | ext_snd_dev = NULL; |
| 1501 | pjsua_ext_snd_dev_destroy(snd_dev); |
| 1502 | } |
| 1503 | } |
| 1504 | |
| 1505 | /////////////////////////////////////////////////////////////////////////////// |
| 1506 | VideoWindow::VideoWindow(pjsua_vid_win_id win_id) |
| 1507 | : winId(win_id) |
| 1508 | { |
| 1509 | #if !PJSUA_HAS_VIDEO |
| 1510 | /* Suppress warning of unused field when video is disabled */ |
| 1511 | PJ_UNUSED_ARG(winId); |
| 1512 | #endif |
| 1513 | } |
| 1514 | |
| 1515 | VideoWindowInfo VideoWindow::getInfo() const PJSUA2_THROW(Error) |
| 1516 | { |
| 1517 | VideoWindowInfo vwi; |
| 1518 | pj_bzero(&vwi, sizeof(vwi)); |
| 1519 | #if PJSUA_HAS_VIDEO |
| 1520 | pjsua_vid_win_info pj_vwi; |
| 1521 | |
| 1522 | PJSUA2_CHECK_EXPR( pjsua_vid_win_get_info(winId, &pj_vwi) ); |
| 1523 | vwi.isNative = (pj_vwi.is_native != PJ_FALSE); |
| 1524 | vwi.winHandle.type = pj_vwi.hwnd.type; |
| 1525 | vwi.winHandle.handle.window = pj_vwi.hwnd.info.window; |
| 1526 | vwi.renderDeviceId = pj_vwi.rdr_dev; |
| 1527 | vwi.show = (pj_vwi.show != PJ_FALSE); |
| 1528 | vwi.pos.x = pj_vwi.pos.x; |
| 1529 | vwi.pos.y = pj_vwi.pos.y; |
| 1530 | vwi.size.w = pj_vwi.size.w; |
| 1531 | vwi.size.h = pj_vwi.size.h; |
| 1532 | |
| 1533 | #endif |
| 1534 | return vwi; |
| 1535 | } |
| 1536 | |
| 1537 | VideoMedia VideoWindow::getVideoMedia() PJSUA2_THROW(Error) |
| 1538 | { |
| 1539 | #if PJSUA_HAS_VIDEO |
| 1540 | pjsua_vid_win_info pj_vwi; |
| 1541 | PJSUA2_CHECK_EXPR( pjsua_vid_win_get_info(winId, &pj_vwi) ); |
| 1542 | |
| 1543 | pjsua_conf_port_id id = pj_vwi.slot_id; |
| 1544 | if (id != PJSUA_INVALID_ID) { |
| 1545 | VideoMediaHelper vm; |
| 1546 | vm.setPortId(id); |
| 1547 | return vm; |
| 1548 | } else { |
| 1549 | PJSUA2_RAISE_ERROR(PJ_ENOTFOUND); |
| 1550 | } |
| 1551 | #else |
| 1552 | PJSUA2_RAISE_ERROR(PJ_EINVALIDOP); |
| 1553 | #endif |
| 1554 | } |
| 1555 | |
| 1556 | void VideoWindow::Show(bool show) PJSUA2_THROW(Error) |
| 1557 | { |
| 1558 | #if PJSUA_HAS_VIDEO |
| 1559 | PJSUA2_CHECK_EXPR( pjsua_vid_win_set_show(winId, show) ); |
| 1560 | #else |
| 1561 | PJ_UNUSED_ARG(show); |
| 1562 | #endif |
| 1563 | } |
| 1564 | |
| 1565 | void VideoWindow::setPos(const MediaCoordinate &pos) PJSUA2_THROW(Error) |
| 1566 | { |
| 1567 | #if PJSUA_HAS_VIDEO |
| 1568 | pjmedia_coord pj_pos; |
| 1569 | |
| 1570 | pj_pos.x = pos.x; |
| 1571 | pj_pos.y = pos.y; |
| 1572 | PJSUA2_CHECK_EXPR( pjsua_vid_win_set_pos(winId, &pj_pos) ); |
| 1573 | #else |
| 1574 | PJ_UNUSED_ARG(pos); |
| 1575 | #endif |
| 1576 | } |
| 1577 | |
| 1578 | void VideoWindow::setSize(const MediaSize &size) PJSUA2_THROW(Error) |
| 1579 | { |
| 1580 | #if PJSUA_HAS_VIDEO |
| 1581 | pjmedia_rect_size pj_size; |
| 1582 | |
| 1583 | pj_size.w = size.w; |
| 1584 | pj_size.h = size.h; |
| 1585 | PJSUA2_CHECK_EXPR( pjsua_vid_win_set_size(winId, &pj_size) ); |
| 1586 | #else |
| 1587 | PJ_UNUSED_ARG(size); |
| 1588 | #endif |
| 1589 | } |
| 1590 | |
| 1591 | void VideoWindow::rotate(int angle) PJSUA2_THROW(Error) |
| 1592 | { |
| 1593 | #if PJSUA_HAS_VIDEO |
| 1594 | PJSUA2_CHECK_EXPR( pjsua_vid_win_rotate(winId, angle) ); |
| 1595 | #else |
| 1596 | PJ_UNUSED_ARG(angle); |
| 1597 | #endif |
| 1598 | } |
| 1599 | |
| 1600 | void VideoWindow::setWindow(const VideoWindowHandle &win) PJSUA2_THROW(Error) |
| 1601 | { |
| 1602 | #if PJSUA_HAS_VIDEO |
| 1603 | pjmedia_vid_dev_hwnd vhwnd; |
| 1604 | |
| 1605 | vhwnd.type = win.type; |
| 1606 | vhwnd.info.window = win.handle.window; |
| 1607 | PJSUA2_CHECK_EXPR( pjsua_vid_win_set_win(winId, &vhwnd) ); |
| 1608 | #else |
| 1609 | PJ_UNUSED_ARG(win); |
| 1610 | #endif |
| 1611 | } |
| 1612 | |
| 1613 | void VideoWindow::setFullScreen(bool enabled) PJSUA2_THROW(Error) |
| 1614 | { |
| 1615 | #if PJSUA_HAS_VIDEO |
| 1616 | pjmedia_vid_dev_fullscreen_flag mode; |
| 1617 | mode = enabled? PJMEDIA_VID_DEV_FULLSCREEN : PJMEDIA_VID_DEV_WINDOWED; |
| 1618 | PJSUA2_CHECK_EXPR( pjsua_vid_win_set_fullscreen(winId, mode) ); |
| 1619 | #else |
| 1620 | PJ_UNUSED_ARG(enabled); |
| 1621 | #endif |
| 1622 | } |
| 1623 | |
| 1624 | void VideoWindow::setFullScreen2(pjmedia_vid_dev_fullscreen_flag mode) |
| 1625 | PJSUA2_THROW(Error) |
| 1626 | { |
| 1627 | #if PJSUA_HAS_VIDEO |
| 1628 | PJSUA2_CHECK_EXPR( pjsua_vid_win_set_fullscreen(winId, mode) ); |
| 1629 | #else |
| 1630 | PJ_UNUSED_ARG(mode); |
| 1631 | #endif |
| 1632 | } |
| 1633 | |
| 1634 | /////////////////////////////////////////////////////////////////////////////// |
| 1635 | VideoPreviewOpParam::VideoPreviewOpParam() |
| 1636 | { |
| 1637 | #if PJSUA_HAS_VIDEO |
| 1638 | pjsua_vid_preview_param vid_prev_param; |
| 1639 | |
| 1640 | pjsua_vid_preview_param_default(&vid_prev_param); |
| 1641 | fromPj(vid_prev_param); |
| 1642 | #endif |
| 1643 | } |
| 1644 | |
| 1645 | void VideoPreviewOpParam::fromPj(const pjsua_vid_preview_param &prm) |
| 1646 | { |
| 1647 | #if PJSUA_HAS_VIDEO |
| 1648 | this->rendId = prm.rend_id; |
| 1649 | this->show = PJ2BOOL(prm.show); |
| 1650 | this->windowFlags = prm.wnd_flags; |
| 1651 | this->window.type = prm.wnd.type; |
| 1652 | this->window.handle.window = prm.wnd.info.window; |
| 1653 | this->format.fromPj(prm.format); |
| 1654 | #else |
| 1655 | PJ_UNUSED_ARG(prm); |
| 1656 | #endif |
| 1657 | } |
| 1658 | |
| 1659 | pjsua_vid_preview_param VideoPreviewOpParam::toPj() const |
| 1660 | { |
| 1661 | pjsua_vid_preview_param param; |
| 1662 | pj_bzero(¶m, sizeof(param)); |
| 1663 | #if PJSUA_HAS_VIDEO |
| 1664 | param.rend_id = this->rendId; |
| 1665 | param.show = this->show; |
| 1666 | param.wnd_flags = this->windowFlags; |
| 1667 | param.wnd.type = this->window.type; |
| 1668 | param.wnd.info.window = this->window.handle.window; |
| 1669 | param.format = this->format.toPj(); |
| 1670 | #endif |
| 1671 | return param; |
| 1672 | } |
| 1673 | |
| 1674 | VideoPreview::VideoPreview(int dev_id) |
| 1675 | : devId(dev_id), winId(PJSUA_INVALID_ID) |
| 1676 | { |
| 1677 | #if !PJSUA_HAS_VIDEO |
| 1678 | /* Suppress warning of unused field when video is disabled */ |
| 1679 | PJ_UNUSED_ARG(winId); |
| 1680 | #endif |
| 1681 | } |
| 1682 | |
| 1683 | bool VideoPreview::hasNative() |
| 1684 | { |
| 1685 | #if PJSUA_HAS_VIDEO |
| 1686 | return(PJ2BOOL(pjsua_vid_preview_has_native(devId))); |
| 1687 | #else |
| 1688 | return false; |
| 1689 | #endif |
| 1690 | } |
| 1691 | |
| 1692 | void VideoPreview::start(const VideoPreviewOpParam ¶m) PJSUA2_THROW(Error) |
| 1693 | { |
| 1694 | #if PJSUA_HAS_VIDEO |
| 1695 | pjsua_vid_preview_param prm = param.toPj(); |
| 1696 | PJSUA2_CHECK_EXPR(pjsua_vid_preview_start(devId, &prm)); |
| 1697 | |
| 1698 | /* Device may be fast-switched and VideoPreview will not aware of that, |
| 1699 | * so better keep win ID too. |
| 1700 | */ |
| 1701 | winId = pjsua_vid_preview_get_win(devId); |
| 1702 | #else |
| 1703 | PJ_UNUSED_ARG(param); |
| 1704 | PJ_UNUSED_ARG(devId); |
| 1705 | #endif |
| 1706 | } |
| 1707 | |
| 1708 | void VideoPreview::stop() PJSUA2_THROW(Error) |
| 1709 | { |
| 1710 | #if PJSUA_HAS_VIDEO |
| 1711 | updateDevId(); |
| 1712 | PJSUA2_CHECK_EXPR(pjsua_vid_preview_stop(devId)); |
| 1713 | #endif |
| 1714 | } |
| 1715 | |
| 1716 | VideoWindow VideoPreview::getVideoWindow() |
| 1717 | { |
| 1718 | #if PJSUA_HAS_VIDEO |
| 1719 | return (VideoWindow(winId)); |
| 1720 | #else |
| 1721 | return (VideoWindow(PJSUA_INVALID_ID)); |
| 1722 | #endif |
| 1723 | } |
| 1724 | |
| 1725 | VideoMedia VideoPreview::getVideoMedia() PJSUA2_THROW(Error) |
| 1726 | { |
| 1727 | #if PJSUA_HAS_VIDEO |
| 1728 | updateDevId(); |
| 1729 | pjsua_conf_port_id id = pjsua_vid_preview_get_vid_conf_port(devId); |
| 1730 | if (id != PJSUA_INVALID_ID) { |
| 1731 | VideoMediaHelper vm; |
| 1732 | vm.setPortId(id); |
| 1733 | return vm; |
| 1734 | } else { |
| 1735 | PJSUA2_RAISE_ERROR2(PJ_EINVALIDOP, "Preview not started"); |
| 1736 | } |
| 1737 | #else |
| 1738 | PJSUA2_RAISE_ERROR(PJ_EINVALIDOP); |
| 1739 | #endif |
| 1740 | } |
| 1741 | |
| 1742 | /* Device may be fastswitched and VideoPreview will not aware of that, |
| 1743 | * this function will update the VideoPreview device ID. |
| 1744 | */ |
| 1745 | void VideoPreview::updateDevId() |
| 1746 | { |
| 1747 | #if PJSUA_HAS_VIDEO |
| 1748 | if (winId != PJSUA_INVALID_ID) { |
| 1749 | PJSUA_LOCK(); |
| 1750 | pjsua_vid_win *w = &pjsua_var.win[winId]; |
| 1751 | pj_assert(w->type == PJSUA_WND_TYPE_PREVIEW); |
| 1752 | devId = w->preview_cap_id; |
| 1753 | PJSUA_UNLOCK(); |
| 1754 | } |
| 1755 | #endif |
| 1756 | } |
| 1757 | |
| 1758 | /////////////////////////////////////////////////////////////////////////////// |
| 1759 | void MediaFormatVideo::fromPj(const pjmedia_format &format) |
| 1760 | { |
| 1761 | #if PJSUA_HAS_VIDEO |
| 1762 | if ((format.type != PJMEDIA_TYPE_VIDEO) && |
| 1763 | (format.detail_type != PJMEDIA_FORMAT_DETAIL_VIDEO)) |
| 1764 | { |
| 1765 | type = PJMEDIA_TYPE_UNKNOWN; |
| 1766 | return; |
| 1767 | } |
| 1768 | |
| 1769 | id = format.id; |
| 1770 | type = format.type; |
| 1771 | |
| 1772 | /* Detail. */ |
| 1773 | width = format.det.vid.size.w; |
| 1774 | height = format.det.vid.size.h; |
| 1775 | fpsNum = format.det.vid.fps.num; |
| 1776 | fpsDenum = format.det.vid.fps.denum; |
| 1777 | avgBps = format.det.vid.avg_bps; |
| 1778 | maxBps = format.det.vid.max_bps; |
| 1779 | #else |
| 1780 | PJ_UNUSED_ARG(format); |
| 1781 | type = PJMEDIA_TYPE_UNKNOWN; |
| 1782 | #endif |
| 1783 | } |
| 1784 | |
| 1785 | pjmedia_format MediaFormatVideo::toPj() const |
| 1786 | { |
| 1787 | pjmedia_format pj_format; |
| 1788 | |
| 1789 | #if PJSUA_HAS_VIDEO |
| 1790 | pj_format.id = id; |
| 1791 | pj_format.type = type; |
| 1792 | |
| 1793 | pj_format.detail_type = PJMEDIA_FORMAT_DETAIL_VIDEO; |
| 1794 | pj_format.det.vid.size.w = width; |
| 1795 | pj_format.det.vid.size.h = height; |
| 1796 | pj_format.det.vid.fps.num = fpsNum; |
| 1797 | pj_format.det.vid.fps.denum = fpsDenum; |
| 1798 | pj_format.det.vid.avg_bps = avgBps; |
| 1799 | pj_format.det.vid.max_bps = maxBps; |
| 1800 | #else |
| 1801 | pj_format.type = PJMEDIA_TYPE_UNKNOWN; |
| 1802 | #endif |
| 1803 | return pj_format; |
| 1804 | } |
| 1805 | |
| 1806 | void MediaFormatVideo::init(pj_uint32_t formatId, |
| 1807 | unsigned width_, unsigned height_, |
| 1808 | int fpsNum_, int fpsDenum_, |
| 1809 | pj_uint32_t avgBps_, pj_uint32_t maxBps_) |
| 1810 | { |
| 1811 | #if PJSUA_HAS_VIDEO |
| 1812 | type = PJMEDIA_TYPE_VIDEO; |
| 1813 | id = formatId; |
| 1814 | |
| 1815 | this->width = width_; |
| 1816 | this->height = height_; |
| 1817 | this->fpsNum = fpsNum_; |
| 1818 | this->fpsDenum = fpsDenum_; |
| 1819 | this->avgBps = avgBps_; |
| 1820 | this->maxBps = maxBps_; |
| 1821 | #else |
| 1822 | PJ_UNUSED_ARG(formatId); |
| 1823 | PJ_UNUSED_ARG(width_); |
| 1824 | PJ_UNUSED_ARG(height_); |
| 1825 | PJ_UNUSED_ARG(fpsNum_); |
| 1826 | PJ_UNUSED_ARG(fpsDenum_); |
| 1827 | PJ_UNUSED_ARG(avgBps_); |
| 1828 | PJ_UNUSED_ARG(maxBps_); |
| 1829 | type = PJMEDIA_TYPE_UNKNOWN; |
| 1830 | #endif |
| 1831 | } |
| 1832 | |
| 1833 | |
| 1834 | /////////////////////////////////////////////////////////////////////////////// |
| 1835 | void VideoDevInfo::fromPj(const pjmedia_vid_dev_info &dev_info) |
| 1836 | { |
| 1837 | #if PJSUA_HAS_VIDEO |
| 1838 | id = dev_info.id; |
| 1839 | name = dev_info.name; |
| 1840 | driver = dev_info.driver; |
| 1841 | dir = dev_info.dir; |
| 1842 | caps = dev_info.caps; |
| 1843 | |
| 1844 | for (unsigned i = 0; i<dev_info.fmt_cnt;++i) { |
| 1845 | MediaFormatVideo format; |
| 1846 | format.fromPj(dev_info.fmt[i]); |
| 1847 | if (format.type == PJMEDIA_TYPE_VIDEO) |
| 1848 | fmt.push_back(PJSUA2_MOVE(format)); |
| 1849 | } |
| 1850 | #else |
| 1851 | PJ_UNUSED_ARG(dev_info); |
| 1852 | #endif |
| 1853 | } |
| 1854 | |
| 1855 | VideoDevInfo::~VideoDevInfo() |
| 1856 | { |
| 1857 | } |
| 1858 | |
| 1859 | /////////////////////////////////////////////////////////////////////////////// |
| 1860 | void VidDevManager::initSubsys() PJSUA2_THROW(Error) |
| 1861 | { |
| 1862 | #if PJSUA_HAS_VIDEO && PJSUA_DONT_INIT_VID_DEV_SUBSYS |
| 1863 | /* Should init once only! */ |
| 1864 | if (pjmedia_get_vid_subsys()->init_count > 0) |
| 1865 | return; |
| 1866 | |
| 1867 | PJSUA2_CHECK_EXPR(pjmedia_vid_dev_subsys_init(pjsua_get_pool_factory())); |
| 1868 | #endif |
| 1869 | } |
| 1870 | |
| 1871 | void VidDevManager::refreshDevs() PJSUA2_THROW(Error) |
| 1872 | { |
| 1873 | #if PJSUA_HAS_VIDEO |
| 1874 | PJSUA2_CHECK_EXPR(pjmedia_vid_dev_refresh()); |
| 1875 | #endif |
| 1876 | } |
| 1877 | |
| 1878 | unsigned VidDevManager::getDevCount() |
| 1879 | { |
| 1880 | #if PJSUA_HAS_VIDEO |
| 1881 | return pjsua_vid_dev_count(); |
| 1882 | #else |
| 1883 | return 0; |
| 1884 | #endif |
| 1885 | } |
| 1886 | |
| 1887 | VideoDevInfo VidDevManager::getDevInfo(int dev_id) const PJSUA2_THROW(Error) |
| 1888 | { |
| 1889 | VideoDevInfo dev_info; |
| 1890 | #if PJSUA_HAS_VIDEO |
| 1891 | pjmedia_vid_dev_info pj_info; |
| 1892 | |
| 1893 | PJSUA2_CHECK_EXPR(pjsua_vid_dev_get_info(dev_id, &pj_info)); |
| 1894 | |
| 1895 | dev_info.fromPj(pj_info); |
| 1896 | #else |
| 1897 | PJ_UNUSED_ARG(dev_id); |
| 1898 | #endif |
| 1899 | return dev_info; |
| 1900 | } |
| 1901 | |
| 1902 | #if !DEPRECATED_FOR_TICKET_2232 |
| 1903 | const VideoDevInfoVector &VidDevManager::enumDev() PJSUA2_THROW(Error) |
| 1904 | { |
| 1905 | #if PJSUA_HAS_VIDEO |
| 1906 | pjmedia_vid_dev_info pj_info[MAX_DEV_COUNT]; |
| 1907 | unsigned count = MAX_DEV_COUNT; |
| 1908 | |
| 1909 | PJSUA2_CHECK_EXPR(pjsua_vid_enum_devs(pj_info, &count)); |
| 1910 | |
| 1911 | pj_enter_critical_section(); |
| 1912 | clearVideoDevList(); |
| 1913 | for (unsigned i = 0; i<count;++i) { |
| 1914 | VideoDevInfo *dev_info = new VideoDevInfo; |
| 1915 | dev_info->fromPj(pj_info[i]); |
| 1916 | videoDevList.push_back(dev_info); |
| 1917 | } |
| 1918 | pj_leave_critical_section(); |
| 1919 | #endif |
| 1920 | return videoDevList; |
| 1921 | } |
| 1922 | #endif |
| 1923 | |
| 1924 | VideoDevInfoVector2 VidDevManager::enumDev2() const PJSUA2_THROW(Error) |
| 1925 | { |
| 1926 | VideoDevInfoVector2 vdiv2; |
| 1927 | #if PJSUA_HAS_VIDEO |
| 1928 | pjmedia_vid_dev_info pj_info[MAX_DEV_COUNT]; |
| 1929 | unsigned count = MAX_DEV_COUNT; |
| 1930 | |
| 1931 | PJSUA2_CHECK_EXPR(pjsua_vid_enum_devs(pj_info, &count)); |
| 1932 | |
| 1933 | for (unsigned i = 0; i<count;++i) { |
| 1934 | VideoDevInfo vdi; |
| 1935 | vdi.fromPj(pj_info[i]); |
| 1936 | vdiv2.push_back(PJSUA2_MOVE(vdi)); |
| 1937 | } |
| 1938 | #endif |
| 1939 | return vdiv2; |
| 1940 | } |
| 1941 | |
| 1942 | int VidDevManager::lookupDev(const string &drv_name, |
| 1943 | const string &dev_name) const PJSUA2_THROW(Error) |
| 1944 | { |
| 1945 | pjmedia_vid_dev_index pj_idx = 0; |
| 1946 | #if PJSUA_HAS_VIDEO |
| 1947 | PJSUA2_CHECK_EXPR(pjmedia_vid_dev_lookup(drv_name.c_str(), |
| 1948 | dev_name.c_str(), |
| 1949 | &pj_idx)); |
| 1950 | #else |
| 1951 | PJ_UNUSED_ARG(drv_name); |
| 1952 | PJ_UNUSED_ARG(dev_name); |
| 1953 | #endif |
| 1954 | return pj_idx; |
| 1955 | } |
| 1956 | |
| 1957 | string VidDevManager::capName(pjmedia_vid_dev_cap cap) const |
| 1958 | { |
| 1959 | string cap_name; |
| 1960 | #if PJSUA_HAS_VIDEO |
| 1961 | cap_name = pjmedia_vid_dev_cap_name(cap, NULL); |
| 1962 | #else |
| 1963 | PJ_UNUSED_ARG(cap); |
| 1964 | #endif |
| 1965 | return cap_name; |
| 1966 | } |
| 1967 | |
| 1968 | void VidDevManager::setFormat(int dev_id, |
| 1969 | const MediaFormatVideo &format, |
| 1970 | bool keep) PJSUA2_THROW(Error) |
| 1971 | { |
| 1972 | #if PJSUA_HAS_VIDEO |
| 1973 | pjmedia_format pj_format = format.toPj(); |
| 1974 | |
| 1975 | PJSUA2_CHECK_EXPR(pjsua_vid_dev_set_setting(dev_id, |
| 1976 | PJMEDIA_VID_DEV_CAP_FORMAT, |
| 1977 | &pj_format, |
| 1978 | keep)); |
| 1979 | #else |
| 1980 | PJ_UNUSED_ARG(dev_id); |
| 1981 | PJ_UNUSED_ARG(format); |
| 1982 | PJ_UNUSED_ARG(keep); |
| 1983 | #endif |
| 1984 | } |
| 1985 | |
| 1986 | MediaFormatVideo VidDevManager::getFormat(int dev_id) const |
| 1987 | PJSUA2_THROW(Error) |
| 1988 | { |
| 1989 | MediaFormatVideo vid_format; |
| 1990 | pj_bzero(&vid_format, sizeof(vid_format)); |
| 1991 | #if PJSUA_HAS_VIDEO |
| 1992 | pjmedia_format pj_format; |
| 1993 | PJSUA2_CHECK_EXPR(pjsua_vid_dev_get_setting(dev_id, |
| 1994 | PJMEDIA_VID_DEV_CAP_FORMAT, |
| 1995 | &pj_format)); |
| 1996 | vid_format.fromPj(pj_format); |
| 1997 | #else |
| 1998 | PJ_UNUSED_ARG(dev_id); |
| 1999 | #endif |
| 2000 | return vid_format; |
| 2001 | } |
| 2002 | |
| 2003 | void VidDevManager::setInputScale(int dev_id, |
| 2004 | const MediaSize &scale, |
| 2005 | bool keep) PJSUA2_THROW(Error) |
| 2006 | { |
| 2007 | #if PJSUA_HAS_VIDEO |
| 2008 | pjmedia_rect_size pj_size; |
| 2009 | pj_size.w = scale.w; |
| 2010 | pj_size.h = scale.h; |
| 2011 | PJSUA2_CHECK_EXPR(pjsua_vid_dev_set_setting(dev_id, |
| 2012 | PJMEDIA_VID_DEV_CAP_INPUT_SCALE, |
| 2013 | &pj_size, |
| 2014 | keep)); |
| 2015 | #else |
| 2016 | PJ_UNUSED_ARG(dev_id); |
| 2017 | PJ_UNUSED_ARG(scale); |
| 2018 | PJ_UNUSED_ARG(keep); |
| 2019 | #endif |
| 2020 | } |
| 2021 | |
| 2022 | MediaSize VidDevManager::getInputScale(int dev_id) const PJSUA2_THROW(Error) |
| 2023 | { |
| 2024 | MediaSize scale; |
| 2025 | pj_bzero(&scale, sizeof(scale)); |
| 2026 | #if PJSUA_HAS_VIDEO |
| 2027 | pjmedia_rect_size pj_size; |
| 2028 | PJSUA2_CHECK_EXPR(pjsua_vid_dev_get_setting(dev_id, |
| 2029 | PJMEDIA_VID_DEV_CAP_INPUT_SCALE, |
| 2030 | &pj_size)); |
| 2031 | |
| 2032 | scale.w = pj_size.w; |
| 2033 | scale.h = pj_size.h; |
| 2034 | #else |
| 2035 | PJ_UNUSED_ARG(dev_id); |
| 2036 | #endif |
| 2037 | return scale; |
| 2038 | } |
| 2039 | |
| 2040 | void VidDevManager::setOutputWindowFlags(int dev_id, |
| 2041 | int flags, |
| 2042 | bool keep) PJSUA2_THROW(Error) |
| 2043 | { |
| 2044 | #if PJSUA_HAS_VIDEO |
| 2045 | PJSUA2_CHECK_EXPR(pjsua_vid_dev_set_setting(dev_id, |
| 2046 | PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW_FLAGS, |
| 2047 | &flags, |
| 2048 | keep)); |
| 2049 | #else |
| 2050 | PJ_UNUSED_ARG(dev_id); |
| 2051 | PJ_UNUSED_ARG(flags); |
| 2052 | PJ_UNUSED_ARG(keep); |
| 2053 | #endif |
| 2054 | } |
| 2055 | |
| 2056 | int VidDevManager::getOutputWindowFlags(int dev_id) PJSUA2_THROW(Error) |
| 2057 | { |
| 2058 | int flags = 0; |
| 2059 | |
| 2060 | #if PJSUA_HAS_VIDEO |
| 2061 | PJSUA2_CHECK_EXPR(pjsua_vid_dev_get_setting(dev_id, |
| 2062 | PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW_FLAGS, |
| 2063 | &flags)); |
| 2064 | #else |
| 2065 | PJ_UNUSED_ARG(dev_id); |
| 2066 | #endif |
| 2067 | return flags; |
| 2068 | } |
| 2069 | |
| 2070 | void VidDevManager::switchDev(int dev_id, |
| 2071 | const VideoSwitchParam ¶m) |
| 2072 | PJSUA2_THROW(Error) |
| 2073 | { |
| 2074 | #if PJSUA_HAS_VIDEO |
| 2075 | pjmedia_vid_dev_switch_param pj_param; |
| 2076 | pj_param.target_id = param.target_id; |
| 2077 | PJSUA2_CHECK_EXPR(pjsua_vid_dev_set_setting(dev_id, |
| 2078 | PJMEDIA_VID_DEV_CAP_SWITCH, |
| 2079 | &pj_param, |
| 2080 | PJ_FALSE)); |
| 2081 | #else |
| 2082 | PJ_UNUSED_ARG(dev_id); |
| 2083 | PJ_UNUSED_ARG(param); |
| 2084 | #endif |
| 2085 | } |
| 2086 | |
| 2087 | void VidDevManager::clearVideoDevList() |
| 2088 | { |
| 2089 | #if PJSUA_HAS_VIDEO && !DEPRECATED_FOR_TICKET_2232 |
| 2090 | for (unsigned i = 0;i<videoDevList.size();++i) { |
| 2091 | delete videoDevList[i]; |
| 2092 | } |
| 2093 | videoDevList.clear(); |
| 2094 | #endif |
| 2095 | } |
| 2096 | |
| 2097 | bool VidDevManager::isCaptureActive(int dev_id) const |
| 2098 | { |
| 2099 | #if PJSUA_HAS_VIDEO |
| 2100 | return (pjsua_vid_dev_is_active(dev_id) == PJ_TRUE? true: false); |
| 2101 | #else |
| 2102 | PJ_UNUSED_ARG(dev_id); |
| 2103 | |
| 2104 | return false; |
| 2105 | #endif |
| 2106 | } |
| 2107 | |
| 2108 | void VidDevManager::setCaptureOrient(pjmedia_vid_dev_index dev_id, |
| 2109 | pjmedia_orient orient, |
| 2110 | bool keep) PJSUA2_THROW(Error) |
| 2111 | { |
| 2112 | #if PJSUA_HAS_VIDEO |
| 2113 | PJSUA2_CHECK_EXPR(pjsua_vid_dev_set_setting(dev_id, |
| 2114 | PJMEDIA_VID_DEV_CAP_ORIENTATION, &orient, keep)); |
| 2115 | #else |
| 2116 | PJ_UNUSED_ARG(dev_id); |
| 2117 | PJ_UNUSED_ARG(orient); |
| 2118 | PJ_UNUSED_ARG(keep); |
| 2119 | #endif |
| 2120 | } |
| 2121 | |
| 2122 | VidDevManager::VidDevManager() |
| 2123 | { |
| 2124 | } |
| 2125 | |
| 2126 | VidDevManager::~VidDevManager() |
| 2127 | { |
| 2128 | #if PJSUA_HAS_VIDEO |
| 2129 | clearVideoDevList(); |
| 2130 | #endif |
| 2131 | } |
| 2132 | /////////////////////////////////////////////////////////////////////////////// |
| 2133 | VideoPlayer::VideoPlayer() |
| 2134 | : playerId(PJSUA_INVALID_ID) |
| 2135 | { |
| 2136 | #if !PJSUA_HAS_VIDEO |
| 2137 | PJ_UNUSED_ARG(playerId); |
| 2138 | #endif |
| 2139 | } |
| 2140 | |
| 2141 | VideoPlayer::~VideoPlayer() |
| 2142 | { |
| 2143 | #if PJSUA_HAS_VIDEO |
| 2144 | if (playerId != PJSUA_INVALID_ID) { |
| 2145 | pjsua_avi_player_destroy(playerId); |
| 2146 | } |
| 2147 | #endif |
| 2148 | } |
| 2149 | |
| 2150 | void VideoPlayer::createVideoPlayer(const string& file_name) PJSUA2_THROW(Error) |
| 2151 | { |
| 2152 | #if PJSUA_HAS_VIDEO |
| 2153 | if (playerId != PJSUA_INVALID_ID) { |
| 2154 | PJSUA2_RAISE_ERROR(PJ_EEXISTS); |
| 2155 | } |
| 2156 | pj_str_t pj_name = str2Pj(file_name); |
| 2157 | |
| 2158 | PJSUA2_CHECK_EXPR(pjsua_avi_player_create(&pj_name, &playerId)); |
| 2159 | #else |
| 2160 | PJ_UNUSED_ARG(file_name); |
| 2161 | PJSUA2_RAISE_ERROR(PJ_EINVALIDOP); |
| 2162 | #endif |
| 2163 | } |
| 2164 | |
| 2165 | AudioMediaVector2 VideoPlayer::mediaEnumPorts() PJSUA2_THROW(Error) |
| 2166 | { |
| 2167 | #if PJSUA_HAS_VIDEO |
| 2168 | AudioMediaVector2 amv2; |
| 2169 | unsigned i, count; |
| 2170 | |
| 2171 | count = pjsua_avi_player_get_num_stream(playerId, PJMEDIA_TYPE_AUDIO); |
| 2172 | |
| 2173 | for (i = 0; i < count; ++i) { |
| 2174 | AudioMediaHelper am; |
| 2175 | |
| 2176 | pjsua_conf_port_id port_id = pjsua_avi_player_get_conf_port(playerId, |
| 2177 | PJMEDIA_TYPE_AUDIO, i); |
| 2178 | if (port_id == PJSUA_INVALID_ID) |
| 2179 | continue; |
| 2180 | |
| 2181 | am.setPortId(port_id); |
| 2182 | amv2.push_back(am); |
| 2183 | } |
| 2184 | if (amv2.size() == 0) |
| 2185 | PJSUA2_RAISE_ERROR(PJ_ENOTFOUND); |
| 2186 | |
| 2187 | return amv2; |
| 2188 | #else |
| 2189 | PJSUA2_RAISE_ERROR(PJ_EINVALIDOP); |
| 2190 | #endif |
| 2191 | } |
| 2192 | |
| 2193 | VideoMediaVector VideoPlayer::mediaEnumVidPorts() PJSUA2_THROW(Error) |
| 2194 | { |
| 2195 | #if PJSUA_HAS_VIDEO |
| 2196 | VideoMediaVector vmv; |
| 2197 | unsigned i, count; |
| 2198 | |
| 2199 | count = pjsua_avi_player_get_num_stream(playerId, PJMEDIA_TYPE_VIDEO); |
| 2200 | |
| 2201 | for (i = 0; i < count; ++i) { |
| 2202 | VideoMediaHelper vm; |
| 2203 | |
| 2204 | pjsua_conf_port_id port_id = pjsua_avi_player_get_conf_port(playerId, |
| 2205 | PJMEDIA_TYPE_VIDEO, i); |
| 2206 | if (port_id == PJSUA_INVALID_ID) |
| 2207 | continue; |
| 2208 | |
| 2209 | vm.setPortId(port_id); |
| 2210 | vmv.push_back(vm); |
| 2211 | } |
| 2212 | if (vmv.size() == 0) |
| 2213 | PJSUA2_RAISE_ERROR(PJ_ENOTFOUND); |
| 2214 | |
| 2215 | return vmv; |
| 2216 | |
| 2217 | #else |
| 2218 | PJSUA2_RAISE_ERROR(PJ_EINVALIDOP); |
| 2219 | #endif |
| 2220 | } |
| 2221 | |
| 2222 | int VideoPlayer::getVideoDevId() PJSUA2_THROW(Error) |
| 2223 | { |
| 2224 | #if PJSUA_HAS_VIDEO |
| 2225 | pjmedia_vid_dev_index vid_idx = pjsua_avi_player_get_vid_dev(playerId); |
| 2226 | if (vid_idx == PJMEDIA_VID_INVALID_DEV) |
| 2227 | PJSUA2_RAISE_ERROR(PJ_ENOTFOUND); |
| 2228 | |
| 2229 | return vid_idx; |
| 2230 | #else |
| 2231 | PJSUA2_RAISE_ERROR(PJ_EINVALIDOP); |
| 2232 | #endif |
| 2233 | } |
| 2234 | |
| 2235 | /////////////////////////////////////////////////////////////////////////////// |
| 2236 | VideoRecorder::VideoRecorder() |
| 2237 | : recorderId(PJSUA_INVALID_ID) |
| 2238 | { |
| 2239 | |
| 2240 | } |
| 2241 | |
| 2242 | VideoRecorder::~VideoRecorder() |
| 2243 | { |
| 2244 | #if PJSUA_HAS_VIDEO |
| 2245 | if (recorderId != PJSUA_INVALID_ID) { |
| 2246 | pjsua_avi_recorder_destroy(recorderId); |
| 2247 | } |
| 2248 | #endif |
| 2249 | } |
| 2250 | |
| 2251 | void VideoRecorder::createVideoRecorder(const string& file_name, |
| 2252 | long max_size, |
| 2253 | MediaFormatVideo *vid_fmt, |
| 2254 | MediaFormatAudio *aud_fmt, |
| 2255 | unsigned options) PJSUA2_THROW(Error) |
| 2256 | { |
| 2257 | #if PJSUA_HAS_VIDEO |
| 2258 | if (recorderId != PJSUA_INVALID_ID) { |
| 2259 | PJSUA2_RAISE_ERROR(PJ_EEXISTS); |
| 2260 | } |
| 2261 | pjmedia_format vidfmt; |
| 2262 | pjmedia_format audfmt; |
| 2263 | |
| 2264 | pj_str_t pj_name = str2Pj(file_name); |
| 2265 | if (vid_fmt) |
| 2266 | vidfmt = vid_fmt->toPj(); |
| 2267 | if (aud_fmt) |
| 2268 | audfmt = aud_fmt->toPj(); |
| 2269 | |
| 2270 | PJSUA2_CHECK_EXPR(pjsua_avi_recorder_create(&pj_name, |
| 2271 | max_size, |
| 2272 | vid_fmt?&vidfmt:NULL, |
| 2273 | aud_fmt?&audfmt:NULL, |
| 2274 | options, |
| 2275 | &recorderId)); |
| 2276 | |
| 2277 | pjsua_avi_recorder_set_cb(recorderId, this, &max_size_cb); |
| 2278 | #else |
| 2279 | PJ_UNUSED_ARG(file_name); |
| 2280 | PJ_UNUSED_ARG(max_size); |
| 2281 | PJ_UNUSED_ARG(vid_fmt); |
| 2282 | PJ_UNUSED_ARG(aud_fmt); |
| 2283 | PJ_UNUSED_ARG(options); |
| 2284 | PJ_UNUSED_ARG(recorderId); |
| 2285 | PJSUA2_RAISE_ERROR(PJ_EINVALIDOP); |
| 2286 | #endif |
| 2287 | } |
| 2288 | |
| 2289 | VideoMedia VideoRecorder::getVideoMedia() PJSUA2_THROW(Error) |
| 2290 | { |
| 2291 | #if PJSUA_HAS_VIDEO |
| 2292 | pjsua_conf_port_id port_id = pjsua_avi_recorder_get_conf_port(recorderId, |
| 2293 | PJMEDIA_TYPE_VIDEO); |
| 2294 | |
| 2295 | if (port_id == PJSUA_INVALID_ID) |
| 2296 | PJSUA2_RAISE_ERROR(PJ_ENOTFOUND); |
| 2297 | |
| 2298 | VideoMediaHelper vm; |
| 2299 | vm.setPortId(port_id); |
| 2300 | |
| 2301 | return vm; |
| 2302 | #else |
| 2303 | PJSUA2_RAISE_ERROR(PJ_EINVALIDOP); |
| 2304 | #endif |
| 2305 | } |
| 2306 | |
| 2307 | AudioMedia VideoRecorder::getAudioMedia() PJSUA2_THROW(Error) |
| 2308 | { |
| 2309 | #if PJSUA_HAS_VIDEO |
| 2310 | pjsua_conf_port_id port_id = pjsua_avi_recorder_get_conf_port(recorderId, |
| 2311 | PJMEDIA_TYPE_AUDIO); |
| 2312 | |
| 2313 | if (port_id == PJSUA_INVALID_ID) |
| 2314 | PJSUA2_RAISE_ERROR(PJ_ENOTFOUND); |
| 2315 | |
| 2316 | AudioMediaHelper vm; |
| 2317 | vm.setPortId(port_id); |
| 2318 | |
| 2319 | return vm; |
| 2320 | #else |
| 2321 | PJSUA2_RAISE_ERROR(PJ_EINVALIDOP); |
| 2322 | #endif |
| 2323 | } |
| 2324 | |
| 2325 | void VideoRecorder::max_size_cb(pjsua_recorder_id id, void *usr_data) |
| 2326 | { |
| 2327 | PJ_UNUSED_ARG(id); |
| 2328 | |
| 2329 | VideoRecorder *vid_rec = (VideoRecorder*)usr_data; |
| 2330 | vid_rec->onMaxSize(); |
| 2331 | } |
| 2332 | |
| 2333 | /////////////////////////////////////////////////////////////////////////////// |
| 2334 | |
| 2335 | /** |
| 2336 | * Utility class for converting CodecFmtpVector to and from pjmedia_codec_fmtp. |
| 2337 | */ |
| 2338 | class CodecFmtpUtil |
| 2339 | { |
| 2340 | public: |
| 2341 | static void fromPj(const pjmedia_codec_fmtp &in_fmtp, |
| 2342 | CodecFmtpVector &out_fmtp) |
| 2343 | { |
| 2344 | unsigned i = 0; |
| 2345 | for (; i<in_fmtp.cnt; ++i) { |
| 2346 | CodecFmtp fmtp; |
| 2347 | fmtp.name = pj2Str(in_fmtp.param[i].name); |
| 2348 | fmtp.val = pj2Str(in_fmtp.param[i].val); |
| 2349 | |
| 2350 | out_fmtp.push_back(PJSUA2_MOVE(fmtp)); |
| 2351 | } |
| 2352 | } |
| 2353 | |
| 2354 | static void toPj(const CodecFmtpVector &in_fmtp, |
| 2355 | pjmedia_codec_fmtp &out_fmtp) |
| 2356 | { |
| 2357 | CodecFmtpVector::const_iterator i; |
| 2358 | out_fmtp.cnt = 0; |
| 2359 | for (i = in_fmtp.begin(); i != in_fmtp.end(); ++i) { |
| 2360 | if (out_fmtp.cnt >= PJMEDIA_CODEC_MAX_FMTP_CNT) { |
| 2361 | break; |
| 2362 | } |
| 2363 | out_fmtp.param[out_fmtp.cnt].name = str2Pj((*i).name); |
| 2364 | out_fmtp.param[out_fmtp.cnt].val = str2Pj((*i).val); |
| 2365 | ++out_fmtp.cnt; |
| 2366 | } |
| 2367 | } |
| 2368 | }; |
| 2369 | |
| 2370 | /////////////////////////////////////////////////////////////////////////////// |
| 2371 | void CodecInfo::fromPj(const pjsua_codec_info &codec_info) |
| 2372 | { |
| 2373 | codecId = pj2Str(codec_info.codec_id); |
| 2374 | priority = codec_info.priority; |
| 2375 | desc = pj2Str(codec_info.desc); |
| 2376 | } |
| 2377 | |
| 2378 | /////////////////////////////////////////////////////////////////////////////// |
| 2379 | void CodecParam::fromPj(const pjmedia_codec_param ¶m) |
| 2380 | { |
| 2381 | /* info part. */ |
| 2382 | info.clockRate = param.info.clock_rate; |
| 2383 | info.channelCnt = param.info.channel_cnt; |
| 2384 | info.avgBps = param.info.avg_bps; |
| 2385 | info.maxBps = param.info.max_bps; |
| 2386 | info.maxRxFrameSize = param.info.max_rx_frame_size; |
| 2387 | info.frameLen = param.info.frm_ptime; |
| 2388 | info.frameLenDenum = param.info.frm_ptime_denum; |
| 2389 | info.encFrameLen = param.info.enc_ptime; |
| 2390 | info.encFrameLenDenum = param.info.enc_ptime_denum; |
| 2391 | info.pcmBitsPerSample = param.info.pcm_bits_per_sample; |
| 2392 | info.pt = param.info.pt; |
| 2393 | info.fmtId = param.info.fmt_id; |
| 2394 | |
| 2395 | /* setting part. */ |
| 2396 | setting.frmPerPkt = param.setting.frm_per_pkt; |
| 2397 | setting.vad = param.setting.vad; |
| 2398 | setting.cng = param.setting.cng; |
| 2399 | setting.penh = param.setting.penh; |
| 2400 | setting.plc = param.setting.plc; |
| 2401 | setting.reserved = param.setting.reserved; |
| 2402 | CodecFmtpUtil::fromPj(param.setting.enc_fmtp, setting.encFmtp); |
| 2403 | CodecFmtpUtil::fromPj(param.setting.dec_fmtp, setting.decFmtp); |
| 2404 | setting.packetLoss = param.setting.packet_loss; |
| 2405 | setting.complexity = param.setting.complexity; |
| 2406 | setting.cbr = PJ2BOOL(param.setting.cbr); |
| 2407 | } |
| 2408 | |
| 2409 | pjmedia_codec_param CodecParam::toPj() const |
| 2410 | { |
| 2411 | pjmedia_codec_param param; |
| 2412 | |
| 2413 | /* info part. */ |
| 2414 | param.info.clock_rate = info.clockRate; |
| 2415 | param.info.channel_cnt = info.channelCnt; |
| 2416 | param.info.avg_bps = (pj_uint32_t)info.avgBps; |
| 2417 | param.info.max_bps= (pj_uint32_t)info.maxBps; |
| 2418 | param.info.max_rx_frame_size = info.maxRxFrameSize; |
| 2419 | param.info.frm_ptime = (pj_uint16_t)info.frameLen; |
| 2420 | param.info.frm_ptime_denum = (pj_uint8_t)info.frameLenDenum; |
| 2421 | param.info.enc_ptime = (pj_uint16_t)info.encFrameLen; |
| 2422 | param.info.enc_ptime_denum = (pj_uint8_t)info.encFrameLenDenum; |
| 2423 | param.info.pcm_bits_per_sample = (pj_uint8_t)info.pcmBitsPerSample; |
| 2424 | param.info.pt = (pj_uint8_t)info.pt; |
| 2425 | param.info.fmt_id = info.fmtId; |
| 2426 | |
| 2427 | /* setting part. */ |
| 2428 | param.setting.frm_per_pkt = (pj_uint8_t)setting.frmPerPkt; |
| 2429 | param.setting.vad = setting.vad; |
| 2430 | param.setting.cng = setting.cng; |
| 2431 | param.setting.penh = setting.penh; |
| 2432 | param.setting.plc = setting.plc; |
| 2433 | param.setting.reserved = setting.reserved; |
| 2434 | CodecFmtpUtil::toPj(setting.encFmtp, param.setting.enc_fmtp); |
| 2435 | CodecFmtpUtil::toPj(setting.decFmtp, param.setting.dec_fmtp); |
| 2436 | param.setting.packet_loss = setting.packetLoss; |
| 2437 | param.setting.complexity = setting.complexity; |
| 2438 | param.setting.cbr = setting.cbr; |
| 2439 | return param; |
| 2440 | } |
| 2441 | |
| 2442 | pjmedia_codec_opus_config CodecOpusConfig::toPj() const |
| 2443 | { |
| 2444 | pjmedia_codec_opus_config config; |
| 2445 | |
| 2446 | config.sample_rate = sample_rate; |
| 2447 | config.channel_cnt = channel_cnt; |
| 2448 | config.frm_ptime = frm_ptime; |
| 2449 | config.frm_ptime_denum = frm_ptime_denum; |
| 2450 | config.bit_rate = bit_rate; |
| 2451 | config.packet_loss = packet_loss; |
| 2452 | config.complexity = complexity; |
| 2453 | config.cbr = cbr; |
| 2454 | |
| 2455 | return config; |
| 2456 | } |
| 2457 | |
| 2458 | void CodecOpusConfig::fromPj(const pjmedia_codec_opus_config &config) |
| 2459 | { |
| 2460 | sample_rate = config.sample_rate; |
| 2461 | channel_cnt = config.channel_cnt; |
| 2462 | frm_ptime = config.frm_ptime; |
| 2463 | frm_ptime_denum = config.frm_ptime_denum; |
| 2464 | bit_rate = config.bit_rate; |
| 2465 | packet_loss = config.packet_loss; |
| 2466 | complexity = config.complexity; |
| 2467 | cbr = PJ2BOOL(config.cbr); |
| 2468 | } |
| 2469 | |
| 2470 | pjmedia_codec_lyra_config CodecLyraConfig::toPj() const |
| 2471 | { |
| 2472 | pjmedia_codec_lyra_config config; |
| 2473 | |
| 2474 | config.bit_rate = bitRate; |
| 2475 | config.model_path = str2Pj(modelPath); |
| 2476 | |
| 2477 | return config; |
| 2478 | } |
| 2479 | |
| 2480 | void CodecLyraConfig::fromPj(const pjmedia_codec_lyra_config &config) |
| 2481 | { |
| 2482 | bitRate = config.bit_rate; |
| 2483 | modelPath = pj2Str(config.model_path); |
| 2484 | } |
| 2485 | |
| 2486 | /////////////////////////////////////////////////////////////////////////////// |
| 2487 | void VidCodecParam::fromPj(const pjmedia_vid_codec_param ¶m) |
| 2488 | { |
| 2489 | dir = param.dir; |
| 2490 | packing = param.packing; |
| 2491 | ignoreFmtp = param.ignore_fmtp != PJ_FALSE; |
| 2492 | encMtu = param.enc_mtu; |
| 2493 | encFmt.fromPj(param.enc_fmt); |
| 2494 | decFmt.fromPj(param.dec_fmt); |
| 2495 | CodecFmtpUtil::fromPj(param.enc_fmtp, encFmtp); |
| 2496 | CodecFmtpUtil::fromPj(param.dec_fmtp, decFmtp); |
| 2497 | } |
| 2498 | |
| 2499 | pjmedia_vid_codec_param VidCodecParam::toPj() const |
| 2500 | { |
| 2501 | pjmedia_vid_codec_param param; |
| 2502 | pj_bzero(¶m, sizeof(param)); |
| 2503 | param.dir = dir; |
| 2504 | param.packing = packing; |
| 2505 | param.ignore_fmtp = ignoreFmtp; |
| 2506 | param.enc_mtu = encMtu; |
| 2507 | param.enc_fmt = encFmt.toPj(); |
| 2508 | param.dec_fmt = decFmt.toPj(); |
| 2509 | CodecFmtpUtil::toPj(encFmtp, param.enc_fmtp); |
| 2510 | CodecFmtpUtil::toPj(decFmtp, param.dec_fmtp); |
| 2511 | return param; |
| 2512 | } |
| 2513 | |
| 2514 | /////////////////////////////////////////////////////////////////////////////// |
| 2515 | void MediaEvent::fromPj(const pjmedia_event &ev) |
| 2516 | { |
| 2517 | type = ev.type; |
| 2518 | if (type == PJMEDIA_EVENT_FMT_CHANGED) { |
| 2519 | data.fmtChanged.newWidth = ev.data.fmt_changed.new_fmt.det.vid.size.w; |
| 2520 | data.fmtChanged.newHeight = ev.data.fmt_changed.new_fmt.det.vid.size.h; |
| 2521 | } else if (type == PJMEDIA_EVENT_AUD_DEV_ERROR) { |
| 2522 | data.audDevError.dir = ev.data.aud_dev_err.dir; |
| 2523 | data.audDevError.id = ev.data.aud_dev_err.id; |
| 2524 | data.audDevError.status = ev.data.aud_dev_err.status; |
| 2525 | } |
| 2526 | pjMediaEvent = (void *)&ev; |
| 2527 | } |
| 2528 | |
| 2529 | |
| 2530 | /////////////////////////////////////////////////////////////////////////////// |
| 2531 | |
| 2532 | void VidConfPortInfo::fromPj(const pjsua_vid_conf_port_info &port_info) |
| 2533 | { |
| 2534 | portId = port_info.slot_id; |
| 2535 | name = pj2Str(port_info.name); |
| 2536 | format.fromPj(port_info.format); |
| 2537 | listeners.clear(); |
| 2538 | for (unsigned i=0; i<port_info.listener_cnt; ++i) { |
| 2539 | listeners.push_back(port_info.listeners[i]); |
| 2540 | } |
| 2541 | transmitters.clear(); |
| 2542 | for (unsigned i=0; i<port_info.transmitter_cnt; ++i) { |
| 2543 | transmitters.push_back(port_info.transmitters[i]); |
| 2544 | } |
| 2545 | } |
| 2546 | |
| 2547 | VideoMedia::VideoMedia() |
| 2548 | : Media(PJMEDIA_TYPE_VIDEO), id(PJSUA_INVALID_ID) |
| 2549 | { |
| 2550 | |
| 2551 | } |
| 2552 | |
| 2553 | void VideoMedia::registerMediaPort(MediaPort port, pj_pool_t *pool) |
| 2554 | PJSUA2_THROW(Error) |
| 2555 | { |
| 2556 | #if PJSUA_HAS_VIDEO |
| 2557 | if (!pool) { |
| 2558 | PJSUA2_RAISE_ERROR3(PJ_EINVAL, "registerMediaPort()", |
| 2559 | "pool must be supplied"); |
| 2560 | } |
| 2561 | if (!port) { |
| 2562 | PJSUA2_RAISE_ERROR3(PJ_EINVAL, "registerMediaPort()", |
| 2563 | "port must be supplied"); |
| 2564 | } |
| 2565 | if (id != PJSUA_INVALID_ID) { |
| 2566 | PJSUA2_RAISE_ERROR3(PJ_EINVALIDOP, "registerMediaPort()", |
| 2567 | "VideoMedia is occupied"); |
| 2568 | } |
| 2569 | |
| 2570 | PJSUA2_CHECK_EXPR( pjsua_vid_conf_add_port(pool, |
| 2571 | (pjmedia_port*)port, NULL, |
| 2572 | &id) ); |
| 2573 | #else |
| 2574 | PJ_UNUSED_ARG(port); |
| 2575 | PJ_UNUSED_ARG(pool); |
| 2576 | PJSUA2_RAISE_ERROR(PJ_EINVALIDOP); |
| 2577 | #endif |
| 2578 | } |
| 2579 | |
| 2580 | void VideoMedia::unregisterMediaPort() |
| 2581 | { |
| 2582 | #if PJSUA_HAS_VIDEO |
| 2583 | if (id != PJSUA_INVALID_ID) { |
| 2584 | pjsua_vid_conf_remove_port(id); |
| 2585 | id = PJSUA_INVALID_ID; |
| 2586 | } |
| 2587 | #endif |
| 2588 | } |
| 2589 | |
| 2590 | VideoMedia::~VideoMedia() |
| 2591 | { |
| 2592 | } |
| 2593 | |
| 2594 | VidConfPortInfo VideoMedia::getPortInfo() const PJSUA2_THROW(Error) |
| 2595 | { |
| 2596 | return VideoMedia::getPortInfoFromId(id); |
| 2597 | } |
| 2598 | |
| 2599 | int VideoMedia::getPortId() const |
| 2600 | { |
| 2601 | return id; |
| 2602 | } |
| 2603 | |
| 2604 | VidConfPortInfo VideoMedia::getPortInfoFromId(int port_id) PJSUA2_THROW(Error) |
| 2605 | { |
| 2606 | #if PJSUA_HAS_VIDEO |
| 2607 | pjsua_vid_conf_port_info pj_info; |
| 2608 | VidConfPortInfo pi; |
| 2609 | |
| 2610 | PJSUA2_CHECK_EXPR( pjsua_vid_conf_get_port_info(port_id, &pj_info) ); |
| 2611 | pi.fromPj(pj_info); |
| 2612 | return pi; |
| 2613 | #else |
| 2614 | PJ_UNUSED_ARG(port_id); |
| 2615 | PJSUA2_RAISE_ERROR(PJ_EINVALIDOP); |
| 2616 | #endif |
| 2617 | } |
| 2618 | |
| 2619 | void VideoMedia::startTransmit(const VideoMedia &sink, |
| 2620 | const VideoMediaTransmitParam ¶m) const |
| 2621 | PJSUA2_THROW(Error) |
| 2622 | { |
| 2623 | PJ_UNUSED_ARG(param); |
| 2624 | #if PJSUA_HAS_VIDEO |
| 2625 | PJSUA2_CHECK_EXPR( pjsua_vid_conf_connect(id, sink.id, NULL) ); |
| 2626 | #else |
| 2627 | PJ_UNUSED_ARG(sink); |
| 2628 | PJSUA2_RAISE_ERROR(PJ_EINVALIDOP); |
| 2629 | #endif |
| 2630 | } |
| 2631 | |
| 2632 | void VideoMedia::stopTransmit(const VideoMedia &sink) const |
| 2633 | PJSUA2_THROW(Error) |
| 2634 | { |
| 2635 | #if PJSUA_HAS_VIDEO |
| 2636 | PJSUA2_CHECK_EXPR( pjsua_vid_conf_disconnect(id, sink.id) ); |
| 2637 | #else |
| 2638 | PJ_UNUSED_ARG(sink); |
| 2639 | PJSUA2_RAISE_ERROR(PJ_EINVALIDOP); |
| 2640 | #endif |
| 2641 | } |
| 2642 | |
| 2643 | void VideoMedia::update() const PJSUA2_THROW(Error) |
| 2644 | { |
| 2645 | #if PJSUA_HAS_VIDEO |
| 2646 | PJSUA2_CHECK_EXPR( pjsua_vid_conf_update_port(id) ); |
| 2647 | #else |
| 2648 | PJSUA2_RAISE_ERROR(PJ_EINVALIDOP); |
| 2649 | #endif |
| 2650 | } |