/**
 * \brief Get current format.
 * \param env JNI environment
 * \param obj object
 * \param native pointer
 * \return current format
 */
JNIEXPORT jobject JNICALL Java_net_java_sip_communicator_impl_neomedia_directshow_DSCaptureDevice_getFormat
  (JNIEnv* env, jobject obj, jlong ptr)
{
    DSCaptureDevice* dev = reinterpret_cast<DSCaptureDevice*>(ptr);
    VideoFormat fmt = dev->getFormat();
    jclass clazzDSFormat = NULL;
    jmethodID initDSFormat = NULL;
    jobject ret = NULL;

    /* get DSFormat class to instantiate some object */
    clazzDSFormat = env->FindClass("net/java/sip/communicator/impl/neomedia/directshow/DSFormat");
    if(clazzDSFormat == NULL)
    {
        return NULL;
    }

    initDSFormat = env->GetMethodID(clazzDSFormat, "<init>", "(IIJ)V");

    if(initDSFormat == NULL)
    {
        return NULL;
    }

    ret = env->NewObject(clazzDSFormat, initDSFormat, static_cast<size_t>(fmt.width),
            static_cast<size_t>(fmt.height), static_cast<jlong>(fmt.pixelFormat));
    return ret;
}
/**
 * \brief Set delegate.
 * \param env JNI environment
 * \param obj object
 * \param ptr native pointer on DSCaptureDevice
 * \param delegate delegate object
 */
JNIEXPORT void JNICALL Java_net_java_sip_communicator_impl_neomedia_directshow_DSCaptureDevice_setDelegate
  (JNIEnv* env, jobject obj, jlong ptr, jobject delegate)
{
    Grabber* grab = NULL;
    DSCaptureDevice* dev = reinterpret_cast<DSCaptureDevice*>(ptr);
    DSGrabberCallback* prev = dev->getCallback();

    if(delegate != NULL)
    {
        delegate = env->NewGlobalRef(delegate);
        if(delegate)
        {
            JavaVM* vm = NULL;
            /* get JavaVM */
            env->GetJavaVM(&vm);
            grab = new Grabber(vm, delegate, dev);
            dev->setCallback(grab);
        }
    }
    else
    {
        dev->setCallback(NULL);
    }

    if(prev)
    {
        jobject tmp_delegate = ((Grabber*)prev)->m_delegate;
        if(tmp_delegate)
        {
            env->DeleteGlobalRef(tmp_delegate);
        }
        delete prev;
    }
}
/**
 * \brief Set delegate.
 * \param env JNI environment
 * \param obj object
 * \param ptr native pointer on DSCaptureDevice
 * \param delegate delegate object
 */
JNIEXPORT void JNICALL Java_org_jitsi_impl_neomedia_jmfext_media_protocol_directshow_DSCaptureDevice_setDelegate
  (JNIEnv* env, jobject obj, jlong ptr, jobject delegate)
{
    SampleGrabberCB* grab = NULL;
    DSCaptureDevice* dev = reinterpret_cast<DSCaptureDevice*>(ptr);
    BasicSampleGrabberCB* prev = dev->getCallback();

    if(delegate != NULL)
    {
        delegate = env->NewGlobalRef(delegate);
        if(delegate)
        {
            JavaVM* vm = NULL;
            /* get JavaVM */
            env->GetJavaVM(&vm);
            grab = new SampleGrabberCB(vm, delegate, dev);
            dev->setCallback(grab);
        }
    }
    else
    {
        dev->setCallback(NULL);
    }

    if(prev)
    {
        jobject tmp_delegate = ((SampleGrabberCB *) prev)->_delegate;
        if(tmp_delegate)
            env->DeleteGlobalRef(tmp_delegate);
        delete prev;
    }
}
/**
 * \brief Get current format.
 * \param env JNI environment
 * \param obj object
 * \param native pointer
 * \return current format
 */
JNIEXPORT jobject JNICALL Java_org_jitsi_impl_neomedia_jmfext_media_protocol_directshow_DSCaptureDevice_getFormat
  (JNIEnv* env, jobject obj, jlong ptr)
{
    DSCaptureDevice* dev = reinterpret_cast<DSCaptureDevice*>(ptr);
    DSFormat fmt = dev->getFormat();
    jclass clazzDSFormat = NULL;
    jmethodID initDSFormat = NULL;

    /* get DSFormat class to instantiate some object */
    clazzDSFormat = env->FindClass("org/jitsi/impl/neomedia/jmfext/media/protocol/directshow/DSFormat");
    if(clazzDSFormat == NULL)
        return NULL;

    initDSFormat = env->GetMethodID(clazzDSFormat, "<init>", "(III)V");

    if(initDSFormat == NULL)
        return NULL;

    return
        env->NewObject(
                clazzDSFormat,
                initDSFormat,
                static_cast<jint>(fmt.width),
                static_cast<jint>(fmt.height),
                static_cast<jint>(fmt.pixelFormat));
}
/**
 * \brief Open native capture device.
 * \param env JNI environment
 * \param obj DSCaptureDevice object
 * \param ptr native pointer of DSCaptureDevice
 */
JNIEXPORT void JNICALL Java_net_java_sip_communicator_impl_neomedia_directshow_DSCaptureDevice_open
  (JNIEnv* env, jobject obj, jlong ptr)
{
    DSCaptureDevice* dev = reinterpret_cast<DSCaptureDevice*>(ptr);

    dev->buildGraph();
    dev->start();
}
JNIEXPORT jint JNICALL
Java_org_jitsi_impl_neomedia_jmfext_media_protocol_directshow_DSCaptureDevice_stop
    (JNIEnv *env, jobject obj, jlong ptr)
{
    DSCaptureDevice *thiz = reinterpret_cast<DSCaptureDevice *>(ptr);

    return (jint) (thiz->stop());
}
/**
 * \brief Connects to the specified capture device.
 * \param env JNI environment
 * \param obj DSCaptureDevice object
 * \param ptr a pointer to a DSCaptureDevice instance to connect to
 */
JNIEXPORT void JNICALL
Java_org_jitsi_impl_neomedia_jmfext_media_protocol_directshow_DSCaptureDevice_connect
    (JNIEnv* env, jobject obj, jlong ptr)
{
    DSCaptureDevice* thiz = reinterpret_cast<DSCaptureDevice*>(ptr);

    thiz->buildGraph();
}
/**
 * \brief Get formats supported by native capture device.
 * \param env JNI environment
 * \param obj DSCaptureDevice object
 * \param ptr native pointer of DSCaptureDevice
 * \return array of DSFormat object
 */
JNIEXPORT jobjectArray JNICALL Java_org_jitsi_impl_neomedia_jmfext_media_protocol_directshow_DSCaptureDevice_getSupportedFormats
  (JNIEnv* env, jobject obj, jlong ptr)
{
    jobjectArray ret = NULL;
    DSCaptureDevice* dev = reinterpret_cast<DSCaptureDevice*>(ptr);
    std::list<DSFormat> formats;
    jclass clazzDSFormat = NULL;
    jmethodID initDSFormat = NULL;
    jsize i = 0;

    /* get DSFormat class to instantiate some object */
    clazzDSFormat = env->FindClass("org/jitsi/impl/neomedia/jmfext/media/protocol/directshow/DSFormat");
    if(clazzDSFormat == NULL)
        return NULL;

    initDSFormat = env->GetMethodID(clazzDSFormat, "<init>", "(III)V");

    if(initDSFormat == NULL)
        return NULL;

    formats = dev->getSupportedFormats();

    ret = env->NewObjectArray(static_cast<jsize>(formats.size()), clazzDSFormat, NULL);
    for(std::list<DSFormat>::iterator it = formats.begin() ; it != formats.end() ; ++it)
    {
        DSFormat tmp = (*it);
        jobject o
            = env->NewObject(
                    clazzDSFormat,
                    initDSFormat,
                    static_cast<jint>(tmp.width),
                    static_cast<jint>(tmp.height),
                    static_cast<jint>(tmp.pixelFormat));

        if(o == NULL)
        {
            fprintf(stderr, "failed!!\n");
            fflush(stderr);
        }
        else
        {

            env->SetObjectArrayElement(ret, i, o);
            env->DeleteLocalRef(o);
            i++;
        }
    }

    return ret;
}
/**
 * \brief Get name of native capture device.
 * \param env JNI environment
 * \param obj DSCaptureDevice object
 * \param ptr native pointer of DSCaptureDevice
 * \return name of the native capture device
 */
JNIEXPORT jstring JNICALL Java_net_java_sip_communicator_impl_neomedia_directshow_DSCaptureDevice_getName
  (JNIEnv* env, jobject obj, jlong ptr)
{
    DSCaptureDevice* dev = reinterpret_cast<DSCaptureDevice*>(ptr);
    jstring ret = NULL;
    jsize len = static_cast<jsize>(wcslen(dev->getName()));
    jchar* name = new jchar[len];

    /* jchar is two bytes! */
    memcpy((void*)name, (void*)dev->getName(), len * 2);

    ret = env->NewString(name, len);
    delete[] name;

    return ret;
}
/**
 * \brief Set format of native capture device.
 * \param env JNI environment
 * \param obj DSCaptureDevice object
 * \param ptr native pointer of DSCaptureDevice
 * \param format DSFormat to set
 */
JNIEXPORT jint JNICALL
Java_org_jitsi_impl_neomedia_jmfext_media_protocol_directshow_DSCaptureDevice_setFormat
    (JNIEnv* env, jobject obj, jlong ptr, jobject format)
{
    DSCaptureDevice* thiz = reinterpret_cast<DSCaptureDevice*>(ptr);
    jclass clazz = env->GetObjectClass(format);
    HRESULT hr;

    if (clazz)
    {
        jfieldID heightFieldID = env->GetFieldID(clazz, "height", "I");

        if (heightFieldID)
        {
            jfieldID widthFieldID = env->GetFieldID(clazz, "width", "I");

            if (widthFieldID)
            {
                jfieldID pixelFormatFieldID
                    = env->GetFieldID(clazz, "pixelFormat", "I");

                if (pixelFormatFieldID)
                {
                    DSFormat format_;

                    format_.height = env->GetIntField(format, heightFieldID);
                    format_.pixelFormat
                        = (DWORD)
                            (env->GetIntField(format, pixelFormatFieldID));
                    format_.width = env->GetIntField(format, widthFieldID);

                    hr = thiz->setFormat(format_);
                }
                else
                    hr = E_FAIL;
            }
            else
                hr = E_FAIL;
        }
        else
            hr = E_FAIL;
    }
    else
        hr = E_FAIL;
    return hr;
}
/**
 * \brief Set format of native capture device.
 * \param env JNI environment
 * \param obj DSCaptureDevice object
 * \param ptr native pointer of DSCaptureDevice
 * \param format DSFormat to set
 */
JNIEXPORT void JNICALL Java_net_java_sip_communicator_impl_neomedia_directshow_DSCaptureDevice_setFormat
  (JNIEnv* env, jobject obj, jlong ptr, jobject format)
{
    DSCaptureDevice* dev = reinterpret_cast<DSCaptureDevice*>(ptr);
    VideoFormat fmt;
    jclass clazz = env->GetObjectClass(format);

    if(clazz)
    {
        jfieldID fieldH = env->GetFieldID(clazz, "height", "I");
        jfieldID fieldW = env->GetFieldID(clazz, "width", "I");
        jfieldID fieldF = env->GetFieldID(clazz, "pixelFormat", "J");
        jlong f = env->GetLongField(format, fieldF);
        jint w = env->GetIntField(format, fieldW);
        jint h = env->GetIntField(format, fieldH);

        fmt.width = w;
        fmt.height = h;
        fmt.pixelFormat = (unsigned long)f;

        dev->setFormat(fmt);
        dev->start();
    }
}
コード例 #12
0
ファイル: DSManager.cpp プロジェクト: xuzhikethinker/libjitsi
void DSManager::initCaptureDevices()
{
    HRESULT ret = 0;
    VARIANT name;
    ICreateDevEnum* devEnum = NULL;
    IEnumMoniker* monikerEnum = NULL;
    IMoniker* moniker = NULL;

    if(m_devices.size() > 0)
    {
        /* clean up our list in case of reinitialization */
        for(std::list<DSCaptureDevice*>::iterator it = m_devices.begin() ; it != m_devices.end() ; ++it)
        {
            delete *it;
        }
        m_devices.clear();
    }

    /* get the available devices list */
    ret = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER,
            IID_ICreateDevEnum, (void**)&devEnum);

    if(FAILED(ret))
    {
        return;
    }

    ret = devEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, 
            &monikerEnum, 0);

    /* error or no devices */
    if(FAILED(ret) || ret == S_FALSE)
    {
        devEnum->Release();
        return;
    }

    /* loop and initialize all available capture devices */
    while(monikerEnum->Next(1, &moniker, 0) == S_OK)
    {
        DSCaptureDevice* captureDevice = NULL;
        IPropertyBag* propertyBag = NULL;

        {
          IBaseFilter* cp = NULL;
          if(!FAILED(moniker->BindToObject(0, 0, IID_IBaseFilter, (void**)&cp)))
          {
            IAMVfwCaptureDialogs* vfw = NULL;
            if(!FAILED(
                  cp->QueryInterface(IID_IAMVfwCaptureDialogs, (void**)&vfw)))
            {
              if(vfw)
              {
                vfw->Release();
                cp->Release();
                continue;
              }
            }
          }
        }

        /* get properties of the device */
        ret = moniker->BindToStorage(0, 0, IID_IPropertyBag, (void**)&propertyBag);
        if(!FAILED(ret))
        {
            VariantInit(&name);

            ret = propertyBag->Read(L"FriendlyName", &name, 0);
            if(FAILED(ret))
            {
                VariantClear(&name);
                propertyBag->Release();
                moniker->Release();
                continue;
            }

            /* create a new capture device */
            captureDevice = new DSCaptureDevice(name.bstrVal);
            /* wprintf(L"%ws\n", name.bstrVal); */

            if(captureDevice && captureDevice->initDevice(moniker))
            {
                /* initialization success, add to the list */
                m_devices.push_back(captureDevice);
            }
            else
            {
                /* printf("failed to initialize device\n"); */
                delete captureDevice;
            }

            /* clean up */
            VariantClear(&name);
            propertyBag->Release();
        }
        moniker->Release();
    }

    /* cleanup */
    monikerEnum->Release();
    devEnum->Release();
}