Exemplo n.º 1
0
int main(int argc, char *argv[])
{
    QOptions options(get_common_options());
    options.add(QLatin1String("QMLPlayer options"))
            ("scale", 1.0, QLatin1String("scale of graphics context. 0: auto"))
            ;
    options.parse(argc, argv);
    if (options.value(QLatin1String("help")).toBool()) {
        options.print();
        return 0;
    }

    QGuiApplication app(argc, argv);
    QDir::setCurrent(qApp->applicationDirPath());
    qDebug() << "arguments======= " << app.arguments();
    set_opengl_backend(options.option(QStringLiteral("gl")).value().toString(), app.arguments().first());
    load_qm(QStringList() << QStringLiteral("QMLPlayer"), options.value(QStringLiteral("language")).toString());
    QtQuick2ApplicationViewer viewer;
    QString binDir = qApp->applicationDirPath();
    if (binDir.endsWith(QLatin1String(".app/Contents/MacOS"))) {
        binDir.remove(QLatin1String(".app/Contents/MacOS"));
        binDir = binDir.left(binDir.lastIndexOf(QLatin1String("/")));
    }
    QQmlEngine *engine = viewer.engine();
    if (!engine->importPathList().contains(binDir))
        engine->addImportPath(binDir);
    qDebug() << engine->importPathList();
    engine->rootContext()->setContextProperty(QStringLiteral("PlayerConfig"), &Config::instance());
    qDebug(">>>>>>>>devicePixelRatio: %f", qApp->devicePixelRatio());
    QScreen *sc = app.primaryScreen();
    qDebug() << "dpi phy: " << sc->physicalDotsPerInch() << ", logical: " << sc->logicalDotsPerInch() << ", dpr: " << sc->devicePixelRatio()
                << "; vis rect:" << sc->virtualGeometry();
    // define a global var for js and qml
    engine->rootContext()->setContextProperty(QStringLiteral("screenPixelDensity"), qApp->primaryScreen()->physicalDotsPerInch()*qApp->primaryScreen()->devicePixelRatio());
    qreal r = sc->physicalDotsPerInch()/sc->logicalDotsPerInch();
    if (std::isinf(r) || std::isnan(r))
#if defined(Q_OS_ANDROID)
        r = 2.0;
#else
        r = 1.0;
#endif
    float sr = options.value(QStringLiteral("scale")).toFloat();
#if defined(Q_OS_ANDROID)
    sr = r;
    if (sr > 2.0)
        sr = 2.0; //FIXME
#endif
    if (qFuzzyIsNull(sr))
        sr = r;
    engine->rootContext()->setContextProperty(QStringLiteral("scaleRatio"), sr);
    QString qml = QStringLiteral("qml/QMLPlayer/main.qml");
    if (QFile(qApp->applicationDirPath() + QLatin1String("/") + qml).exists())
        qml.prepend(qApp->applicationDirPath() + QLatin1String("/"));
    else
        qml.prepend(QLatin1String("qrc:///"));
    viewer.setMainQmlFile(qml);
    viewer.show();
    QOption op = options.option(QStringLiteral("width"));
    if (op.isSet())
        viewer.setWidth(op.value().toInt());
    op = options.option(QStringLiteral("height"));
    if (op.isSet())
        viewer.setHeight(op.value().toInt());
    op = options.option(QStringLiteral("x"));
    if (op.isSet())
        viewer.setX(op.value().toInt());
    op = options.option(QStringLiteral("y"));
    if (op.isSet())
        viewer.setY(op.value().toInt());
    if (options.value(QStringLiteral("fullscreen")).toBool())
        viewer.showFullScreen();

    viewer.setTitle(QStringLiteral("QMLPlayer based on QtAV. [email protected]"));
    /*
     * find root item, then root.init(argv). so we can deal with argv in qml
     */
#if 1
    QString json = app.arguments().join(QStringLiteral("\",\""));
    json.prepend(QLatin1String("[\"")).append(QLatin1String("\"]"));
    json.replace(QLatin1String("\\"), QLatin1String("/")); //FIXME
    QMetaObject::invokeMethod(viewer.rootObject(), "init", Q_ARG(QVariant, json));
//#else
    QObject *player = viewer.rootObject()->findChild<QObject*>(QStringLiteral("player"));
    if (player) {
        AppEventFilter *ae = new AppEventFilter(player, player);
        qApp->installEventFilter(ae);
    }
    QString file;
#ifdef Q_OS_ANDROID
    file = QAndroidJniObject::callStaticObjectMethod("org.qtav.qmlplayer.QMLPlayerActivity"
                                                                              , "getUrl"
                                                                              , "()Ljava/lang/String;")
            .toString();
#endif
    if (app.arguments().size() > 1) {
        file = options.value(QStringLiteral("file")).toString();
        if (file.isEmpty()) {
            if (argc > 1 && !app.arguments().last().startsWith(QLatin1Char('-')) && !app.arguments().at(argc-2).startsWith(QLatin1Char('-')))
                file = app.arguments().last();
        }
    }
    qDebug() << "file: " << file;
    if (player && !file.isEmpty()) {
        if (!file.startsWith(QLatin1String("file:")) && QFile(file).exists())
            file.prepend(QLatin1String("file:")); //qml use url and will add qrc: if no scheme
        file.replace(QLatin1String("\\"), QLatin1String("/")); //qurl
        QMetaObject::invokeMethod(player, "play", Q_ARG(QUrl, QUrl(file)));
    }
#endif
    QObject::connect(viewer.rootObject(), SIGNAL(requestFullScreen()), &viewer, SLOT(showFullScreen()));
    QObject::connect(viewer.rootObject(), SIGNAL(requestNormalSize()), &viewer, SLOT(showNormal()));
    ScreenSaver::instance().disable(); //restore in dtor
    return app.exec();
}
Exemplo n.º 2
0
int main(int argc, char *argv[])
{
    QOptions options(get_common_options());
    options.add("QMLPlayer options")
            ("scale", 1.0, "scale of graphics context. 0: auto")
            ;
    options.parse(argc, argv);
    if (options.value("help").toBool()) {
        options.print();
        return 0;
    }

    QGuiApplication app(argc, argv);
    QtQuick2ApplicationViewer viewer;
    qDebug(">>>>>>>>devicePixelRatio: %f", qApp->devicePixelRatio());
    QScreen *sc = app.primaryScreen();
    qDebug() << "dpi phy: " << sc->physicalDotsPerInch() << ", logical: " << sc->logicalDotsPerInch() << ", dpr: " << sc->devicePixelRatio()
                << "; vis rect:" << sc->virtualGeometry();
    // define a global var for js and qml
    viewer.engine()->rootContext()->setContextProperty("screenPixelDensity", qApp->primaryScreen()->physicalDotsPerInch()*qApp->primaryScreen()->devicePixelRatio());
    qreal r = sc->physicalDotsPerInch()/sc->logicalDotsPerInch();
    if (std::isinf(r) || std::isnan(r))
#if defined(Q_OS_ANDROID)
        r = 2.0;
#else
        r = 1.0;
#endif
    float sr = options.value("scale").toFloat();
    if (qFuzzyIsNull(sr))
        sr = r;
    viewer.engine()->rootContext()->setContextProperty("scaleRatio", sr);
    QString qml = "qml/QMLPlayer/main.qml";
    if (QFile(qApp->applicationDirPath() + "/" + qml).exists())
        qml.prepend(qApp->applicationDirPath() + "/");
    else
        qml.prepend("qrc:///");
    viewer.setMainQmlFile(qml);
    viewer.showExpanded();
    QOption op = options.option("width");
    if (op.isSet())
        viewer.setWidth(op.value().toInt());
    op = options.option("height");
    if (op.isSet())
        viewer.setHeight(op.value().toInt());
    op = options.option("x");
    if (op.isSet())
        viewer.setX(op.value().toInt());
    op = options.option("y");
    if (op.isSet())
        viewer.setY(op.value().toInt());
    if (options.value("fullscreen").toBool())
        viewer.showFullScreen();

    viewer.setTitle("QMLPlayer based on QtAV. [email protected]");
    /*
     * find root item, then root.init(argv). so we can deal with argv in qml
     */
#if 1
    QString json = app.arguments().join("\",\"");
    json.prepend("[\"").append("\"]");
    json.replace("\\", "/"); //FIXME
    QMetaObject::invokeMethod(viewer.rootObject(), "init", Q_ARG(QVariant, json));
//#else
    if (app.arguments().size() > 1) {
        qDebug("arguments > 1");
        QObject *player = viewer.rootObject()->findChild<QObject*>("player");
        QString file = options.value("file").toString();
        if (player && !file.isEmpty()) {
            if (QFile(file).exists())
                file.prepend("file://");
            file.replace("\\", "/"); //qurl
            QMetaObject::invokeMethod(player, "play", Q_ARG(QUrl, QUrl(file)));
        }
    }
#endif
    QObject::connect(viewer.rootObject(), SIGNAL(requestFullScreen()), &viewer, SLOT(showFullScreen()));
    QObject::connect(viewer.rootObject(), SIGNAL(requestNormalSize()), &viewer, SLOT(showNormal()));
    ScreenSaver::instance().disable(); //restore in dtor
    return app.exec();
}
Exemplo n.º 3
0
void Logger(QtMsgType type, const char *msg)
{
#else
void Logger(QtMsgType type, const QMessageLogContext &, const QString& qmsg)
{
    const QByteArray msgArray = qmsg.toLocal8Bit();
    const char* msg = msgArray.constData();
#endif
	 switch (type) {
     case QtDebugMsg:
		 fprintf(stdout, "Debug: %s\n", msg);
         if (sLogfile)
            fprintf(sLogfile, "Debug: %s\n", msg);
         break;
     case QtWarningMsg:
		 fprintf(stdout, "Warning: %s\n", msg);
         if (sLogfile)
            fprintf(sLogfile, "Warning: %s\n", msg);
		 break;
     case QtCriticalMsg:
		 fprintf(stderr, "Critical: %s\n", msg);
         if (sLogfile)
            fprintf(sLogfile, "Critical: %s\n", msg);
		 break;
     case QtFatalMsg:
		 fprintf(stderr, "Fatal: %s\n", msg);
         if (sLogfile)
            fprintf(sLogfile, "Fatal: %s\n", msg);
		 abort();
     }
     fflush(0);
}

int main(int argc, char *argv[])
{
    // has no effect if qInstallMessageHandler() called
    //qSetMessagePattern("%{function} @%{line}: %{message}");

    QOptions options = get_common_options();
    options.add("player options")
            ("-vo", "gl", "video renderer engine. can be gl, qt, d2d, gdi, xv.")
            ("ao", "", "audio output. can be 'null'")
            ("no-ffmpeg-log", "disable ffmpeg log")
            ;
    options.parse(argc, argv);
    if (options.value("help").toBool()) {
        qDebug() << aboutQtAV_PlainText();
        options.print();
        return 0;
    }

    QApplication a(argc, argv);
    load_qm(QStringList() << "player", options.value("language").toString());

    sLogfile = fopen(QString(qApp->applicationDirPath() + "/log.txt").toUtf8().constData(), "w+");
    if (!sLogfile) {
        qWarning("Failed to open log file");
        sLogfile = stdout;
    }
    qInstallMessageHandler(Logger);

    QOption op = options.option("vo");
    QString vo = op.value().toString();
    if (!op.isSet()) {
        QString exe(a.arguments().at(0));
        int i = exe.lastIndexOf('-');
        if (i > 0) {
            vo = exe.mid(i+1, exe.indexOf('.') - i - 1);
        }
    }
    qDebug("vo: %s", vo.toUtf8().constData());
    vo = vo.toLower();
    if (vo != "gl" && vo != "d2d" && vo != "gdi" && vo != "xv" && vo != "qt")
        vo = "gl";
    QString title = "QtAV " /*+ vo + " "*/ + QtAV_Version_String_Long() + " [email protected]";
#ifndef QT_NO_OPENGL
    VideoRendererId vid = VideoRendererId_GLWidget2;
#else
    VideoRendererId vid = VideoRendererId_Widget;
#endif
    // TODO: move to VideoRendererTypes or factory to query name
    struct {
        const char* name;
        VideoRendererId id;
    } vid_map[] = {
    { "gl", VideoRendererId_GLWidget2 },
    { "d2d", VideoRendererId_Direct2D },
    { "gdi", VideoRendererId_GDI },
    { "xv", VideoRendererId_XV },
    { "qt", VideoRendererId_Widget },
    { 0, 0 }
    };
    for (int i = 0; vid_map[i].name; ++i) {
        if (vo == vid_map[i].name) {
            vid = vid_map[i].id;
            break;
        }
    }
    VideoOutput *renderer = new VideoOutput(vid); //or VideoRenderer
    if (!renderer) {
        QMessageBox::critical(0, "QtAV", "vo '" + vo + "' not supported");
        return 1;
    }
    //renderer->scaleInRenderer(false);
    renderer->setOutAspectRatioMode(VideoRenderer::VideoAspectRatio);

    MainWindow window;
    window.show();
    window.setWindowTitle(title);
    window.setRenderer(renderer);
    int w = renderer->widget()->width();
    int h = renderer->widget()->width()*9/16;
    int x = window.x();
    int y = window.y();
    op = options.option("width");
    w = op.value().toInt();
    op = options.option("height");
    h = op.value().toInt();
    op = options.option("x");
    if (op.isSet())
        x = op.value().toInt();
    op = options.option("y");
    if (op.isSet())
        y = op.value().toInt();
    window.resize(w, h);
    window.move(x, y);
    if (options.value("fullscreen").toBool())
        window.showFullScreen();

    window.enableAudio(options.value("ao").toString() != "null");

    op = options.option("vd");
    if (op.isSet()) {
        QStringList vd = op.value().toString().split(";", QString::SkipEmptyParts);
        if (!vd.isEmpty())
            window.setVideoDecoderNames(vd);
    }

    if (options.value("no-ffmpeg-log").toBool())
        setFFmpegLogHandler(0);
    op = options.option("file");
    if (op.isSet()) {
        window.play(op.value().toString());
    }
    int ret = a.exec();
    return ret;
}
Exemplo n.º 4
0
int main(int argc, char *argv[])
{
    qDebug() << aboutQtAV_PlainText();
    QApplication a(argc, argv);
    qDebug() <<a.arguments();
    a.setApplicationName(QString::fromLatin1("Player"));
//    a.setApplicationDisplayName(QString::fromLatin1("QtAV Player"));

    QDir::setCurrent(qApp->applicationDirPath());
    QOptions options = get_common_options();
    options.add(QString::fromLatin1("player options"))
            ("ffmpeg-log",  QString(), QString::fromLatin1("ffmpeg log level. can be: quiet, panic, fatal, error, warn, info, verbose, debug. this can override env 'QTAV_FFMPEG_LOG'"))
            ("-vo",
#ifndef QT_NO_OPENGL
#ifdef Q_OS_ANDROID
             QString::fromLatin1("opengl")
#else
             QString::fromLatin1("gl")
#endif
#else
             QString::fromLatin1("qt")
#endif
             , QString::fromLatin1("video renderer engine. can be gl, qt, d2d, gdi, xv, x11."))
            ;
    options.parse(argc, argv);
    do_common_options(options);
    set_opengl_backend(options.option(QString::fromLatin1("gl")).value().toString(), a.arguments().first());
    load_qm(QStringList() << QString::fromLatin1("player"), options.value(QString::fromLatin1("language")).toString());
    QtAV::setFFmpegLogLevel(options.value(QString::fromLatin1("ffmpeg-log")).toByteArray());

    QOption op = options.option(QString::fromLatin1("vo"));
    QString vo = op.value().toString();
    if (!op.isSet()) {
        QString exe(a.arguments().at(0));
        int i = exe.lastIndexOf(QLatin1Char('-'));
        if (i > 0) {
            vo = exe.mid(i+1, exe.indexOf(QLatin1Char('.')) - i - 1);
        }
    }
    qDebug("vo: %s", vo.toUtf8().constData());
    MainWindow window;
    window.setProperty("rendererId", rendererId_from_opt_name(vo.toLower()));
    window.show();
    window.setWindowTitle(QString::fromLatin1("QtAV %1 [email protected]").arg(QtAV_Version_String_Long()));
    AppEventFilter ae(&window);
    qApp->installEventFilter(&ae);

    int x = window.x();
    int y = window.y();
    op = options.option(QString::fromLatin1("width"));
    int w = op.value().toInt();
    op = options.option(QString::fromLatin1("height"));
    int h = op.value().toInt();
    op = options.option(QString::fromLatin1("x"));
    if (op.isSet())
        x = op.value().toInt();
    op = options.option(QString::fromLatin1("y"));
    if (op.isSet())
        y = op.value().toInt();
    window.resize(w, h);
    window.move(x, y);
    if (options.value(QString::fromLatin1("fullscreen")).toBool())
        window.showFullScreen();

    op = options.option(QString::fromLatin1("ao"));
    if (op.isSet()) {
        QString aos(op.value().toString());
        QStringList ao;
        if (aos.contains(QString::fromLatin1(";")))
            ao = aos.split(QString::fromLatin1(";"), QString::SkipEmptyParts);
        else
            ao = aos.split(QString::fromLatin1(","), QString::SkipEmptyParts);
        window.setAudioBackends(ao);
    }

    op = options.option(QString::fromLatin1("vd"));
    if (op.isSet()) {
        QStringList vd = op.value().toString().split(QString::fromLatin1(";"), QString::SkipEmptyParts);
        if (!vd.isEmpty())
            window.setVideoDecoderNames(vd);
    }
    op = options.option(QString::fromLatin1("file"));
    if (op.isSet()) {
        qDebug() << "-f set: " << op.value().toString();
        window.play(op.value().toString());
    } else {
        if (argc > 1 && !a.arguments().last().startsWith(QLatin1Char('-')) && !a.arguments().at(argc-2).startsWith(QLatin1Char('-')))
            window.play(a.arguments().last());
    }
    int ret = a.exec();
    return ret;
}