| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
#include <pjmedia-videodev/videodev_imp.h> |
| 19 |
#include <pj/assert.h> |
| 20 |
#include <pj/log.h> |
| 21 |
#include <pj/os.h> |
| 22 |
#include <pj/unicode.h> |
| 23 |
|
| 24 |
|
| 25 |
#if defined(PJMEDIA_HAS_VIDEO) && PJMEDIA_HAS_VIDEO != 0 && \ |
| 26 |
defined(PJMEDIA_VIDEO_DEV_HAS_DSHOW) && PJMEDIA_VIDEO_DEV_HAS_DSHOW != 0 |
| 27 |
|
| 28 |
|
| 29 |
#ifdef _MSC_VER |
| 30 |
# pragma warning(push, 3) |
| 31 |
#endif |
| 32 |
|
| 33 |
#include <windows.h> |
| 34 |
#define COBJMACROS |
| 35 |
#include <DShow.h> |
| 36 |
#include <wmsdkidl.h> |
| 37 |
|
| 38 |
#ifdef _MSC_VER |
| 39 |
# pragma warning(pop) |
| 40 |
#else |
| 41 |
#include <amvideo2.h> |
| 42 |
#endif |
| 43 |
|
| 44 |
#pragma comment(lib, "Strmiids.lib") |
| 45 |
#pragma comment(lib, "Rpcrt4.lib") |
| 46 |
#pragma comment(lib, "Quartz.lib") |
| 47 |
|
| 48 |
#define THIS_FILE "dshow_dev.c" |
| 49 |
#define DEFAULT_CLOCK_RATE 90000 |
| 50 |
#define DEFAULT_WIDTH 640 |
| 51 |
#define DEFAULT_HEIGHT 480 |
| 52 |
#define DEFAULT_FPS 25 |
| 53 |
|
| 54 |
|
| 55 |
#define HAS_VMR 0 |
| 56 |
|
| 57 |
typedef void (*input_callback)(void *user_data, IMediaSample *pMediaSample); |
| 58 |
typedef struct NullRenderer NullRenderer; |
| 59 |
IBaseFilter* NullRenderer_Create(input_callback input_cb, |
| 60 |
void *user_data); |
| 61 |
typedef struct SourceFilter SourceFilter; |
| 62 |
IBaseFilter* SourceFilter_Create(SourceFilter **pSrc); |
| 63 |
HRESULT SourceFilter_Deliver(SourceFilter *src, void *buf, long size); |
| 64 |
void SourceFilter_SetMediaType(SourceFilter *src, AM_MEDIA_TYPE *pmt); |
| 65 |
|
| 66 |
typedef struct dshow_fmt_info |
| 67 |
{ |
| 68 |
pjmedia_format_id pjmedia_format; |
| 69 |
const GUID *dshow_format; |
| 70 |
pj_bool_t enabled; |
| 71 |
} dshow_fmt_info; |
| 72 |
|
| 73 |
static dshow_fmt_info dshow_fmts[] = |
| 74 |
{ |
| 75 |
{PJMEDIA_FORMAT_YUY2, &MEDIASUBTYPE_YUY2, PJ_FALSE} , |
| 76 |
{PJMEDIA_FORMAT_RGB24, &MEDIASUBTYPE_RGB24, PJ_FALSE} , |
| 77 |
{PJMEDIA_FORMAT_RGB32, &MEDIASUBTYPE_RGB32, PJ_FALSE} , |
| 78 |
{PJMEDIA_FORMAT_IYUV, &MEDIASUBTYPE_IYUV, PJ_FALSE} , |
| 79 |
{PJMEDIA_FORMAT_I420, &WMMEDIASUBTYPE_I420, PJ_FALSE} |
| 80 |
}; |
| 81 |
|
| 82 |
|
| 83 |
struct dshow_dev_info |
| 84 |
{ |
| 85 |
pjmedia_vid_dev_info info; |
| 86 |
unsigned dev_id; |
| 87 |
WCHAR display_name[192]; |
| 88 |
}; |
| 89 |
|
| 90 |
|
| 91 |
typedef struct dshow_cap_thread |
| 92 |
{ |
| 93 |
PJ_DECL_LIST_MEMBER(struct dshow_cap_thread); |
| 94 |
DWORD tid; |
| 95 |
pj_thread_desc thread_desc; |
| 96 |
} dshow_cap_thread; |
| 97 |
|
| 98 |
|
| 99 |
struct dshow_factory |
| 100 |
{ |
| 101 |
pjmedia_vid_dev_factory base; |
| 102 |
pj_pool_t *pool; |
| 103 |
pj_pool_t *dev_pool; |
| 104 |
pj_pool_factory *pf; |
| 105 |
|
| 106 |
unsigned dev_count; |
| 107 |
struct dshow_dev_info *dev_info; |
| 108 |
|
| 109 |
CRITICAL_SECTION cap_thread_cs; |
| 110 |
pj_bool_t cap_thread_cs_init; |
| 111 |
dshow_cap_thread cap_threads; |
| 112 |
}; |
| 113 |
|
| 114 |
|
| 115 |
struct dshow_stream |
| 116 |
{ |
| 117 |
pjmedia_vid_dev_stream base; |
| 118 |
pjmedia_vid_dev_param param; |
| 119 |
pj_pool_t *pool; |
| 120 |
struct dshow_factory *factory; |
| 121 |
|
| 122 |
pjmedia_vid_dev_cb vid_cb; |
| 123 |
void *user_data; |
| 124 |
|
| 125 |
pj_bool_t quit_flag; |
| 126 |
pj_bool_t rend_thread_exited; |
| 127 |
pj_bool_t cap_thread_exited; |
| 128 |
pj_bool_t cap_thread_initialized; |
| 129 |
void *frm_buf; |
| 130 |
unsigned frm_buf_size; |
| 131 |
|
| 132 |
struct dshow_graph |
| 133 |
{ |
| 134 |
IFilterGraph *filter_graph; |
| 135 |
IMediaFilter *media_filter; |
| 136 |
SourceFilter *csource_filter; |
| 137 |
IBaseFilter *source_filter; |
| 138 |
IBaseFilter *rend_filter; |
| 139 |
AM_MEDIA_TYPE *mediatype; |
| 140 |
} dgraph; |
| 141 |
|
| 142 |
pj_timestamp cap_ts; |
| 143 |
unsigned cap_ts_inc; |
| 144 |
}; |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
static pj_status_t dshow_factory_init(pjmedia_vid_dev_factory *f); |
| 149 |
static pj_status_t dshow_factory_destroy(pjmedia_vid_dev_factory *f); |
| 150 |
static pj_status_t dshow_factory_refresh(pjmedia_vid_dev_factory *f); |
| 151 |
static unsigned dshow_factory_get_dev_count(pjmedia_vid_dev_factory *f); |
| 152 |
static pj_status_t dshow_factory_get_dev_info(pjmedia_vid_dev_factory *f, |
| 153 |
unsigned index, |
| 154 |
pjmedia_vid_dev_info *info); |
| 155 |
static pj_status_t dshow_factory_default_param(pj_pool_t *pool, |
| 156 |
pjmedia_vid_dev_factory *f, |
| 157 |
unsigned index, |
| 158 |
pjmedia_vid_dev_param *param); |
| 159 |
static pj_status_t dshow_factory_create_stream( |
| 160 |
pjmedia_vid_dev_factory *f, |
| 161 |
pjmedia_vid_dev_param *param, |
| 162 |
const pjmedia_vid_dev_cb *cb, |
| 163 |
void *user_data, |
| 164 |
pjmedia_vid_dev_stream **p_vid_strm); |
| 165 |
|
| 166 |
static pj_status_t dshow_stream_get_param(pjmedia_vid_dev_stream *strm, |
| 167 |
pjmedia_vid_dev_param *param); |
| 168 |
static pj_status_t dshow_stream_get_cap(pjmedia_vid_dev_stream *strm, |
| 169 |
pjmedia_vid_dev_cap cap, |
| 170 |
void *value); |
| 171 |
static pj_status_t dshow_stream_set_cap(pjmedia_vid_dev_stream *strm, |
| 172 |
pjmedia_vid_dev_cap cap, |
| 173 |
const void *value); |
| 174 |
static pj_status_t dshow_stream_start(pjmedia_vid_dev_stream *strm); |
| 175 |
static pj_status_t dshow_stream_put_frame(pjmedia_vid_dev_stream *strm, |
| 176 |
const pjmedia_frame *frame); |
| 177 |
static pj_status_t dshow_stream_stop(pjmedia_vid_dev_stream *strm); |
| 178 |
static pj_status_t dshow_stream_destroy(pjmedia_vid_dev_stream *strm); |
| 179 |
|
| 180 |
|
| 181 |
static pjmedia_vid_dev_factory_op factory_op = |
| 182 |
{ |
| 183 |
&dshow_factory_init, |
| 184 |
&dshow_factory_destroy, |
| 185 |
&dshow_factory_get_dev_count, |
| 186 |
&dshow_factory_get_dev_info, |
| 187 |
&dshow_factory_default_param, |
| 188 |
&dshow_factory_create_stream, |
| 189 |
&dshow_factory_refresh |
| 190 |
}; |
| 191 |
|
| 192 |
static pjmedia_vid_dev_stream_op stream_op = |
| 193 |
{ |
| 194 |
&dshow_stream_get_param, |
| 195 |
&dshow_stream_get_cap, |
| 196 |
&dshow_stream_set_cap, |
| 197 |
&dshow_stream_start, |
| 198 |
NULL, |
| 199 |
&dshow_stream_put_frame, |
| 200 |
&dshow_stream_stop, |
| 201 |
&dshow_stream_destroy |
| 202 |
}; |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
pjmedia_vid_dev_factory* pjmedia_dshow_factory(pj_pool_factory *pf) |
| 212 |
{ |
| 213 |
struct dshow_factory *f; |
| 214 |
pj_pool_t *pool; |
| 215 |
|
| 216 |
pool = pj_pool_create(pf, "dshow video", 1000, 1000, NULL); |
| 217 |
f = PJ_POOL_ZALLOC_T(pool, struct dshow_factory); |
| 218 |
f->pf = pf; |
| 219 |
f->pool = pool; |
| 220 |
f->base.op = &factory_op; |
| 221 |
|
| 222 |
return &f->base; |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
static pj_status_t dshow_factory_init(pjmedia_vid_dev_factory *f) |
| 227 |
{ |
| 228 |
struct dshow_factory *df = (struct dshow_factory*)f; |
| 229 |
HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED); |
| 230 |
if (hr == RPC_E_CHANGED_MODE) { |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED); |
| 236 |
if (FAILED(hr)) { |
| 237 |
PJ_LOG(4,(THIS_FILE, "Failed initializing DShow: " |
| 238 |
"COM library already initialized with " |
| 239 |
"incompatible concurrency model")); |
| 240 |
return PJMEDIA_EVID_INIT; |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
pj_list_init(&df->cap_threads); |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
InitializeCriticalSection(&df->cap_thread_cs); |
| 252 |
df->cap_thread_cs_init = PJ_TRUE; |
| 253 |
|
| 254 |
return dshow_factory_refresh(f); |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
static pj_status_t dshow_factory_destroy(pjmedia_vid_dev_factory *f) |
| 259 |
{ |
| 260 |
struct dshow_factory *df = (struct dshow_factory*)f; |
| 261 |
pj_pool_t *pool = df->pool; |
| 262 |
|
| 263 |
if (df->cap_thread_cs_init) { |
| 264 |
DeleteCriticalSection(&df->cap_thread_cs); |
| 265 |
df->cap_thread_cs_init = PJ_FALSE; |
| 266 |
} |
| 267 |
|
| 268 |
df->pool = NULL; |
| 269 |
if (df->dev_pool) |
| 270 |
pj_pool_release(df->dev_pool); |
| 271 |
if (pool) |
| 272 |
pj_pool_release(pool); |
| 273 |
|
| 274 |
CoUninitialize(); |
| 275 |
|
| 276 |
return PJ_SUCCESS; |
| 277 |
} |
| 278 |
|
| 279 |
static HRESULT get_cap_device(struct dshow_factory *df, |
| 280 |
unsigned id, |
| 281 |
IBaseFilter **filter) |
| 282 |
{ |
| 283 |
IBindCtx *pbc; |
| 284 |
HRESULT hr; |
| 285 |
|
| 286 |
hr = CreateBindCtx(0, &pbc); |
| 287 |
if (SUCCEEDED (hr)) { |
| 288 |
IMoniker *moniker; |
| 289 |
DWORD pchEaten; |
| 290 |
|
| 291 |
hr = MkParseDisplayName(pbc, df->dev_info[id].display_name, |
| 292 |
&pchEaten, &moniker); |
| 293 |
if (SUCCEEDED(hr)) { |
| 294 |
hr = IMoniker_BindToObject(moniker, pbc, NULL, |
| 295 |
&IID_IBaseFilter, |
| 296 |
(LPVOID *)filter); |
| 297 |
IMoniker_Release(moniker); |
| 298 |
} |
| 299 |
IBindCtx_Release(pbc); |
| 300 |
} |
| 301 |
|
| 302 |
return hr; |
| 303 |
} |
| 304 |
|
| 305 |
static void enum_dev_cap(IBaseFilter *filter, |
| 306 |
pjmedia_dir dir, |
| 307 |
const GUID *dshow_fmt, |
| 308 |
AM_MEDIA_TYPE **pMediatype, |
| 309 |
int width, |
| 310 |
int height, |
| 311 |
IPin **pSrcpin, |
| 312 |
pjmedia_vid_dev_info *vdi) |
| 313 |
{ |
| 314 |
IEnumPins *pEnum; |
| 315 |
AM_MEDIA_TYPE *mediatype = NULL; |
| 316 |
pj_bool_t match_wh = PJ_FALSE; |
| 317 |
HRESULT hr; |
| 318 |
|
| 319 |
if (pSrcpin) |
| 320 |
*pSrcpin = NULL; |
| 321 |
hr = IBaseFilter_EnumPins(filter, &pEnum); |
| 322 |
if (SUCCEEDED(hr)) { |
| 323 |
|
| 324 |
IPin *pPin = NULL; |
| 325 |
|
| 326 |
while (IEnumPins_Next(pEnum, 1, &pPin, NULL) == S_OK) { |
| 327 |
PIN_DIRECTION pindirtmp; |
| 328 |
|
| 329 |
hr = IPin_QueryDirection(pPin, &pindirtmp); |
| 330 |
if (hr != S_OK || pindirtmp != PINDIR_OUTPUT) { |
| 331 |
if (SUCCEEDED(hr)) |
| 332 |
IPin_Release(pPin); |
| 333 |
continue; |
| 334 |
} |
| 335 |
|
| 336 |
if (dir == PJMEDIA_DIR_CAPTURE) { |
| 337 |
IAMStreamConfig *streamcaps; |
| 338 |
|
| 339 |
hr = IPin_QueryInterface(pPin, &IID_IAMStreamConfig, |
| 340 |
(LPVOID *)&streamcaps); |
| 341 |
if (SUCCEEDED(hr)) { |
| 342 |
VIDEO_STREAM_CONFIG_CAPS vscc; |
| 343 |
int i, isize, icount; |
| 344 |
|
| 345 |
IAMStreamConfig_GetNumberOfCapabilities(streamcaps, |
| 346 |
&icount, &isize); |
| 347 |
|
| 348 |
for (i = 0; i < icount; i++) { |
| 349 |
unsigned j, nformat; |
| 350 |
RPC_STATUS rpcstatus, rpcstatus2; |
| 351 |
|
| 352 |
hr = IAMStreamConfig_GetStreamCaps(streamcaps, i, |
| 353 |
&mediatype, |
| 354 |
(BYTE *)&vscc); |
| 355 |
if (FAILED (hr)) |
| 356 |
continue; |
| 357 |
|
| 358 |
nformat = (dshow_fmt ? 1 : PJ_ARRAY_SIZE(dshow_fmts)); |
| 359 |
for (j = 0; j < nformat; j++) { |
| 360 |
const GUID *dshow_format = dshow_fmt; |
| 361 |
|
| 362 |
if (!dshow_format) |
| 363 |
dshow_format = dshow_fmts[j].dshow_format; |
| 364 |
if (UuidCompare(&mediatype->subtype, |
| 365 |
(UUID*)dshow_format, |
| 366 |
&rpcstatus) == 0 && |
| 367 |
rpcstatus == RPC_S_OK && |
| 368 |
UuidCompare(&mediatype->formattype, |
| 369 |
(UUID*)&FORMAT_VideoInfo, |
| 370 |
&rpcstatus2) == 0 && |
| 371 |
rpcstatus2 == RPC_S_OK) |
| 372 |
{ |
| 373 |
VIDEOINFOHEADER *vi; |
| 374 |
|
| 375 |
vi = (VIDEOINFOHEADER *)mediatype->pbFormat; |
| 376 |
if (!dshow_fmt) |
| 377 |
dshow_fmts[j].enabled = PJ_TRUE; |
| 378 |
|
| 379 |
if (vdi && vdi->fmt_cnt < |
| 380 |
PJMEDIA_VID_DEV_INFO_FMT_CNT) |
| 381 |
{ |
| 382 |
unsigned fps_num=DEFAULT_FPS, fps_denum=1; |
| 383 |
|
| 384 |
if (vi->AvgTimePerFrame != 0) { |
| 385 |
fps_num = 10000000; |
| 386 |
fps_denum=(unsigned)vi->AvgTimePerFrame; |
| 387 |
} |
| 388 |
|
| 389 |
pjmedia_format_init_video( |
| 390 |
&vdi->fmt[vdi->fmt_cnt++], |
| 391 |
dshow_fmts[j].pjmedia_format, |
| 392 |
vi->bmiHeader.biWidth, |
| 393 |
vi->bmiHeader.biHeight, |
| 394 |
fps_num, fps_denum); |
| 395 |
} |
| 396 |
|
| 397 |
if (pSrcpin) { |
| 398 |
if ((width == 0 && height == 0 ) || |
| 399 |
(vi->bmiHeader.biWidth == width && |
| 400 |
vi->bmiHeader.biHeight == height)) |
| 401 |
{ |
| 402 |
match_wh = PJ_TRUE; |
| 403 |
} |
| 404 |
*pSrcpin = pPin; |
| 405 |
*pMediatype = mediatype; |
| 406 |
} |
| 407 |
} |
| 408 |
} |
| 409 |
if (pSrcpin && *pSrcpin && match_wh) |
| 410 |
break; |
| 411 |
} |
| 412 |
IAMStreamConfig_Release(streamcaps); |
| 413 |
} |
| 414 |
} else { |
| 415 |
*pSrcpin = pPin; |
| 416 |
} |
| 417 |
if (pSrcpin && *pSrcpin) |
| 418 |
break; |
| 419 |
IPin_Release(pPin); |
| 420 |
} |
| 421 |
IEnumPins_Release(pEnum); |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
|
| 426 |
static pj_status_t dshow_factory_refresh(pjmedia_vid_dev_factory *f) |
| 427 |
{ |
| 428 |
struct dshow_factory *df = (struct dshow_factory*)f; |
| 429 |
struct dshow_dev_info *ddi; |
| 430 |
int dev_count = 0; |
| 431 |
unsigned c; |
| 432 |
ICreateDevEnum *dev_enum = NULL; |
| 433 |
IEnumMoniker *enum_cat = NULL; |
| 434 |
IMoniker *moniker = NULL; |
| 435 |
HRESULT hr; |
| 436 |
ULONG fetched; |
| 437 |
|
| 438 |
if (df->dev_pool) { |
| 439 |
pj_pool_release(df->dev_pool); |
| 440 |
df->dev_pool = NULL; |
| 441 |
} |
| 442 |
|
| 443 |
for (c = 0; c < PJ_ARRAY_SIZE(dshow_fmts); c++) { |
| 444 |
dshow_fmts[c].enabled = PJ_FALSE; |
| 445 |
} |
| 446 |
|
| 447 |
df->dev_count = 0; |
| 448 |
df->dev_pool = pj_pool_create(df->pf, "dshow video", 500, 500, NULL); |
| 449 |
|
| 450 |
hr = CoCreateInstance(&CLSID_SystemDeviceEnum, NULL, |
| 451 |
CLSCTX_INPROC_SERVER, &IID_ICreateDevEnum, |
| 452 |
(void**)&dev_enum); |
| 453 |
if (FAILED(hr) || |
| 454 |
ICreateDevEnum_CreateClassEnumerator(dev_enum, |
| 455 |
&CLSID_VideoInputDeviceCategory, &enum_cat, 0) != S_OK) |
| 456 |
{ |
| 457 |
PJ_LOG(4,(THIS_FILE, "Windows found no video input devices")); |
| 458 |
if (dev_enum) |
| 459 |
ICreateDevEnum_Release(dev_enum); |
| 460 |
dev_count = 0; |
| 461 |
} else { |
| 462 |
while (IEnumMoniker_Next(enum_cat, 1, &moniker, &fetched) == S_OK) { |
| 463 |
dev_count++; |
| 464 |
} |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
dev_count += 1; |
| 469 |
df->dev_info = (struct dshow_dev_info*) |
| 470 |
pj_pool_calloc(df->dev_pool, dev_count, |
| 471 |
sizeof(struct dshow_dev_info)); |
| 472 |
|
| 473 |
if (dev_count > 1) { |
| 474 |
IEnumMoniker_Reset(enum_cat); |
| 475 |
while (IEnumMoniker_Next(enum_cat, 1, &moniker, &fetched) == S_OK) { |
| 476 |
IPropertyBag *prop_bag; |
| 477 |
|
| 478 |
hr = IMoniker_BindToStorage(moniker, 0, 0, &IID_IPropertyBag, |
| 479 |
(void**)&prop_bag); |
| 480 |
if (SUCCEEDED(hr)) { |
| 481 |
VARIANT var_name; |
| 482 |
|
| 483 |
VariantInit(&var_name); |
| 484 |
hr = IPropertyBag_Read(prop_bag, L"FriendlyName", |
| 485 |
&var_name, NULL); |
| 486 |
if (SUCCEEDED(hr) && var_name.bstrVal) { |
| 487 |
WCHAR *wszDisplayName = NULL; |
| 488 |
IBaseFilter *filter; |
| 489 |
|
| 490 |
ddi = &df->dev_info[df->dev_count++]; |
| 491 |
pj_bzero(ddi, sizeof(*ddi)); |
| 492 |
pj_unicode_to_ansi(var_name.bstrVal, |
| 493 |
wcslen(var_name.bstrVal), |
| 494 |
ddi->info.name, |
| 495 |
sizeof(ddi->info.name)); |
| 496 |
|
| 497 |
hr = IMoniker_GetDisplayName(moniker, NULL, NULL, |
| 498 |
&wszDisplayName); |
| 499 |
if (hr == S_OK && wszDisplayName) { |
| 500 |
pj_memcpy(ddi->display_name, wszDisplayName, |
| 501 |
(wcslen(wszDisplayName)+1) * sizeof(WCHAR)); |
| 502 |
CoTaskMemFree(wszDisplayName); |
| 503 |
} |
| 504 |
|
| 505 |
pj_ansi_strxcpy(ddi->info.driver, "dshow", |
| 506 |
sizeof(ddi->info.driver)); |
| 507 |
ddi->info.dir = PJMEDIA_DIR_CAPTURE; |
| 508 |
ddi->info.has_callback = PJ_TRUE; |
| 509 |
|
| 510 |
|
| 511 |
ddi->info.caps = PJMEDIA_VID_DEV_CAP_FORMAT; |
| 512 |
|
| 513 |
hr = get_cap_device(df, df->dev_count-1, &filter); |
| 514 |
if (SUCCEEDED(hr)) { |
| 515 |
ddi->info.fmt_cnt = 0; |
| 516 |
enum_dev_cap(filter, ddi->info.dir, NULL, NULL, |
| 517 |
0, 0, NULL, &ddi->info); |
| 518 |
} |
| 519 |
} |
| 520 |
VariantClear(&var_name); |
| 521 |
|
| 522 |
IPropertyBag_Release(prop_bag); |
| 523 |
} |
| 524 |
IMoniker_Release(moniker); |
| 525 |
} |
| 526 |
|
| 527 |
IEnumMoniker_Release(enum_cat); |
| 528 |
ICreateDevEnum_Release(dev_enum); |
| 529 |
} |
| 530 |
|
| 531 |
#if HAS_VMR |
| 532 |
ddi = &df->dev_info[df->dev_count++]; |
| 533 |
pj_bzero(ddi, sizeof(*ddi)); |
| 534 |
pj_ansi_strxcpy(ddi->info.name, "Video Mixing Renderer", |
| 535 |
sizeof(ddi->info.name)); |
| 536 |
pj_ansi_strxcpy(ddi->info.driver, "dshow", |
| 537 |
sizeof(ddi->info.driver)); |
| 538 |
ddi->info.driver[sizeof(ddi->info.driver)-1] = '\0'; |
| 539 |
ddi->info.dir = PJMEDIA_DIR_RENDER; |
| 540 |
ddi->info.has_callback = PJ_FALSE; |
| 541 |
ddi->info.caps = PJMEDIA_VID_DEV_CAP_FORMAT; |
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
ddi->info.fmt_cnt = 1; |
| 546 |
pjmedia_format_init_video(&ddi->info.fmt[0], dshow_fmts[0].pjmedia_format, |
| 547 |
DEFAULT_WIDTH, DEFAULT_HEIGHT, |
| 548 |
DEFAULT_FPS, 1); |
| 549 |
#endif |
| 550 |
|
| 551 |
PJ_LOG(4, (THIS_FILE, "DShow has %d devices:", |
| 552 |
df->dev_count)); |
| 553 |
for (c = 0; c < df->dev_count; ++c) { |
| 554 |
PJ_LOG(4, (THIS_FILE, " dev_id %d: %s (%s)", |
| 555 |
c, |
| 556 |
df->dev_info[c].info.name, |
| 557 |
df->dev_info[c].info.dir & PJMEDIA_DIR_CAPTURE ? |
| 558 |
"capture" : "render")); |
| 559 |
} |
| 560 |
|
| 561 |
return PJ_SUCCESS; |
| 562 |
} |
| 563 |
|
| 564 |
|
| 565 |
static unsigned dshow_factory_get_dev_count(pjmedia_vid_dev_factory *f) |
| 566 |
{ |
| 567 |
struct dshow_factory *df = (struct dshow_factory*)f; |
| 568 |
return df->dev_count; |
| 569 |
} |
| 570 |
|
| 571 |
|
| 572 |
static pj_status_t dshow_factory_get_dev_info(pjmedia_vid_dev_factory *f, |
| 573 |
unsigned index, |
| 574 |
pjmedia_vid_dev_info *info) |
| 575 |
{ |
| 576 |
struct dshow_factory *df = (struct dshow_factory*)f; |
| 577 |
|
| 578 |
PJ_ASSERT_RETURN(index < df->dev_count, PJMEDIA_EVID_INVDEV); |
| 579 |
|
| 580 |
pj_memcpy(info, &df->dev_info[index].info, sizeof(*info)); |
| 581 |
|
| 582 |
return PJ_SUCCESS; |
| 583 |
} |
| 584 |
|
| 585 |
|
| 586 |
static pj_status_t dshow_factory_default_param(pj_pool_t *pool, |
| 587 |
pjmedia_vid_dev_factory *f, |
| 588 |
unsigned index, |
| 589 |
pjmedia_vid_dev_param *param) |
| 590 |
{ |
| 591 |
struct dshow_factory *df = (struct dshow_factory*)f; |
| 592 |
struct dshow_dev_info *di = &df->dev_info[index]; |
| 593 |
|
| 594 |
PJ_ASSERT_RETURN(index < df->dev_count, PJMEDIA_EVID_INVDEV); |
| 595 |
|
| 596 |
PJ_UNUSED_ARG(pool); |
| 597 |
|
| 598 |
pj_bzero(param, sizeof(*param)); |
| 599 |
if (di->info.dir & PJMEDIA_DIR_CAPTURE) { |
| 600 |
param->dir = PJMEDIA_DIR_CAPTURE; |
| 601 |
param->cap_id = index; |
| 602 |
param->rend_id = PJMEDIA_VID_INVALID_DEV; |
| 603 |
} else if (di->info.dir & PJMEDIA_DIR_RENDER) { |
| 604 |
param->dir = PJMEDIA_DIR_RENDER; |
| 605 |
param->rend_id = index; |
| 606 |
param->cap_id = PJMEDIA_VID_INVALID_DEV; |
| 607 |
} else { |
| 608 |
return PJMEDIA_EVID_INVDEV; |
| 609 |
} |
| 610 |
|
| 611 |
|
| 612 |
param->clock_rate = DEFAULT_CLOCK_RATE; |
| 613 |
param->flags = PJMEDIA_VID_DEV_CAP_FORMAT; |
| 614 |
|
| 615 |
pjmedia_format_copy(¶m->fmt, &di->info.fmt[0]); |
| 616 |
|
| 617 |
return PJ_SUCCESS; |
| 618 |
} |
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
static pj_bool_t dshow_register_thread(struct dshow_factory *df) |
| 629 |
{ |
| 630 |
DWORD tid = GetCurrentThreadId(); |
| 631 |
dshow_cap_thread *ct; |
| 632 |
pj_bool_t new_entry = PJ_FALSE; |
| 633 |
|
| 634 |
EnterCriticalSection(&df->cap_thread_cs); |
| 635 |
|
| 636 |
ct = df->cap_threads.next; |
| 637 |
while (ct != &df->cap_threads) { |
| 638 |
if (ct->tid == tid) |
| 639 |
break; |
| 640 |
ct = ct->next; |
| 641 |
} |
| 642 |
|
| 643 |
if (ct == &df->cap_threads) { |
| 644 |
ct = PJ_POOL_ZALLOC_T(df->pool, dshow_cap_thread); |
| 645 |
ct->tid = tid; |
| 646 |
pj_list_push_back(&df->cap_threads, ct); |
| 647 |
new_entry = PJ_TRUE; |
| 648 |
} |
| 649 |
|
| 650 |
LeaveCriticalSection(&df->cap_thread_cs); |
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
if (new_entry || !pj_thread_is_registered()) { |
| 661 |
pj_thread_t *thread; |
| 662 |
pj_status_t status; |
| 663 |
|
| 664 |
status = pj_thread_register("ds_cap", ct->thread_desc, &thread); |
| 665 |
if (status != PJ_SUCCESS) |
| 666 |
return PJ_FALSE; |
| 667 |
PJ_LOG(5,(THIS_FILE, "Capture thread registered (tid=%lu)", |
| 668 |
(unsigned long)tid)); |
| 669 |
} |
| 670 |
|
| 671 |
return PJ_TRUE; |
| 672 |
} |
| 673 |
|
| 674 |
static void input_cb(void *user_data, IMediaSample *pMediaSample) |
| 675 |
{ |
| 676 |
struct dshow_stream *strm = (struct dshow_stream*)user_data; |
| 677 |
pjmedia_frame frame = {0}; |
| 678 |
|
| 679 |
if (strm->quit_flag) { |
| 680 |
strm->cap_thread_exited = PJ_TRUE; |
| 681 |
return; |
| 682 |
} |
| 683 |
|
| 684 |
if (!dshow_register_thread(strm->factory)) |
| 685 |
return; |
| 686 |
strm->cap_thread_initialized = 1; |
| 687 |
|
| 688 |
frame.type = PJMEDIA_FRAME_TYPE_VIDEO; |
| 689 |
IMediaSample_GetPointer(pMediaSample, (BYTE **)&frame.buf); |
| 690 |
frame.size = IMediaSample_GetActualDataLength(pMediaSample); |
| 691 |
frame.bit_info = 0; |
| 692 |
frame.timestamp = strm->cap_ts; |
| 693 |
strm->cap_ts.u64 += strm->cap_ts_inc; |
| 694 |
|
| 695 |
if (strm->frm_buf_size) { |
| 696 |
unsigned i, stride; |
| 697 |
BYTE *src_buf, *dst_buf; |
| 698 |
pjmedia_video_format_detail *vfd; |
| 699 |
|
| 700 |
|
| 701 |
src_buf = dst_buf = (BYTE *)frame.buf; |
| 702 |
stride = strm->frm_buf_size; |
| 703 |
vfd = pjmedia_format_get_video_format_detail(&strm->param.fmt, |
| 704 |
PJ_TRUE); |
| 705 |
src_buf += (vfd->size.h - 1) * stride; |
| 706 |
|
| 707 |
for (i = vfd->size.h / 2; i > 0; i--) { |
| 708 |
memcpy(strm->frm_buf, dst_buf, stride); |
| 709 |
memcpy(dst_buf, src_buf, stride); |
| 710 |
memcpy(src_buf, strm->frm_buf, stride); |
| 711 |
dst_buf += stride; |
| 712 |
src_buf -= stride; |
| 713 |
} |
| 714 |
} |
| 715 |
|
| 716 |
if (strm->vid_cb.capture_cb) |
| 717 |
(*strm->vid_cb.capture_cb)(&strm->base, strm->user_data, &frame); |
| 718 |
} |
| 719 |
|
| 720 |
|
| 721 |
static pj_status_t dshow_stream_put_frame(pjmedia_vid_dev_stream *strm, |
| 722 |
const pjmedia_frame *frame) |
| 723 |
{ |
| 724 |
struct dshow_stream *stream = (struct dshow_stream*)strm; |
| 725 |
HRESULT hr; |
| 726 |
|
| 727 |
if (stream->quit_flag) { |
| 728 |
stream->rend_thread_exited = PJ_TRUE; |
| 729 |
return PJ_SUCCESS; |
| 730 |
} |
| 731 |
|
| 732 |
hr = SourceFilter_Deliver(stream->dgraph.csource_filter, |
| 733 |
frame->buf, (long)frame->size); |
| 734 |
if (FAILED(hr)) |
| 735 |
return hr; |
| 736 |
|
| 737 |
return PJ_SUCCESS; |
| 738 |
} |
| 739 |
|
| 740 |
static dshow_fmt_info* get_dshow_format_info(pjmedia_format_id id) |
| 741 |
{ |
| 742 |
unsigned i; |
| 743 |
|
| 744 |
for (i = 0; i < PJ_ARRAY_SIZE(dshow_fmts); i++) { |
| 745 |
if (dshow_fmts[i].pjmedia_format == id && dshow_fmts[i].enabled) |
| 746 |
return &dshow_fmts[i]; |
| 747 |
} |
| 748 |
|
| 749 |
return NULL; |
| 750 |
} |
| 751 |
|
| 752 |
static pj_status_t create_filter_graph(pjmedia_dir dir, |
| 753 |
unsigned id, |
| 754 |
pj_bool_t use_def_size, |
| 755 |
pj_bool_t use_def_fps, |
| 756 |
struct dshow_factory *df, |
| 757 |
struct dshow_stream *strm, |
| 758 |
struct dshow_graph *graph) |
| 759 |
{ |
| 760 |
HRESULT hr; |
| 761 |
IEnumPins *pEnum; |
| 762 |
IPin *srcpin = NULL; |
| 763 |
IPin *sinkpin = NULL; |
| 764 |
AM_MEDIA_TYPE *mediatype= NULL, mtype; |
| 765 |
VIDEOINFOHEADER *video_info, *vi = NULL; |
| 766 |
pjmedia_video_format_detail *vfd; |
| 767 |
const pjmedia_video_format_info *vfi; |
| 768 |
|
| 769 |
vfi = pjmedia_get_video_format_info(pjmedia_video_format_mgr_instance(), |
| 770 |
strm->param.fmt.id); |
| 771 |
if (!vfi) |
| 772 |
return PJMEDIA_EVID_BADFORMAT; |
| 773 |
|
| 774 |
hr = CoCreateInstance(&CLSID_FilterGraph, NULL, CLSCTX_INPROC, |
| 775 |
&IID_IFilterGraph, (LPVOID *)&graph->filter_graph); |
| 776 |
if (FAILED(hr)) { |
| 777 |
goto on_error; |
| 778 |
} |
| 779 |
|
| 780 |
hr = IFilterGraph_QueryInterface(graph->filter_graph, &IID_IMediaFilter, |
| 781 |
(LPVOID *)&graph->media_filter); |
| 782 |
if (FAILED(hr)) { |
| 783 |
goto on_error; |
| 784 |
} |
| 785 |
|
| 786 |
if (dir == PJMEDIA_DIR_CAPTURE) { |
| 787 |
hr = get_cap_device(df, id, &graph->source_filter); |
| 788 |
if (FAILED(hr)) { |
| 789 |
goto on_error; |
| 790 |
} |
| 791 |
} else { |
| 792 |
graph->source_filter = SourceFilter_Create(&graph->csource_filter); |
| 793 |
} |
| 794 |
|
| 795 |
hr = IFilterGraph_AddFilter(graph->filter_graph, graph->source_filter, |
| 796 |
L"capture"); |
| 797 |
if (FAILED(hr)) { |
| 798 |
goto on_error; |
| 799 |
} |
| 800 |
|
| 801 |
if (dir == PJMEDIA_DIR_CAPTURE) { |
| 802 |
graph->rend_filter = NullRenderer_Create(input_cb, strm); |
| 803 |
} else { |
| 804 |
hr = CoCreateInstance(&CLSID_VideoMixingRenderer, NULL, |
| 805 |
CLSCTX_INPROC, &IID_IBaseFilter, |
| 806 |
(LPVOID *)&graph->rend_filter); |
| 807 |
if (FAILED (hr)) { |
| 808 |
goto on_error; |
| 809 |
} |
| 810 |
} |
| 811 |
|
| 812 |
IBaseFilter_EnumPins(graph->rend_filter, &pEnum); |
| 813 |
if (SUCCEEDED(hr)) { |
| 814 |
|
| 815 |
IPin *pPin = NULL; |
| 816 |
|
| 817 |
while (IEnumPins_Next(pEnum, 1, &pPin, NULL) == S_OK) { |
| 818 |
PIN_DIRECTION pindirtmp; |
| 819 |
|
| 820 |
hr = IPin_QueryDirection(pPin, &pindirtmp); |
| 821 |
if (hr == S_OK && pindirtmp == PINDIR_INPUT) { |
| 822 |
sinkpin = pPin; |
| 823 |
break; |
| 824 |
} |
| 825 |
IPin_Release(pPin); |
| 826 |
} |
| 827 |
IEnumPins_Release(pEnum); |
| 828 |
} |
| 829 |
|
| 830 |
vfd = pjmedia_format_get_video_format_detail(&strm->param.fmt, PJ_TRUE); |
| 831 |
|
| 832 |
enum_dev_cap(graph->source_filter, dir, |
| 833 |
get_dshow_format_info(strm->param.fmt.id)->dshow_format, |
| 834 |
&mediatype, (use_def_size? 0: vfd->size.w), |
| 835 |
(use_def_size? 0: vfd->size.h), &srcpin, NULL); |
| 836 |
graph->mediatype = mediatype; |
| 837 |
|
| 838 |
if (srcpin && dir == PJMEDIA_DIR_RENDER) { |
| 839 |
mediatype = graph->mediatype = &mtype; |
| 840 |
|
| 841 |
memset (mediatype, 0, sizeof(AM_MEDIA_TYPE)); |
| 842 |
mediatype->majortype = MEDIATYPE_Video; |
| 843 |
mediatype->subtype = *(get_dshow_format_info(strm->param.fmt.id)-> |
| 844 |
dshow_format); |
| 845 |
mediatype->bFixedSizeSamples = TRUE; |
| 846 |
mediatype->bTemporalCompression = FALSE; |
| 847 |
|
| 848 |
vi = (VIDEOINFOHEADER *) |
| 849 |
CoTaskMemAlloc(sizeof(VIDEOINFOHEADER)); |
| 850 |
memset (vi, 0, sizeof(VIDEOINFOHEADER)); |
| 851 |
mediatype->formattype = FORMAT_VideoInfo; |
| 852 |
mediatype->cbFormat = sizeof(VIDEOINFOHEADER); |
| 853 |
mediatype->pbFormat = (BYTE *)vi; |
| 854 |
|
| 855 |
vi->rcSource.bottom = vfd->size.h; |
| 856 |
vi->rcSource.right = vfd->size.w; |
| 857 |
vi->rcTarget.bottom = vfd->size.h; |
| 858 |
vi->rcTarget.right = vfd->size.w; |
| 859 |
|
| 860 |
vi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); |
| 861 |
vi->bmiHeader.biPlanes = 1; |
| 862 |
vi->bmiHeader.biBitCount = vfi->bpp; |
| 863 |
vi->bmiHeader.biCompression = strm->param.fmt.id; |
| 864 |
} |
| 865 |
|
| 866 |
if (!srcpin || !sinkpin || !mediatype) { |
| 867 |
hr = VFW_E_TYPE_NOT_ACCEPTED; |
| 868 |
goto on_error; |
| 869 |
} |
| 870 |
video_info = (VIDEOINFOHEADER *) mediatype->pbFormat; |
| 871 |
if (!use_def_size) { |
| 872 |
video_info->bmiHeader.biWidth = vfd->size.w; |
| 873 |
video_info->bmiHeader.biHeight = vfd->size.h; |
| 874 |
} |
| 875 |
if (video_info->AvgTimePerFrame == 0 || |
| 876 |
(!use_def_fps && vfd->fps.num != 0)) |
| 877 |
{ |
| 878 |
video_info->AvgTimePerFrame = (LONGLONG) (10000000 * |
| 879 |
(double)vfd->fps.denum / |
| 880 |
vfd->fps.num); |
| 881 |
} |
| 882 |
video_info->bmiHeader.biSizeImage = DIBSIZE(video_info->bmiHeader); |
| 883 |
mediatype->lSampleSize = DIBSIZE(video_info->bmiHeader); |
| 884 |
if (graph->csource_filter) |
| 885 |
SourceFilter_SetMediaType(graph->csource_filter, |
| 886 |
mediatype); |
| 887 |
|
| 888 |
hr = IFilterGraph_AddFilter(graph->filter_graph, |
| 889 |
(IBaseFilter *)graph->rend_filter, |
| 890 |
L"renderer"); |
| 891 |
if (FAILED(hr)) |
| 892 |
goto on_error; |
| 893 |
|
| 894 |
hr = IFilterGraph_ConnectDirect(graph->filter_graph, srcpin, sinkpin, |
| 895 |
mediatype); |
| 896 |
if (SUCCEEDED(hr)) { |
| 897 |
if (use_def_size || use_def_fps) { |
| 898 |
pjmedia_format_init_video(&strm->param.fmt, strm->param.fmt.id, |
| 899 |
video_info->bmiHeader.biWidth, |
| 900 |
video_info->bmiHeader.biHeight, |
| 901 |
10000000, |
| 902 |
(unsigned)video_info->AvgTimePerFrame); |
| 903 |
} |
| 904 |
|
| 905 |
strm->frm_buf_size = 0; |
| 906 |
if (dir == PJMEDIA_DIR_CAPTURE && |
| 907 |
video_info->bmiHeader.biCompression == BI_RGB && |
| 908 |
video_info->bmiHeader.biHeight > 0) |
| 909 |
{ |
| 910 |
|
| 911 |
strm->frm_buf_size = (video_info->bmiHeader.biBitCount >> 3) * |
| 912 |
video_info->bmiHeader.biWidth; |
| 913 |
strm->frm_buf = pj_pool_alloc(strm->pool, strm->frm_buf_size); |
| 914 |
} |
| 915 |
} |
| 916 |
|
| 917 |
on_error: |
| 918 |
if (srcpin) |
| 919 |
IPin_Release(srcpin); |
| 920 |
if (sinkpin) |
| 921 |
IPin_Release(sinkpin); |
| 922 |
if (vi) |
| 923 |
CoTaskMemFree(vi); |
| 924 |
if (FAILED(hr)) { |
| 925 |
char msg[80]; |
| 926 |
if (AMGetErrorText(hr, msg, sizeof(msg))) { |
| 927 |
PJ_LOG(4,(THIS_FILE, "Error creating filter graph: %s (hr=0x%x)", |
| 928 |
msg, hr)); |
| 929 |
} |
| 930 |
return PJ_EUNKNOWN; |
| 931 |
} |
| 932 |
|
| 933 |
return PJ_SUCCESS; |
| 934 |
} |
| 935 |
|
| 936 |
static void destroy_filter_graph(struct dshow_stream * stream) |
| 937 |
{ |
| 938 |
if (stream->dgraph.source_filter) { |
| 939 |
IBaseFilter_Release(stream->dgraph.source_filter); |
| 940 |
stream->dgraph.source_filter = NULL; |
| 941 |
} |
| 942 |
if (stream->dgraph.rend_filter) { |
| 943 |
IBaseFilter_Release(stream->dgraph.rend_filter); |
| 944 |
stream->dgraph.rend_filter = NULL; |
| 945 |
} |
| 946 |
if (stream->dgraph.media_filter) { |
| 947 |
IMediaFilter_Release(stream->dgraph.media_filter); |
| 948 |
stream->dgraph.media_filter = NULL; |
| 949 |
} |
| 950 |
if (stream->dgraph.filter_graph) { |
| 951 |
IFilterGraph_Release(stream->dgraph.filter_graph); |
| 952 |
stream->dgraph.filter_graph = NULL; |
| 953 |
} |
| 954 |
} |
| 955 |
|
| 956 |
|
| 957 |
static pj_status_t dshow_factory_create_stream( |
| 958 |
pjmedia_vid_dev_factory *f, |
| 959 |
pjmedia_vid_dev_param *param, |
| 960 |
const pjmedia_vid_dev_cb *cb, |
| 961 |
void *user_data, |
| 962 |
pjmedia_vid_dev_stream **p_vid_strm) |
| 963 |
{ |
| 964 |
struct dshow_factory *df = (struct dshow_factory*)f; |
| 965 |
pj_pool_t *pool; |
| 966 |
struct dshow_stream *strm; |
| 967 |
pj_status_t status; |
| 968 |
|
| 969 |
PJ_ASSERT_RETURN(param->dir == PJMEDIA_DIR_CAPTURE || |
| 970 |
param->dir == PJMEDIA_DIR_RENDER, PJ_EINVAL); |
| 971 |
|
| 972 |
if (!get_dshow_format_info(param->fmt.id)) |
| 973 |
return PJMEDIA_EVID_BADFORMAT; |
| 974 |
|
| 975 |
|
| 976 |
pool = pj_pool_create(df->pf, "dshow-dev", 1000, 1000, NULL); |
| 977 |
PJ_ASSERT_RETURN(pool != NULL, PJ_ENOMEM); |
| 978 |
|
| 979 |
strm = PJ_POOL_ZALLOC_T(pool, struct dshow_stream); |
| 980 |
pj_memcpy(&strm->param, param, sizeof(*param)); |
| 981 |
strm->pool = pool; |
| 982 |
strm->factory = df; |
| 983 |
pj_memcpy(&strm->vid_cb, cb, sizeof(*cb)); |
| 984 |
strm->user_data = user_data; |
| 985 |
|
| 986 |
if (param->dir & PJMEDIA_DIR_CAPTURE) { |
| 987 |
const pjmedia_video_format_detail *vfd; |
| 988 |
|
| 989 |
|
| 990 |
status = create_filter_graph(PJMEDIA_DIR_CAPTURE, param->cap_id, |
| 991 |
PJ_FALSE, PJ_FALSE, df, strm, |
| 992 |
&strm->dgraph); |
| 993 |
if (status != PJ_SUCCESS) { |
| 994 |
destroy_filter_graph(strm); |
| 995 |
|
| 996 |
PJ_LOG(4,(THIS_FILE, "Trying to open dshow dev with default fps")); |
| 997 |
status = create_filter_graph(PJMEDIA_DIR_CAPTURE, param->cap_id, |
| 998 |
PJ_FALSE, PJ_TRUE, df, strm, |
| 999 |
&strm->dgraph); |
| 1000 |
|
| 1001 |
if (status != PJ_SUCCESS) { |
| 1002 |
|
| 1003 |
destroy_filter_graph(strm); |
| 1004 |
|
| 1005 |
PJ_LOG(4,(THIS_FILE, "Trying to open dshow dev with default " |
| 1006 |
"size & fps")); |
| 1007 |
status = create_filter_graph(PJMEDIA_DIR_CAPTURE, |
| 1008 |
param->cap_id, |
| 1009 |
PJ_TRUE, PJ_TRUE, df, strm, |
| 1010 |
&strm->dgraph); |
| 1011 |
} |
| 1012 |
|
| 1013 |
if (status != PJ_SUCCESS) |
| 1014 |
goto on_error; |
| 1015 |
pj_memcpy(param, &strm->param, sizeof(*param)); |
| 1016 |
} |
| 1017 |
|
| 1018 |
vfd = pjmedia_format_get_video_format_detail(¶m->fmt, PJ_TRUE); |
| 1019 |
strm->cap_ts_inc = PJMEDIA_SPF2(param->clock_rate, &vfd->fps, 1); |
| 1020 |
} else if (param->dir & PJMEDIA_DIR_RENDER) { |
| 1021 |
|
| 1022 |
status = create_filter_graph(PJMEDIA_DIR_RENDER, param->rend_id, |
| 1023 |
PJ_FALSE, PJ_FALSE, df, strm, |
| 1024 |
&strm->dgraph); |
| 1025 |
if (status != PJ_SUCCESS) |
| 1026 |
goto on_error; |
| 1027 |
} |
| 1028 |
|
| 1029 |
|
| 1030 |
if (param->flags & PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW) { |
| 1031 |
dshow_stream_set_cap(&strm->base, |
| 1032 |
PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW, |
| 1033 |
¶m->window); |
| 1034 |
} |
| 1035 |
|
| 1036 |
|
| 1037 |
strm->base.op = &stream_op; |
| 1038 |
*p_vid_strm = &strm->base; |
| 1039 |
|
| 1040 |
return PJ_SUCCESS; |
| 1041 |
|
| 1042 |
on_error: |
| 1043 |
dshow_stream_destroy((pjmedia_vid_dev_stream *)strm); |
| 1044 |
return status; |
| 1045 |
} |
| 1046 |
|
| 1047 |
|
| 1048 |
static pj_status_t dshow_stream_get_param(pjmedia_vid_dev_stream *s, |
| 1049 |
pjmedia_vid_dev_param *pi) |
| 1050 |
{ |
| 1051 |
struct dshow_stream *strm = (struct dshow_stream*)s; |
| 1052 |
|
| 1053 |
PJ_ASSERT_RETURN(strm && pi, PJ_EINVAL); |
| 1054 |
|
| 1055 |
pj_memcpy(pi, &strm->param, sizeof(*pi)); |
| 1056 |
|
| 1057 |
if (dshow_stream_get_cap(s, PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW, |
| 1058 |
&pi->window) == PJ_SUCCESS) |
| 1059 |
{ |
| 1060 |
pi->flags |= PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW; |
| 1061 |
} |
| 1062 |
|
| 1063 |
return PJ_SUCCESS; |
| 1064 |
} |
| 1065 |
|
| 1066 |
|
| 1067 |
static pj_status_t dshow_stream_get_cap(pjmedia_vid_dev_stream *s, |
| 1068 |
pjmedia_vid_dev_cap cap, |
| 1069 |
void *pval) |
| 1070 |
{ |
| 1071 |
struct dshow_stream *strm = (struct dshow_stream*)s; |
| 1072 |
|
| 1073 |
PJ_UNUSED_ARG(strm); |
| 1074 |
|
| 1075 |
PJ_ASSERT_RETURN(s && pval, PJ_EINVAL); |
| 1076 |
|
| 1077 |
if (cap==PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW) |
| 1078 |
{ |
| 1079 |
*(unsigned*)pval = 0; |
| 1080 |
return PJ_SUCCESS; |
| 1081 |
} else { |
| 1082 |
return PJMEDIA_EVID_INVCAP; |
| 1083 |
} |
| 1084 |
} |
| 1085 |
|
| 1086 |
|
| 1087 |
static pj_status_t dshow_stream_set_cap(pjmedia_vid_dev_stream *s, |
| 1088 |
pjmedia_vid_dev_cap cap, |
| 1089 |
const void *pval) |
| 1090 |
{ |
| 1091 |
struct dshow_stream *strm = (struct dshow_stream*)s; |
| 1092 |
|
| 1093 |
PJ_UNUSED_ARG(strm); |
| 1094 |
|
| 1095 |
PJ_ASSERT_RETURN(s && pval, PJ_EINVAL); |
| 1096 |
|
| 1097 |
if (cap==PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW) |
| 1098 |
{ |
| 1099 |
|
| 1100 |
return PJ_SUCCESS; |
| 1101 |
} |
| 1102 |
|
| 1103 |
return PJMEDIA_EVID_INVCAP; |
| 1104 |
} |
| 1105 |
|
| 1106 |
|
| 1107 |
static pj_status_t dshow_stream_start(pjmedia_vid_dev_stream *strm) |
| 1108 |
{ |
| 1109 |
struct dshow_stream *stream = (struct dshow_stream*)strm; |
| 1110 |
HRESULT hr; |
| 1111 |
|
| 1112 |
stream->quit_flag = PJ_FALSE; |
| 1113 |
stream->cap_thread_exited = PJ_FALSE; |
| 1114 |
stream->rend_thread_exited = PJ_FALSE; |
| 1115 |
|
| 1116 |
hr = IMediaFilter_Run(stream->dgraph.media_filter, 0); |
| 1117 |
if (FAILED(hr)) { |
| 1118 |
char msg[80]; |
| 1119 |
if (AMGetErrorText(hr, msg, sizeof(msg))) { |
| 1120 |
PJ_LOG(4,(THIS_FILE, "Error starting media: %s", msg)); |
| 1121 |
} |
| 1122 |
return PJ_EUNKNOWN; |
| 1123 |
} |
| 1124 |
|
| 1125 |
PJ_LOG(4, (THIS_FILE, "Starting dshow video stream")); |
| 1126 |
|
| 1127 |
return PJ_SUCCESS; |
| 1128 |
} |
| 1129 |
|
| 1130 |
|
| 1131 |
static pj_status_t dshow_stream_stop(pjmedia_vid_dev_stream *strm) |
| 1132 |
{ |
| 1133 |
struct dshow_stream *stream = (struct dshow_stream*)strm; |
| 1134 |
unsigned i; |
| 1135 |
|
| 1136 |
stream->quit_flag = PJ_TRUE; |
| 1137 |
if (stream->cap_thread_initialized) { |
| 1138 |
for (i=0; !stream->cap_thread_exited && i<100; ++i) |
| 1139 |
pj_thread_sleep(10); |
| 1140 |
} |
| 1141 |
if (stream->param.dir & PJMEDIA_DIR_RENDER) { |
| 1142 |
for (i=0; !stream->rend_thread_exited && i<100; ++i) |
| 1143 |
pj_thread_sleep(10); |
| 1144 |
} |
| 1145 |
|
| 1146 |
if (stream->dgraph.media_filter) |
| 1147 |
IMediaFilter_Stop(stream->dgraph.media_filter); |
| 1148 |
|
| 1149 |
PJ_LOG(4, (THIS_FILE, "Stopping dshow video stream")); |
| 1150 |
|
| 1151 |
return PJ_SUCCESS; |
| 1152 |
} |
| 1153 |
|
| 1154 |
|
| 1155 |
static pj_status_t dshow_stream_destroy(pjmedia_vid_dev_stream *strm) |
| 1156 |
{ |
| 1157 |
struct dshow_stream *stream = (struct dshow_stream*)strm; |
| 1158 |
|
| 1159 |
PJ_ASSERT_RETURN(stream != NULL, PJ_EINVAL); |
| 1160 |
|
| 1161 |
dshow_stream_stop(strm); |
| 1162 |
destroy_filter_graph(stream); |
| 1163 |
|
| 1164 |
pj_pool_release(stream->pool); |
| 1165 |
|
| 1166 |
return PJ_SUCCESS; |
| 1167 |
} |
| 1168 |
|
| 1169 |
#endif |