├── .gitignore ├── BasicPipeline ├── build.gradle ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src │ └── main │ └── java │ └── org │ └── freedesktop │ └── gstreamer │ └── examples │ ├── BasicPipeline.java │ └── Utils.java ├── BufferProbe ├── build.gradle ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src │ └── main │ └── java │ └── org │ └── freedesktop │ └── gstreamer │ └── examples │ ├── BufferProbe.java │ └── Utils.java ├── Controllers ├── build.gradle ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src │ └── main │ └── java │ └── org │ └── freedesktop │ └── gstreamer │ └── examples │ ├── Controllers.java │ └── Utils.java ├── FXCamera ├── build.gradle ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src │ └── main │ └── java │ └── org │ └── freedesktop │ └── gstreamer │ └── examples │ ├── FXCamera.java │ └── Utils.java ├── FXPlayer ├── build.gradle ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src │ └── main │ └── java │ └── org │ └── freedesktop │ └── gstreamer │ └── examples │ ├── FXPlayer.java │ └── Utils.java ├── HLS ├── build.gradle ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src │ └── main │ ├── java │ └── org │ │ └── freedesktop │ │ └── gstreamer │ │ └── examples │ │ ├── HLS.java │ │ └── Utils.java │ └── resources │ └── public │ └── index.html ├── README.md ├── SwingCamera ├── build.gradle ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src │ └── main │ └── java │ └── org │ └── freedesktop │ └── gstreamer │ └── examples │ ├── SwingCamera.java │ └── Utils.java ├── SwingPlayer ├── build.gradle ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src │ └── main │ └── java │ └── org │ └── freedesktop │ └── gstreamer │ └── examples │ ├── SwingPlayer.java │ └── Utils.java ├── WebRTCSendRecv ├── build.gradle ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src │ └── main │ └── java │ └── org │ └── freedesktop │ └── gstreamer │ └── examples │ ├── Utils.java │ └── WebRTCSendRecv.java └── archive ├── nbactions.xml ├── pom.xml └── src └── main └── java └── org └── freedesktop └── gstreamer └── examples ├── AppSrcToAppSinkExample.java ├── BatchTranscode.java ├── CameraTest.java ├── MultiSinkExample.java ├── PlayBinVideoPlayer.java ├── SimpleVideoComponent.java ├── TestFootageCapture.java ├── TestProcessVideoMP4ToMP4.java ├── TestProcessVideoRTSPToMP4.java └── TextColorControlExample.java /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | 3 | # Package Files # 4 | *.jar 5 | *.war 6 | *.ear 7 | 8 | hs_err_pid* 9 | 10 | .gradle 11 | build/ 12 | 13 | target/ 14 | 15 | # Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored) 16 | !gradle-wrapper.jar 17 | 18 | # Cache of project 19 | .gradletasknamecache 20 | 21 | # Eclipse Files # 22 | .classpath 23 | .project 24 | .settings 25 | 26 | # NetBeans Files # 27 | /nbproject/ 28 | nb-configuration.xml 29 | -------------------------------------------------------------------------------- /BasicPipeline/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java' 2 | apply plugin: 'application' 3 | 4 | 5 | mainClassName = 'org.freedesktop.gstreamer.examples.BasicPipeline' 6 | 7 | repositories { 8 | mavenCentral() 9 | } 10 | 11 | dependencies { 12 | implementation 'net.java.dev.jna:jna:5.10.0' 13 | implementation 'net.java.dev.jna:jna-platform:5.10.0' 14 | implementation 'org.freedesktop.gstreamer:gst1-java-core:1.4.0' 15 | } 16 | -------------------------------------------------------------------------------- /BasicPipeline/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gstreamer-java/gst1-java-examples/0667161ac70d3c8394990e8c010947c3a6aed7fe/BasicPipeline/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /BasicPipeline/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /BasicPipeline/gradlew: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # 4 | # Copyright © 2015-2021 the original authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | # 21 | # Gradle start up script for POSIX generated by Gradle. 22 | # 23 | # Important for running: 24 | # 25 | # (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is 26 | # noncompliant, but you have some other compliant shell such as ksh or 27 | # bash, then to run this script, type that shell name before the whole 28 | # command line, like: 29 | # 30 | # ksh Gradle 31 | # 32 | # Busybox and similar reduced shells will NOT work, because this script 33 | # requires all of these POSIX shell features: 34 | # * functions; 35 | # * expansions «$var», «${var}», «${var:-default}», «${var+SET}», 36 | # «${var#prefix}», «${var%suffix}», and «$( cmd )»; 37 | # * compound commands having a testable exit status, especially «case»; 38 | # * various built-in commands including «command», «set», and «ulimit». 39 | # 40 | # Important for patching: 41 | # 42 | # (2) This script targets any POSIX shell, so it avoids extensions provided 43 | # by Bash, Ksh, etc; in particular arrays are avoided. 44 | # 45 | # The "traditional" practice of packing multiple parameters into a 46 | # space-separated string is a well documented source of bugs and security 47 | # problems, so this is (mostly) avoided, by progressively accumulating 48 | # options in "$@", and eventually passing that to Java. 49 | # 50 | # Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, 51 | # and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; 52 | # see the in-line comments for details. 53 | # 54 | # There are tweaks for specific operating systems such as AIX, CygWin, 55 | # Darwin, MinGW, and NonStop. 56 | # 57 | # (3) This script is generated from the Groovy template 58 | # https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt 59 | # within the Gradle project. 60 | # 61 | # You can find Gradle at https://github.com/gradle/gradle/. 62 | # 63 | ############################################################################## 64 | 65 | # Attempt to set APP_HOME 66 | 67 | # Resolve links: $0 may be a link 68 | app_path=$0 69 | 70 | # Need this for daisy-chained symlinks. 71 | while 72 | APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path 73 | [ -h "$app_path" ] 74 | do 75 | ls=$( ls -ld "$app_path" ) 76 | link=${ls#*' -> '} 77 | case $link in #( 78 | /*) app_path=$link ;; #( 79 | *) app_path=$APP_HOME$link ;; 80 | esac 81 | done 82 | 83 | APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit 84 | 85 | APP_NAME="Gradle" 86 | APP_BASE_NAME=${0##*/} 87 | 88 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 89 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 90 | 91 | # Use the maximum available, or set MAX_FD != -1 to use that value. 92 | MAX_FD=maximum 93 | 94 | warn () { 95 | echo "$*" 96 | } >&2 97 | 98 | die () { 99 | echo 100 | echo "$*" 101 | echo 102 | exit 1 103 | } >&2 104 | 105 | # OS specific support (must be 'true' or 'false'). 106 | cygwin=false 107 | msys=false 108 | darwin=false 109 | nonstop=false 110 | case "$( uname )" in #( 111 | CYGWIN* ) cygwin=true ;; #( 112 | Darwin* ) darwin=true ;; #( 113 | MSYS* | MINGW* ) msys=true ;; #( 114 | NONSTOP* ) nonstop=true ;; 115 | esac 116 | 117 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 118 | 119 | 120 | # Determine the Java command to use to start the JVM. 121 | if [ -n "$JAVA_HOME" ] ; then 122 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 123 | # IBM's JDK on AIX uses strange locations for the executables 124 | JAVACMD=$JAVA_HOME/jre/sh/java 125 | else 126 | JAVACMD=$JAVA_HOME/bin/java 127 | fi 128 | if [ ! -x "$JAVACMD" ] ; then 129 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 130 | 131 | Please set the JAVA_HOME variable in your environment to match the 132 | location of your Java installation." 133 | fi 134 | else 135 | JAVACMD=java 136 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 137 | 138 | Please set the JAVA_HOME variable in your environment to match the 139 | location of your Java installation." 140 | fi 141 | 142 | # Increase the maximum file descriptors if we can. 143 | if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then 144 | case $MAX_FD in #( 145 | max*) 146 | MAX_FD=$( ulimit -H -n ) || 147 | warn "Could not query maximum file descriptor limit" 148 | esac 149 | case $MAX_FD in #( 150 | '' | soft) :;; #( 151 | *) 152 | ulimit -n "$MAX_FD" || 153 | warn "Could not set maximum file descriptor limit to $MAX_FD" 154 | esac 155 | fi 156 | 157 | # Collect all arguments for the java command, stacking in reverse order: 158 | # * args from the command line 159 | # * the main class name 160 | # * -classpath 161 | # * -D...appname settings 162 | # * --module-path (only if needed) 163 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. 164 | 165 | # For Cygwin or MSYS, switch paths to Windows format before running java 166 | if "$cygwin" || "$msys" ; then 167 | APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) 168 | CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) 169 | 170 | JAVACMD=$( cygpath --unix "$JAVACMD" ) 171 | 172 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 173 | for arg do 174 | if 175 | case $arg in #( 176 | -*) false ;; # don't mess with options #( 177 | /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath 178 | [ -e "$t" ] ;; #( 179 | *) false ;; 180 | esac 181 | then 182 | arg=$( cygpath --path --ignore --mixed "$arg" ) 183 | fi 184 | # Roll the args list around exactly as many times as the number of 185 | # args, so each arg winds up back in the position where it started, but 186 | # possibly modified. 187 | # 188 | # NB: a `for` loop captures its iteration list before it begins, so 189 | # changing the positional parameters here affects neither the number of 190 | # iterations, nor the values presented in `arg`. 191 | shift # remove old arg 192 | set -- "$@" "$arg" # push replacement arg 193 | done 194 | fi 195 | 196 | # Collect all arguments for the java command; 197 | # * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of 198 | # shell script including quotes and variable substitutions, so put them in 199 | # double quotes to make sure that they get re-expanded; and 200 | # * put everything else in single quotes, so that it's not re-expanded. 201 | 202 | set -- \ 203 | "-Dorg.gradle.appname=$APP_BASE_NAME" \ 204 | -classpath "$CLASSPATH" \ 205 | org.gradle.wrapper.GradleWrapperMain \ 206 | "$@" 207 | 208 | # Use "xargs" to parse quoted args. 209 | # 210 | # With -n1 it outputs one arg per line, with the quotes and backslashes removed. 211 | # 212 | # In Bash we could simply go: 213 | # 214 | # readarray ARGS < <( xargs -n1 <<<"$var" ) && 215 | # set -- "${ARGS[@]}" "$@" 216 | # 217 | # but POSIX shell has neither arrays nor command substitution, so instead we 218 | # post-process each arg (as a line of input to sed) to backslash-escape any 219 | # character that might be a shell metacharacter, then use eval to reverse 220 | # that process (while maintaining the separation between arguments), and wrap 221 | # the whole thing up as a single "set" statement. 222 | # 223 | # This will of course break if any of these variables contains a newline or 224 | # an unmatched quote. 225 | # 226 | 227 | eval "set -- $( 228 | printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | 229 | xargs -n1 | 230 | sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | 231 | tr '\n' ' ' 232 | )" '"$@"' 233 | 234 | exec "$JAVACMD" "$@" 235 | -------------------------------------------------------------------------------- /BasicPipeline/gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 34 | 35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 37 | 38 | @rem Find java.exe 39 | if defined JAVA_HOME goto findJavaFromJavaHome 40 | 41 | set JAVA_EXE=java.exe 42 | %JAVA_EXE% -version >NUL 2>&1 43 | if "%ERRORLEVEL%" == "0" goto execute 44 | 45 | echo. 46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 47 | echo. 48 | echo Please set the JAVA_HOME variable in your environment to match the 49 | echo location of your Java installation. 50 | 51 | goto fail 52 | 53 | :findJavaFromJavaHome 54 | set JAVA_HOME=%JAVA_HOME:"=% 55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 56 | 57 | if exist "%JAVA_EXE%" goto execute 58 | 59 | echo. 60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 61 | echo. 62 | echo Please set the JAVA_HOME variable in your environment to match the 63 | echo location of your Java installation. 64 | 65 | goto fail 66 | 67 | :execute 68 | @rem Setup the command line 69 | 70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 71 | 72 | 73 | @rem Execute Gradle 74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 75 | 76 | :end 77 | @rem End local scope for the variables with windows NT shell 78 | if "%ERRORLEVEL%"=="0" goto mainEnd 79 | 80 | :fail 81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 82 | rem the _cmd.exe /c_ return code! 83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 84 | exit /b 1 85 | 86 | :mainEnd 87 | if "%OS%"=="Windows_NT" endlocal 88 | 89 | :omega 90 | -------------------------------------------------------------------------------- /BasicPipeline/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'BasicPipeline' 2 | -------------------------------------------------------------------------------- /BasicPipeline/src/main/java/org/freedesktop/gstreamer/examples/BasicPipeline.java: -------------------------------------------------------------------------------- 1 | /* 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 | * 4 | * Copyright 2021 Neil C Smith - Codelerity Ltd. 5 | * 6 | * Copying and distribution of this file, with or without modification, 7 | * are permitted in any medium without royalty provided the copyright 8 | * notice and this notice are preserved. This file is offered as-is, 9 | * without any warranty. 10 | * 11 | */ 12 | package org.freedesktop.gstreamer.examples; 13 | 14 | import java.util.concurrent.TimeUnit; 15 | import org.freedesktop.gstreamer.Gst; 16 | import org.freedesktop.gstreamer.Pipeline; 17 | import org.freedesktop.gstreamer.Version; 18 | 19 | /** 20 | * Simply launches a test GStreamer pipeline using the Java bindings. 21 | * 22 | * @author Neil C Smith ( https://www.codelerity.com ) 23 | */ 24 | public class BasicPipeline { 25 | 26 | /** 27 | * Always store the top-level pipeline reference to stop it being garbage 28 | * collected. 29 | */ 30 | private static Pipeline pipeline; 31 | 32 | /** 33 | * @param args the command line arguments 34 | */ 35 | public static void main(String[] args) { 36 | 37 | /** 38 | * Set up paths to native GStreamer libraries - see adjacent file. 39 | */ 40 | Utils.configurePaths(); 41 | 42 | /** 43 | * Initialize GStreamer. Always pass the lowest version you require - 44 | * Version.BASELINE is GStreamer 1.8. Use Version.of() for higher. 45 | * Features requiring later versions of GStreamer than passed here will 46 | * throw an exception in the bindings even if the actual native library 47 | * is a higher version. 48 | */ 49 | Gst.init(Version.BASELINE, "BasicPipeline", args); 50 | 51 | /** 52 | * Use Gst.parseLaunch() to create a pipeline from a GStreamer string 53 | * definition. This method returns Pipeline when more than one element 54 | * is specified. 55 | */ 56 | pipeline = (Pipeline) Gst.parseLaunch("videotestsrc ! autovideosink"); 57 | 58 | /** 59 | * Start the pipeline. 60 | */ 61 | pipeline.play(); 62 | 63 | /** 64 | * GStreamer native threads will not be taken into account by the JVM 65 | * when deciding whether to shutdown, so we have to keep the main thread 66 | * alive. Gst.main() will keep the calling thread alive until Gst.quit() 67 | * is called. Here we use the built-in executor to schedule a quit after 68 | * 10 seconds. 69 | */ 70 | Gst.getExecutor().schedule(Gst::quit, 10, TimeUnit.SECONDS); 71 | Gst.main(); 72 | 73 | } 74 | 75 | 76 | 77 | } 78 | -------------------------------------------------------------------------------- /BasicPipeline/src/main/java/org/freedesktop/gstreamer/examples/Utils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 | * 4 | * Copyright 2021 Neil C Smith - Codelerity Ltd. 5 | * 6 | * Copying and distribution of this file, with or without modification, 7 | * are permitted in any medium without royalty provided the copyright 8 | * notice and this notice are preserved. This file is offered as-is, 9 | * without any warranty. 10 | * 11 | */ 12 | package org.freedesktop.gstreamer.examples; 13 | 14 | import com.sun.jna.Platform; 15 | import com.sun.jna.platform.win32.Kernel32; 16 | import java.io.File; 17 | import java.util.stream.Stream; 18 | 19 | /** 20 | * Utility methods for use in examples. 21 | */ 22 | class Utils { 23 | 24 | private Utils() { 25 | } 26 | 27 | /** 28 | * Configures paths to the GStreamer libraries. On Windows queries various 29 | * GStreamer environment variables, and then sets up the PATH environment 30 | * variable. On macOS, adds the location to jna.library.path (macOS binaries 31 | * link to each other). On both, the gstreamer.path system property can be 32 | * used to override. On Linux, assumes GStreamer is in the path already. 33 | */ 34 | static void configurePaths() { 35 | if (Platform.isWindows()) { 36 | String gstPath = System.getProperty("gstreamer.path", findWindowsLocation()); 37 | if (!gstPath.isEmpty()) { 38 | String systemPath = System.getenv("PATH"); 39 | if (systemPath == null || systemPath.trim().isEmpty()) { 40 | Kernel32.INSTANCE.SetEnvironmentVariable("PATH", gstPath); 41 | } else { 42 | Kernel32.INSTANCE.SetEnvironmentVariable("PATH", gstPath 43 | + File.pathSeparator + systemPath); 44 | } 45 | } 46 | } else if (Platform.isMac()) { 47 | String gstPath = System.getProperty("gstreamer.path", 48 | "/Library/Frameworks/GStreamer.framework/Libraries/"); 49 | if (!gstPath.isEmpty()) { 50 | String jnaPath = System.getProperty("jna.library.path", "").trim(); 51 | if (jnaPath.isEmpty()) { 52 | System.setProperty("jna.library.path", gstPath); 53 | } else { 54 | System.setProperty("jna.library.path", jnaPath + File.pathSeparator + gstPath); 55 | } 56 | } 57 | 58 | } 59 | } 60 | 61 | /** 62 | * Query over a stream of possible environment variables for GStreamer 63 | * location, filtering on the first non-null result, and adding \bin\ to the 64 | * value. 65 | * 66 | * @return location or empty string 67 | */ 68 | static String findWindowsLocation() { 69 | if (Platform.is64Bit()) { 70 | return Stream.of("GSTREAMER_1_0_ROOT_MSVC_X86_64", 71 | "GSTREAMER_1_0_ROOT_MINGW_X86_64", 72 | "GSTREAMER_1_0_ROOT_X86_64") 73 | .map(System::getenv) 74 | .filter(p -> p != null) 75 | .map(p -> p.endsWith("\\") ? p + "bin\\" : p + "\\bin\\") 76 | .findFirst().orElse(""); 77 | } else { 78 | return ""; 79 | } 80 | } 81 | 82 | } 83 | -------------------------------------------------------------------------------- /BufferProbe/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java' 2 | apply plugin: 'application' 3 | 4 | 5 | mainClassName = 'org.freedesktop.gstreamer.examples.BufferProbe' 6 | 7 | repositories { 8 | mavenCentral() 9 | } 10 | 11 | dependencies { 12 | implementation 'net.java.dev.jna:jna:5.10.0' 13 | implementation 'net.java.dev.jna:jna-platform:5.10.0' 14 | implementation 'org.freedesktop.gstreamer:gst1-java-core:1.4.0' 15 | } 16 | -------------------------------------------------------------------------------- /BufferProbe/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gstreamer-java/gst1-java-examples/0667161ac70d3c8394990e8c010947c3a6aed7fe/BufferProbe/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /BufferProbe/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /BufferProbe/gradlew: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # 4 | # Copyright © 2015-2021 the original authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | # 21 | # Gradle start up script for POSIX generated by Gradle. 22 | # 23 | # Important for running: 24 | # 25 | # (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is 26 | # noncompliant, but you have some other compliant shell such as ksh or 27 | # bash, then to run this script, type that shell name before the whole 28 | # command line, like: 29 | # 30 | # ksh Gradle 31 | # 32 | # Busybox and similar reduced shells will NOT work, because this script 33 | # requires all of these POSIX shell features: 34 | # * functions; 35 | # * expansions «$var», «${var}», «${var:-default}», «${var+SET}», 36 | # «${var#prefix}», «${var%suffix}», and «$( cmd )»; 37 | # * compound commands having a testable exit status, especially «case»; 38 | # * various built-in commands including «command», «set», and «ulimit». 39 | # 40 | # Important for patching: 41 | # 42 | # (2) This script targets any POSIX shell, so it avoids extensions provided 43 | # by Bash, Ksh, etc; in particular arrays are avoided. 44 | # 45 | # The "traditional" practice of packing multiple parameters into a 46 | # space-separated string is a well documented source of bugs and security 47 | # problems, so this is (mostly) avoided, by progressively accumulating 48 | # options in "$@", and eventually passing that to Java. 49 | # 50 | # Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, 51 | # and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; 52 | # see the in-line comments for details. 53 | # 54 | # There are tweaks for specific operating systems such as AIX, CygWin, 55 | # Darwin, MinGW, and NonStop. 56 | # 57 | # (3) This script is generated from the Groovy template 58 | # https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt 59 | # within the Gradle project. 60 | # 61 | # You can find Gradle at https://github.com/gradle/gradle/. 62 | # 63 | ############################################################################## 64 | 65 | # Attempt to set APP_HOME 66 | 67 | # Resolve links: $0 may be a link 68 | app_path=$0 69 | 70 | # Need this for daisy-chained symlinks. 71 | while 72 | APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path 73 | [ -h "$app_path" ] 74 | do 75 | ls=$( ls -ld "$app_path" ) 76 | link=${ls#*' -> '} 77 | case $link in #( 78 | /*) app_path=$link ;; #( 79 | *) app_path=$APP_HOME$link ;; 80 | esac 81 | done 82 | 83 | APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit 84 | 85 | APP_NAME="Gradle" 86 | APP_BASE_NAME=${0##*/} 87 | 88 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 89 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 90 | 91 | # Use the maximum available, or set MAX_FD != -1 to use that value. 92 | MAX_FD=maximum 93 | 94 | warn () { 95 | echo "$*" 96 | } >&2 97 | 98 | die () { 99 | echo 100 | echo "$*" 101 | echo 102 | exit 1 103 | } >&2 104 | 105 | # OS specific support (must be 'true' or 'false'). 106 | cygwin=false 107 | msys=false 108 | darwin=false 109 | nonstop=false 110 | case "$( uname )" in #( 111 | CYGWIN* ) cygwin=true ;; #( 112 | Darwin* ) darwin=true ;; #( 113 | MSYS* | MINGW* ) msys=true ;; #( 114 | NONSTOP* ) nonstop=true ;; 115 | esac 116 | 117 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 118 | 119 | 120 | # Determine the Java command to use to start the JVM. 121 | if [ -n "$JAVA_HOME" ] ; then 122 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 123 | # IBM's JDK on AIX uses strange locations for the executables 124 | JAVACMD=$JAVA_HOME/jre/sh/java 125 | else 126 | JAVACMD=$JAVA_HOME/bin/java 127 | fi 128 | if [ ! -x "$JAVACMD" ] ; then 129 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 130 | 131 | Please set the JAVA_HOME variable in your environment to match the 132 | location of your Java installation." 133 | fi 134 | else 135 | JAVACMD=java 136 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 137 | 138 | Please set the JAVA_HOME variable in your environment to match the 139 | location of your Java installation." 140 | fi 141 | 142 | # Increase the maximum file descriptors if we can. 143 | if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then 144 | case $MAX_FD in #( 145 | max*) 146 | MAX_FD=$( ulimit -H -n ) || 147 | warn "Could not query maximum file descriptor limit" 148 | esac 149 | case $MAX_FD in #( 150 | '' | soft) :;; #( 151 | *) 152 | ulimit -n "$MAX_FD" || 153 | warn "Could not set maximum file descriptor limit to $MAX_FD" 154 | esac 155 | fi 156 | 157 | # Collect all arguments for the java command, stacking in reverse order: 158 | # * args from the command line 159 | # * the main class name 160 | # * -classpath 161 | # * -D...appname settings 162 | # * --module-path (only if needed) 163 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. 164 | 165 | # For Cygwin or MSYS, switch paths to Windows format before running java 166 | if "$cygwin" || "$msys" ; then 167 | APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) 168 | CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) 169 | 170 | JAVACMD=$( cygpath --unix "$JAVACMD" ) 171 | 172 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 173 | for arg do 174 | if 175 | case $arg in #( 176 | -*) false ;; # don't mess with options #( 177 | /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath 178 | [ -e "$t" ] ;; #( 179 | *) false ;; 180 | esac 181 | then 182 | arg=$( cygpath --path --ignore --mixed "$arg" ) 183 | fi 184 | # Roll the args list around exactly as many times as the number of 185 | # args, so each arg winds up back in the position where it started, but 186 | # possibly modified. 187 | # 188 | # NB: a `for` loop captures its iteration list before it begins, so 189 | # changing the positional parameters here affects neither the number of 190 | # iterations, nor the values presented in `arg`. 191 | shift # remove old arg 192 | set -- "$@" "$arg" # push replacement arg 193 | done 194 | fi 195 | 196 | # Collect all arguments for the java command; 197 | # * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of 198 | # shell script including quotes and variable substitutions, so put them in 199 | # double quotes to make sure that they get re-expanded; and 200 | # * put everything else in single quotes, so that it's not re-expanded. 201 | 202 | set -- \ 203 | "-Dorg.gradle.appname=$APP_BASE_NAME" \ 204 | -classpath "$CLASSPATH" \ 205 | org.gradle.wrapper.GradleWrapperMain \ 206 | "$@" 207 | 208 | # Use "xargs" to parse quoted args. 209 | # 210 | # With -n1 it outputs one arg per line, with the quotes and backslashes removed. 211 | # 212 | # In Bash we could simply go: 213 | # 214 | # readarray ARGS < <( xargs -n1 <<<"$var" ) && 215 | # set -- "${ARGS[@]}" "$@" 216 | # 217 | # but POSIX shell has neither arrays nor command substitution, so instead we 218 | # post-process each arg (as a line of input to sed) to backslash-escape any 219 | # character that might be a shell metacharacter, then use eval to reverse 220 | # that process (while maintaining the separation between arguments), and wrap 221 | # the whole thing up as a single "set" statement. 222 | # 223 | # This will of course break if any of these variables contains a newline or 224 | # an unmatched quote. 225 | # 226 | 227 | eval "set -- $( 228 | printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | 229 | xargs -n1 | 230 | sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | 231 | tr '\n' ' ' 232 | )" '"$@"' 233 | 234 | exec "$JAVACMD" "$@" 235 | -------------------------------------------------------------------------------- /BufferProbe/gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 34 | 35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 37 | 38 | @rem Find java.exe 39 | if defined JAVA_HOME goto findJavaFromJavaHome 40 | 41 | set JAVA_EXE=java.exe 42 | %JAVA_EXE% -version >NUL 2>&1 43 | if "%ERRORLEVEL%" == "0" goto execute 44 | 45 | echo. 46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 47 | echo. 48 | echo Please set the JAVA_HOME variable in your environment to match the 49 | echo location of your Java installation. 50 | 51 | goto fail 52 | 53 | :findJavaFromJavaHome 54 | set JAVA_HOME=%JAVA_HOME:"=% 55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 56 | 57 | if exist "%JAVA_EXE%" goto execute 58 | 59 | echo. 60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 61 | echo. 62 | echo Please set the JAVA_HOME variable in your environment to match the 63 | echo location of your Java installation. 64 | 65 | goto fail 66 | 67 | :execute 68 | @rem Setup the command line 69 | 70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 71 | 72 | 73 | @rem Execute Gradle 74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 75 | 76 | :end 77 | @rem End local scope for the variables with windows NT shell 78 | if "%ERRORLEVEL%"=="0" goto mainEnd 79 | 80 | :fail 81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 82 | rem the _cmd.exe /c_ return code! 83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 84 | exit /b 1 85 | 86 | :mainEnd 87 | if "%OS%"=="Windows_NT" endlocal 88 | 89 | :omega 90 | -------------------------------------------------------------------------------- /BufferProbe/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'BufferProbe' 2 | -------------------------------------------------------------------------------- /BufferProbe/src/main/java/org/freedesktop/gstreamer/examples/BufferProbe.java: -------------------------------------------------------------------------------- 1 | /* 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 | * 4 | * Copyright 2021 Neil C Smith - Codelerity Ltd. 5 | * 6 | * Copying and distribution of this file, with or without modification, 7 | * are permitted in any medium without royalty provided the copyright 8 | * notice and this notice are preserved. This file is offered as-is, 9 | * without any warranty. 10 | * 11 | */ 12 | package org.freedesktop.gstreamer.examples; 13 | 14 | import java.awt.Color; 15 | import java.awt.GradientPaint; 16 | import java.awt.Graphics2D; 17 | import java.awt.Paint; 18 | import java.awt.RenderingHints; 19 | import java.awt.geom.AffineTransform; 20 | import java.awt.geom.GeneralPath; 21 | import java.awt.image.BufferedImage; 22 | import java.awt.image.DataBufferInt; 23 | import java.nio.ByteOrder; 24 | import java.nio.IntBuffer; 25 | import org.freedesktop.gstreamer.Buffer; 26 | import org.freedesktop.gstreamer.Bus; 27 | import org.freedesktop.gstreamer.Element; 28 | import org.freedesktop.gstreamer.Gst; 29 | import org.freedesktop.gstreamer.Pad; 30 | import org.freedesktop.gstreamer.PadProbeInfo; 31 | import org.freedesktop.gstreamer.PadProbeReturn; 32 | import org.freedesktop.gstreamer.PadProbeType; 33 | import org.freedesktop.gstreamer.Pipeline; 34 | import org.freedesktop.gstreamer.Version; 35 | 36 | /** 37 | * Launches a GStreamer pipeline and uses a buffer probe to draw an animation on 38 | * top of the video stream using Java2D. 39 | * 40 | * @author Neil C Smith ( https://www.codelerity.com ) 41 | */ 42 | public class BufferProbe { 43 | 44 | private static final int WIDTH = 800; 45 | private static final int HEIGHT = 600; 46 | 47 | /** 48 | * Use a Pad.PROBE and Java2D to draw on to the buffers passing through the 49 | * pipeline. 50 | */ 51 | private static Pipeline pipeline; 52 | 53 | /** 54 | * @param args the command line arguments 55 | */ 56 | public static void main(String[] args) { 57 | 58 | /** 59 | * Set up paths to native GStreamer libraries - see adjacent file. 60 | */ 61 | Utils.configurePaths(); 62 | 63 | /** 64 | * Initialize GStreamer. Always pass the lowest version you require - 65 | * Version.BASELINE is GStreamer 1.8. Use Version.of() for higher. 66 | * Features requiring later versions of GStreamer than passed here will 67 | * throw an exception in the bindings even if the actual native library 68 | * is a higher version. 69 | */ 70 | Gst.init(Version.BASELINE, "BufferProbe", args); 71 | 72 | /** 73 | * Set up a Caps string with the width, height and buffer format 74 | * required for reading and writing into the BufferedImage. 75 | */ 76 | String caps = "video/x-raw, width=" + WIDTH + ", height=" + HEIGHT 77 | + ", pixel-aspect-ratio=1/1, " 78 | + (ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN 79 | ? "format=BGRx" : "format=xRGB"); 80 | 81 | /** 82 | * Use Gst.parseLaunch() to create a pipeline from a GStreamer string 83 | * definition. This method returns Pipeline when more than one element 84 | * is specified. 85 | * 86 | * The named Identity element can be acquired from the pipeline by name 87 | * and the probe attached to its sink pad. 88 | */ 89 | pipeline = (Pipeline) Gst.parseLaunch("autovideosrc ! videoconvert ! videoscale ! " 90 | + caps + " ! identity name=identity ! videoconvert ! autovideosink"); 91 | Element identity = pipeline.getElementByName("identity"); 92 | identity.getStaticPad("sink") 93 | .addProbe(PadProbeType.BUFFER, new Renderer(WIDTH, HEIGHT)); 94 | 95 | /** 96 | * Start the pipeline. Attach a bus listener to call Gst.quit on EOS or 97 | * error. 98 | */ 99 | pipeline.getBus().connect((Bus.ERROR) ((source, code, message) -> { 100 | System.out.println(message); 101 | Gst.quit(); 102 | })); 103 | pipeline.getBus().connect((Bus.EOS) (source) -> Gst.quit()); 104 | pipeline.play(); 105 | Gst.main(); 106 | 107 | } 108 | 109 | /** 110 | * A Pad.PROBE implementation that acquires the Buffer, reads it into the 111 | * data array of a BufferedImage, renders an animation on top, and writes 112 | * back into the Buffer. 113 | */ 114 | static class Renderer implements Pad.PROBE { 115 | 116 | private final BufferedImage image; 117 | private final int[] data; 118 | private final Point[] points; 119 | private final Paint fill; 120 | 121 | private Renderer(int width, int height) { 122 | image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 123 | data = ((DataBufferInt) (image.getRaster().getDataBuffer())).getData(); 124 | points = new Point[18]; 125 | for (int i = 0; i < points.length; i++) { 126 | points[i] = new Point(); 127 | } 128 | fill = new GradientPaint(0, 0, new Color(1.0f, 0.3f, 0.5f, 0.9f), 129 | 60, 20, new Color(0.3f, 1.0f, 0.7f, 0.8f), true); 130 | } 131 | 132 | @Override 133 | public PadProbeReturn probeCallback(Pad pad, PadProbeInfo info) { 134 | Buffer buffer = info.getBuffer(); 135 | if (buffer.isWritable()) { 136 | IntBuffer ib = buffer.map(true).asIntBuffer(); 137 | ib.get(data); 138 | render(); 139 | ib.rewind(); 140 | ib.put(data); 141 | buffer.unmap(); 142 | } 143 | return PadProbeReturn.OK; 144 | } 145 | 146 | private void render() { 147 | Graphics2D g2d = image.createGraphics(); 148 | g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 149 | RenderingHints.VALUE_ANTIALIAS_ON); 150 | for (Point point : points) { 151 | point.tick(); 152 | } 153 | GeneralPath path = new GeneralPath(); 154 | path.moveTo(points[0].x, points[0].y); 155 | for (int i = 2; i < points.length; i += 2) { 156 | path.quadTo(points[i - 1].x, points[i - 1].y, 157 | points[i].x, points[i].y); 158 | } 159 | path.closePath(); 160 | path.transform(AffineTransform.getScaleInstance(image.getWidth(), image.getHeight())); 161 | g2d.setPaint(fill); 162 | g2d.fill(path); 163 | g2d.setColor(Color.BLACK); 164 | g2d.draw(path); 165 | } 166 | 167 | } 168 | 169 | static class Point { 170 | 171 | private double x, y, dx, dy; 172 | 173 | private Point() { 174 | this.x = Math.random(); 175 | this.y = Math.random(); 176 | this.dx = 0.02 * Math.random(); 177 | this.dy = 0.02 * Math.random(); 178 | } 179 | 180 | private void tick() { 181 | x += dx; 182 | y += dy; 183 | if (x < 0 || x > 1) { 184 | dx = -dx; 185 | } 186 | if (y < 0 || y > 1) { 187 | dy = -dy; 188 | } 189 | } 190 | 191 | } 192 | 193 | } 194 | -------------------------------------------------------------------------------- /BufferProbe/src/main/java/org/freedesktop/gstreamer/examples/Utils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 | * 4 | * Copyright 2021 Neil C Smith - Codelerity Ltd. 5 | * 6 | * Copying and distribution of this file, with or without modification, 7 | * are permitted in any medium without royalty provided the copyright 8 | * notice and this notice are preserved. This file is offered as-is, 9 | * without any warranty. 10 | * 11 | */ 12 | package org.freedesktop.gstreamer.examples; 13 | 14 | import com.sun.jna.Platform; 15 | import com.sun.jna.platform.win32.Kernel32; 16 | import java.io.File; 17 | import java.util.stream.Stream; 18 | 19 | /** 20 | * Utility methods for use in examples. 21 | */ 22 | class Utils { 23 | 24 | private Utils() { 25 | } 26 | 27 | /** 28 | * Configures paths to the GStreamer libraries. On Windows queries various 29 | * GStreamer environment variables, and then sets up the PATH environment 30 | * variable. On macOS, adds the location to jna.library.path (macOS binaries 31 | * link to each other). On both, the gstreamer.path system property can be 32 | * used to override. On Linux, assumes GStreamer is in the path already. 33 | */ 34 | static void configurePaths() { 35 | if (Platform.isWindows()) { 36 | String gstPath = System.getProperty("gstreamer.path", findWindowsLocation()); 37 | if (!gstPath.isEmpty()) { 38 | String systemPath = System.getenv("PATH"); 39 | if (systemPath == null || systemPath.trim().isEmpty()) { 40 | Kernel32.INSTANCE.SetEnvironmentVariable("PATH", gstPath); 41 | } else { 42 | Kernel32.INSTANCE.SetEnvironmentVariable("PATH", gstPath 43 | + File.pathSeparator + systemPath); 44 | } 45 | } 46 | } else if (Platform.isMac()) { 47 | String gstPath = System.getProperty("gstreamer.path", 48 | "/Library/Frameworks/GStreamer.framework/Libraries/"); 49 | if (!gstPath.isEmpty()) { 50 | String jnaPath = System.getProperty("jna.library.path", "").trim(); 51 | if (jnaPath.isEmpty()) { 52 | System.setProperty("jna.library.path", gstPath); 53 | } else { 54 | System.setProperty("jna.library.path", jnaPath + File.pathSeparator + gstPath); 55 | } 56 | } 57 | 58 | } 59 | } 60 | 61 | /** 62 | * Query over a stream of possible environment variables for GStreamer 63 | * location, filtering on the first non-null result, and adding \bin\ to the 64 | * value. 65 | * 66 | * @return location or empty string 67 | */ 68 | static String findWindowsLocation() { 69 | if (Platform.is64Bit()) { 70 | return Stream.of("GSTREAMER_1_0_ROOT_MSVC_X86_64", 71 | "GSTREAMER_1_0_ROOT_MINGW_X86_64", 72 | "GSTREAMER_1_0_ROOT_X86_64") 73 | .map(System::getenv) 74 | .filter(p -> p != null) 75 | .map(p -> p.endsWith("\\") ? p + "bin\\" : p + "\\bin\\") 76 | .findFirst().orElse(""); 77 | } else { 78 | return ""; 79 | } 80 | } 81 | 82 | } 83 | -------------------------------------------------------------------------------- /Controllers/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java' 2 | apply plugin: 'application' 3 | 4 | 5 | mainClassName = 'org.freedesktop.gstreamer.examples.Controllers' 6 | 7 | repositories { 8 | mavenCentral() 9 | } 10 | 11 | dependencies { 12 | implementation 'net.java.dev.jna:jna:5.10.0' 13 | implementation 'net.java.dev.jna:jna-platform:5.10.0' 14 | implementation 'org.freedesktop.gstreamer:gst1-java-core:1.4.0' 15 | } 16 | -------------------------------------------------------------------------------- /Controllers/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gstreamer-java/gst1-java-examples/0667161ac70d3c8394990e8c010947c3a6aed7fe/Controllers/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /Controllers/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /Controllers/gradlew: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # 4 | # Copyright © 2015-2021 the original authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | # 21 | # Gradle start up script for POSIX generated by Gradle. 22 | # 23 | # Important for running: 24 | # 25 | # (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is 26 | # noncompliant, but you have some other compliant shell such as ksh or 27 | # bash, then to run this script, type that shell name before the whole 28 | # command line, like: 29 | # 30 | # ksh Gradle 31 | # 32 | # Busybox and similar reduced shells will NOT work, because this script 33 | # requires all of these POSIX shell features: 34 | # * functions; 35 | # * expansions «$var», «${var}», «${var:-default}», «${var+SET}», 36 | # «${var#prefix}», «${var%suffix}», and «$( cmd )»; 37 | # * compound commands having a testable exit status, especially «case»; 38 | # * various built-in commands including «command», «set», and «ulimit». 39 | # 40 | # Important for patching: 41 | # 42 | # (2) This script targets any POSIX shell, so it avoids extensions provided 43 | # by Bash, Ksh, etc; in particular arrays are avoided. 44 | # 45 | # The "traditional" practice of packing multiple parameters into a 46 | # space-separated string is a well documented source of bugs and security 47 | # problems, so this is (mostly) avoided, by progressively accumulating 48 | # options in "$@", and eventually passing that to Java. 49 | # 50 | # Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, 51 | # and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; 52 | # see the in-line comments for details. 53 | # 54 | # There are tweaks for specific operating systems such as AIX, CygWin, 55 | # Darwin, MinGW, and NonStop. 56 | # 57 | # (3) This script is generated from the Groovy template 58 | # https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt 59 | # within the Gradle project. 60 | # 61 | # You can find Gradle at https://github.com/gradle/gradle/. 62 | # 63 | ############################################################################## 64 | 65 | # Attempt to set APP_HOME 66 | 67 | # Resolve links: $0 may be a link 68 | app_path=$0 69 | 70 | # Need this for daisy-chained symlinks. 71 | while 72 | APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path 73 | [ -h "$app_path" ] 74 | do 75 | ls=$( ls -ld "$app_path" ) 76 | link=${ls#*' -> '} 77 | case $link in #( 78 | /*) app_path=$link ;; #( 79 | *) app_path=$APP_HOME$link ;; 80 | esac 81 | done 82 | 83 | APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit 84 | 85 | APP_NAME="Gradle" 86 | APP_BASE_NAME=${0##*/} 87 | 88 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 89 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 90 | 91 | # Use the maximum available, or set MAX_FD != -1 to use that value. 92 | MAX_FD=maximum 93 | 94 | warn () { 95 | echo "$*" 96 | } >&2 97 | 98 | die () { 99 | echo 100 | echo "$*" 101 | echo 102 | exit 1 103 | } >&2 104 | 105 | # OS specific support (must be 'true' or 'false'). 106 | cygwin=false 107 | msys=false 108 | darwin=false 109 | nonstop=false 110 | case "$( uname )" in #( 111 | CYGWIN* ) cygwin=true ;; #( 112 | Darwin* ) darwin=true ;; #( 113 | MSYS* | MINGW* ) msys=true ;; #( 114 | NONSTOP* ) nonstop=true ;; 115 | esac 116 | 117 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 118 | 119 | 120 | # Determine the Java command to use to start the JVM. 121 | if [ -n "$JAVA_HOME" ] ; then 122 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 123 | # IBM's JDK on AIX uses strange locations for the executables 124 | JAVACMD=$JAVA_HOME/jre/sh/java 125 | else 126 | JAVACMD=$JAVA_HOME/bin/java 127 | fi 128 | if [ ! -x "$JAVACMD" ] ; then 129 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 130 | 131 | Please set the JAVA_HOME variable in your environment to match the 132 | location of your Java installation." 133 | fi 134 | else 135 | JAVACMD=java 136 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 137 | 138 | Please set the JAVA_HOME variable in your environment to match the 139 | location of your Java installation." 140 | fi 141 | 142 | # Increase the maximum file descriptors if we can. 143 | if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then 144 | case $MAX_FD in #( 145 | max*) 146 | MAX_FD=$( ulimit -H -n ) || 147 | warn "Could not query maximum file descriptor limit" 148 | esac 149 | case $MAX_FD in #( 150 | '' | soft) :;; #( 151 | *) 152 | ulimit -n "$MAX_FD" || 153 | warn "Could not set maximum file descriptor limit to $MAX_FD" 154 | esac 155 | fi 156 | 157 | # Collect all arguments for the java command, stacking in reverse order: 158 | # * args from the command line 159 | # * the main class name 160 | # * -classpath 161 | # * -D...appname settings 162 | # * --module-path (only if needed) 163 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. 164 | 165 | # For Cygwin or MSYS, switch paths to Windows format before running java 166 | if "$cygwin" || "$msys" ; then 167 | APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) 168 | CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) 169 | 170 | JAVACMD=$( cygpath --unix "$JAVACMD" ) 171 | 172 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 173 | for arg do 174 | if 175 | case $arg in #( 176 | -*) false ;; # don't mess with options #( 177 | /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath 178 | [ -e "$t" ] ;; #( 179 | *) false ;; 180 | esac 181 | then 182 | arg=$( cygpath --path --ignore --mixed "$arg" ) 183 | fi 184 | # Roll the args list around exactly as many times as the number of 185 | # args, so each arg winds up back in the position where it started, but 186 | # possibly modified. 187 | # 188 | # NB: a `for` loop captures its iteration list before it begins, so 189 | # changing the positional parameters here affects neither the number of 190 | # iterations, nor the values presented in `arg`. 191 | shift # remove old arg 192 | set -- "$@" "$arg" # push replacement arg 193 | done 194 | fi 195 | 196 | # Collect all arguments for the java command; 197 | # * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of 198 | # shell script including quotes and variable substitutions, so put them in 199 | # double quotes to make sure that they get re-expanded; and 200 | # * put everything else in single quotes, so that it's not re-expanded. 201 | 202 | set -- \ 203 | "-Dorg.gradle.appname=$APP_BASE_NAME" \ 204 | -classpath "$CLASSPATH" \ 205 | org.gradle.wrapper.GradleWrapperMain \ 206 | "$@" 207 | 208 | # Use "xargs" to parse quoted args. 209 | # 210 | # With -n1 it outputs one arg per line, with the quotes and backslashes removed. 211 | # 212 | # In Bash we could simply go: 213 | # 214 | # readarray ARGS < <( xargs -n1 <<<"$var" ) && 215 | # set -- "${ARGS[@]}" "$@" 216 | # 217 | # but POSIX shell has neither arrays nor command substitution, so instead we 218 | # post-process each arg (as a line of input to sed) to backslash-escape any 219 | # character that might be a shell metacharacter, then use eval to reverse 220 | # that process (while maintaining the separation between arguments), and wrap 221 | # the whole thing up as a single "set" statement. 222 | # 223 | # This will of course break if any of these variables contains a newline or 224 | # an unmatched quote. 225 | # 226 | 227 | eval "set -- $( 228 | printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | 229 | xargs -n1 | 230 | sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | 231 | tr '\n' ' ' 232 | )" '"$@"' 233 | 234 | exec "$JAVACMD" "$@" 235 | -------------------------------------------------------------------------------- /Controllers/gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 34 | 35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 37 | 38 | @rem Find java.exe 39 | if defined JAVA_HOME goto findJavaFromJavaHome 40 | 41 | set JAVA_EXE=java.exe 42 | %JAVA_EXE% -version >NUL 2>&1 43 | if "%ERRORLEVEL%" == "0" goto execute 44 | 45 | echo. 46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 47 | echo. 48 | echo Please set the JAVA_HOME variable in your environment to match the 49 | echo location of your Java installation. 50 | 51 | goto fail 52 | 53 | :findJavaFromJavaHome 54 | set JAVA_HOME=%JAVA_HOME:"=% 55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 56 | 57 | if exist "%JAVA_EXE%" goto execute 58 | 59 | echo. 60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 61 | echo. 62 | echo Please set the JAVA_HOME variable in your environment to match the 63 | echo location of your Java installation. 64 | 65 | goto fail 66 | 67 | :execute 68 | @rem Setup the command line 69 | 70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 71 | 72 | 73 | @rem Execute Gradle 74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 75 | 76 | :end 77 | @rem End local scope for the variables with windows NT shell 78 | if "%ERRORLEVEL%"=="0" goto mainEnd 79 | 80 | :fail 81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 82 | rem the _cmd.exe /c_ return code! 83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 84 | exit /b 1 85 | 86 | :mainEnd 87 | if "%OS%"=="Windows_NT" endlocal 88 | 89 | :omega 90 | -------------------------------------------------------------------------------- /Controllers/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'Controllers' 2 | -------------------------------------------------------------------------------- /Controllers/src/main/java/org/freedesktop/gstreamer/examples/Controllers.java: -------------------------------------------------------------------------------- 1 | /* 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 | * 4 | * Copyright 2021 Neil C Smith - Codelerity Ltd. / Tim-Philipp Müller 5 | * 6 | * Copying and distribution of this file, with or without modification, 7 | * are permitted in any medium without royalty provided the copyright 8 | * notice and this notice are preserved. This file is offered as-is, 9 | * without any warranty. 10 | * 11 | */ 12 | package org.freedesktop.gstreamer.examples; 13 | 14 | import java.util.concurrent.TimeUnit; 15 | import org.freedesktop.gstreamer.Element; 16 | import org.freedesktop.gstreamer.ElementFactory; 17 | import org.freedesktop.gstreamer.Gst; 18 | import org.freedesktop.gstreamer.Pipeline; 19 | import org.freedesktop.gstreamer.Version; 20 | import org.freedesktop.gstreamer.controller.ARGBControlBinding; 21 | import org.freedesktop.gstreamer.controller.DirectControlBinding; 22 | import org.freedesktop.gstreamer.controller.LFOControlSource; 23 | 24 | /** 25 | * Builds a pipeline with videotestsrc and textoverlay, and uses control sources 26 | * to modulate text colour and position. 27 | *

28 | * Adapted from the C example by Tim-Philipp Müller at 29 | * 30 | * https://gitlab.freedesktop.org/gstreamer/gstreamer/blob/master/tests/examples/controller/text-color-example.c 31 | * 32 | * @author Neil C Smith ( https://www.codelerity.com ) 33 | */ 34 | public class Controllers { 35 | 36 | /** 37 | * Always store the top-level pipeline reference to stop it being garbage 38 | * collected. 39 | */ 40 | private static Pipeline pipeline; 41 | 42 | /** 43 | * @param args the command line arguments 44 | */ 45 | public static void main(String[] args) { 46 | 47 | /** 48 | * Set up paths to native GStreamer libraries - see adjacent file. 49 | */ 50 | Utils.configurePaths(); 51 | 52 | /** 53 | * Initialize GStreamer. Always pass the lowest version you require - 54 | * Version.BASELINE is GStreamer 1.8. Use Version.of() for higher. 55 | * Features requiring later versions of GStreamer than passed here will 56 | * throw an exception in the bindings even if the actual native library 57 | * is a higher version. 58 | */ 59 | Gst.init(Version.BASELINE, "Controllers", args); 60 | 61 | pipeline = new Pipeline(); 62 | 63 | /** 64 | * Set up a videotestsrc. Use setAsString to pass the pattern as a name 65 | * rather than a number. 66 | */ 67 | Element src = ElementFactory.make("videotestsrc", "src"); 68 | src.setAsString("pattern", "circular"); 69 | 70 | /** 71 | * Set up a textoverlay. Configure horizontal and vertical alignment to 72 | * position so the controllers can move the text. 73 | */ 74 | Element text = ElementFactory.make("textoverlay", "text"); 75 | text.set("text", "GStreamer rocks!"); 76 | text.set("font-desc", "Sans, 42"); 77 | text.setAsString("halignment", "position"); 78 | text.setAsString("valignment", "position"); 79 | 80 | /** 81 | * Use a capsfilter to increase the default dimensions of the output. 82 | */ 83 | Element capsfilter = ElementFactory.make("capsfilter", "capsfilter"); 84 | capsfilter.setAsString("caps", "video/x-raw, width=800, height=600"); 85 | 86 | /** 87 | * Use an autovideosink. Add all the elements to the pipeline and link. 88 | */ 89 | Element sink = ElementFactory.make("autovideosink", "sink"); 90 | pipeline.addMany(src, text, capsfilter, sink); 91 | Pipeline.linkMany(src, text, capsfilter, sink); 92 | 93 | /** 94 | * Create and configure LFO control sources to control the text 95 | * position. Create direct control bindings mapped to the element 96 | * properties, then add the bindings to the element. 97 | */ 98 | LFOControlSource csXPos = new LFOControlSource(); 99 | csXPos.setFrequency(0.11).setAmplitude(0.2).setOffset(0.5); 100 | text.addControlBinding(DirectControlBinding.create(text, "xpos", csXPos)); 101 | 102 | LFOControlSource csYPos = new LFOControlSource(); 103 | csYPos.setFrequency(0.04).setAmplitude(0.4).setOffset(0.5); 104 | text.addControlBinding(DirectControlBinding.create(text, "ypos", csYPos)); 105 | 106 | /** 107 | * Create and configure 3 LFO control sources for the colour channels, 108 | * create an ARGB control binding mapped to the colour property, and add 109 | * the binding to the element. 110 | */ 111 | LFOControlSource csR = new LFOControlSource(); 112 | csR.setFrequency(0.19).setAmplitude(0.5).setOffset(0.5); 113 | LFOControlSource csG = new LFOControlSource(); 114 | csG.setFrequency(0.27).setAmplitude(0.5).setOffset(0.5); 115 | LFOControlSource csB = new LFOControlSource(); 116 | csB.setFrequency(0.13).setAmplitude(0.5).setOffset(0.5); 117 | text.addControlBinding(ARGBControlBinding.create(text, "color", null, csR, csG, csB)); 118 | 119 | // start the pipeline 120 | pipeline.play(); 121 | 122 | /** 123 | * GStreamer native threads will not be taken into account by the JVM 124 | * when deciding whether to shutdown, so we have to keep the main thread 125 | * alive. Gst.main() will keep the calling thread alive until Gst.quit() 126 | * is called. Here we use the built-in executor to schedule a quit after 127 | * 15 seconds. 128 | */ 129 | Gst.getExecutor().schedule(Gst::quit, 15, TimeUnit.SECONDS); 130 | Gst.main(); 131 | 132 | } 133 | 134 | } 135 | -------------------------------------------------------------------------------- /Controllers/src/main/java/org/freedesktop/gstreamer/examples/Utils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 | * 4 | * Copyright 2021 Neil C Smith - Codelerity Ltd. 5 | * 6 | * Copying and distribution of this file, with or without modification, 7 | * are permitted in any medium without royalty provided the copyright 8 | * notice and this notice are preserved. This file is offered as-is, 9 | * without any warranty. 10 | * 11 | */ 12 | package org.freedesktop.gstreamer.examples; 13 | 14 | import com.sun.jna.Platform; 15 | import com.sun.jna.platform.win32.Kernel32; 16 | import java.io.File; 17 | import java.util.stream.Stream; 18 | 19 | /** 20 | * Utility methods for use in examples. 21 | */ 22 | class Utils { 23 | 24 | private Utils() { 25 | } 26 | 27 | /** 28 | * Configures paths to the GStreamer libraries. On Windows queries various 29 | * GStreamer environment variables, and then sets up the PATH environment 30 | * variable. On macOS, adds the location to jna.library.path (macOS binaries 31 | * link to each other). On both, the gstreamer.path system property can be 32 | * used to override. On Linux, assumes GStreamer is in the path already. 33 | */ 34 | static void configurePaths() { 35 | if (Platform.isWindows()) { 36 | String gstPath = System.getProperty("gstreamer.path", findWindowsLocation()); 37 | if (!gstPath.isEmpty()) { 38 | String systemPath = System.getenv("PATH"); 39 | if (systemPath == null || systemPath.trim().isEmpty()) { 40 | Kernel32.INSTANCE.SetEnvironmentVariable("PATH", gstPath); 41 | } else { 42 | Kernel32.INSTANCE.SetEnvironmentVariable("PATH", gstPath 43 | + File.pathSeparator + systemPath); 44 | } 45 | } 46 | } else if (Platform.isMac()) { 47 | String gstPath = System.getProperty("gstreamer.path", 48 | "/Library/Frameworks/GStreamer.framework/Libraries/"); 49 | if (!gstPath.isEmpty()) { 50 | String jnaPath = System.getProperty("jna.library.path", "").trim(); 51 | if (jnaPath.isEmpty()) { 52 | System.setProperty("jna.library.path", gstPath); 53 | } else { 54 | System.setProperty("jna.library.path", jnaPath + File.pathSeparator + gstPath); 55 | } 56 | } 57 | 58 | } 59 | } 60 | 61 | /** 62 | * Query over a stream of possible environment variables for GStreamer 63 | * location, filtering on the first non-null result, and adding \bin\ to the 64 | * value. 65 | * 66 | * @return location or empty string 67 | */ 68 | static String findWindowsLocation() { 69 | if (Platform.is64Bit()) { 70 | return Stream.of("GSTREAMER_1_0_ROOT_MSVC_X86_64", 71 | "GSTREAMER_1_0_ROOT_MINGW_X86_64", 72 | "GSTREAMER_1_0_ROOT_X86_64") 73 | .map(System::getenv) 74 | .filter(p -> p != null) 75 | .map(p -> p.endsWith("\\") ? p + "bin\\" : p + "\\bin\\") 76 | .findFirst().orElse(""); 77 | } else { 78 | return ""; 79 | } 80 | } 81 | 82 | } 83 | -------------------------------------------------------------------------------- /FXCamera/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'application' 3 | id 'org.openjfx.javafxplugin' version '0.0.11' 4 | } 5 | 6 | mainClassName = 'org.freedesktop.gstreamer.examples.FXCamera' 7 | 8 | repositories { 9 | mavenCentral() 10 | } 11 | 12 | javafx { 13 | version = "17" 14 | modules = [ 'javafx.controls' ] 15 | } 16 | 17 | dependencies { 18 | implementation 'net.java.dev.jna:jna:5.10.0' 19 | implementation 'net.java.dev.jna:jna-platform:5.10.0' 20 | implementation 'org.freedesktop.gstreamer:gst1-java-core:1.4.0' 21 | implementation 'org.freedesktop.gstreamer:gst1-java-fx:0.9.0' 22 | } 23 | -------------------------------------------------------------------------------- /FXCamera/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gstreamer-java/gst1-java-examples/0667161ac70d3c8394990e8c010947c3a6aed7fe/FXCamera/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /FXCamera/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /FXCamera/gradlew: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # 4 | # Copyright © 2015-2021 the original authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | # 21 | # Gradle start up script for POSIX generated by Gradle. 22 | # 23 | # Important for running: 24 | # 25 | # (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is 26 | # noncompliant, but you have some other compliant shell such as ksh or 27 | # bash, then to run this script, type that shell name before the whole 28 | # command line, like: 29 | # 30 | # ksh Gradle 31 | # 32 | # Busybox and similar reduced shells will NOT work, because this script 33 | # requires all of these POSIX shell features: 34 | # * functions; 35 | # * expansions «$var», «${var}», «${var:-default}», «${var+SET}», 36 | # «${var#prefix}», «${var%suffix}», and «$( cmd )»; 37 | # * compound commands having a testable exit status, especially «case»; 38 | # * various built-in commands including «command», «set», and «ulimit». 39 | # 40 | # Important for patching: 41 | # 42 | # (2) This script targets any POSIX shell, so it avoids extensions provided 43 | # by Bash, Ksh, etc; in particular arrays are avoided. 44 | # 45 | # The "traditional" practice of packing multiple parameters into a 46 | # space-separated string is a well documented source of bugs and security 47 | # problems, so this is (mostly) avoided, by progressively accumulating 48 | # options in "$@", and eventually passing that to Java. 49 | # 50 | # Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, 51 | # and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; 52 | # see the in-line comments for details. 53 | # 54 | # There are tweaks for specific operating systems such as AIX, CygWin, 55 | # Darwin, MinGW, and NonStop. 56 | # 57 | # (3) This script is generated from the Groovy template 58 | # https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt 59 | # within the Gradle project. 60 | # 61 | # You can find Gradle at https://github.com/gradle/gradle/. 62 | # 63 | ############################################################################## 64 | 65 | # Attempt to set APP_HOME 66 | 67 | # Resolve links: $0 may be a link 68 | app_path=$0 69 | 70 | # Need this for daisy-chained symlinks. 71 | while 72 | APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path 73 | [ -h "$app_path" ] 74 | do 75 | ls=$( ls -ld "$app_path" ) 76 | link=${ls#*' -> '} 77 | case $link in #( 78 | /*) app_path=$link ;; #( 79 | *) app_path=$APP_HOME$link ;; 80 | esac 81 | done 82 | 83 | APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit 84 | 85 | APP_NAME="Gradle" 86 | APP_BASE_NAME=${0##*/} 87 | 88 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 89 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 90 | 91 | # Use the maximum available, or set MAX_FD != -1 to use that value. 92 | MAX_FD=maximum 93 | 94 | warn () { 95 | echo "$*" 96 | } >&2 97 | 98 | die () { 99 | echo 100 | echo "$*" 101 | echo 102 | exit 1 103 | } >&2 104 | 105 | # OS specific support (must be 'true' or 'false'). 106 | cygwin=false 107 | msys=false 108 | darwin=false 109 | nonstop=false 110 | case "$( uname )" in #( 111 | CYGWIN* ) cygwin=true ;; #( 112 | Darwin* ) darwin=true ;; #( 113 | MSYS* | MINGW* ) msys=true ;; #( 114 | NONSTOP* ) nonstop=true ;; 115 | esac 116 | 117 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 118 | 119 | 120 | # Determine the Java command to use to start the JVM. 121 | if [ -n "$JAVA_HOME" ] ; then 122 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 123 | # IBM's JDK on AIX uses strange locations for the executables 124 | JAVACMD=$JAVA_HOME/jre/sh/java 125 | else 126 | JAVACMD=$JAVA_HOME/bin/java 127 | fi 128 | if [ ! -x "$JAVACMD" ] ; then 129 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 130 | 131 | Please set the JAVA_HOME variable in your environment to match the 132 | location of your Java installation." 133 | fi 134 | else 135 | JAVACMD=java 136 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 137 | 138 | Please set the JAVA_HOME variable in your environment to match the 139 | location of your Java installation." 140 | fi 141 | 142 | # Increase the maximum file descriptors if we can. 143 | if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then 144 | case $MAX_FD in #( 145 | max*) 146 | MAX_FD=$( ulimit -H -n ) || 147 | warn "Could not query maximum file descriptor limit" 148 | esac 149 | case $MAX_FD in #( 150 | '' | soft) :;; #( 151 | *) 152 | ulimit -n "$MAX_FD" || 153 | warn "Could not set maximum file descriptor limit to $MAX_FD" 154 | esac 155 | fi 156 | 157 | # Collect all arguments for the java command, stacking in reverse order: 158 | # * args from the command line 159 | # * the main class name 160 | # * -classpath 161 | # * -D...appname settings 162 | # * --module-path (only if needed) 163 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. 164 | 165 | # For Cygwin or MSYS, switch paths to Windows format before running java 166 | if "$cygwin" || "$msys" ; then 167 | APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) 168 | CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) 169 | 170 | JAVACMD=$( cygpath --unix "$JAVACMD" ) 171 | 172 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 173 | for arg do 174 | if 175 | case $arg in #( 176 | -*) false ;; # don't mess with options #( 177 | /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath 178 | [ -e "$t" ] ;; #( 179 | *) false ;; 180 | esac 181 | then 182 | arg=$( cygpath --path --ignore --mixed "$arg" ) 183 | fi 184 | # Roll the args list around exactly as many times as the number of 185 | # args, so each arg winds up back in the position where it started, but 186 | # possibly modified. 187 | # 188 | # NB: a `for` loop captures its iteration list before it begins, so 189 | # changing the positional parameters here affects neither the number of 190 | # iterations, nor the values presented in `arg`. 191 | shift # remove old arg 192 | set -- "$@" "$arg" # push replacement arg 193 | done 194 | fi 195 | 196 | # Collect all arguments for the java command; 197 | # * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of 198 | # shell script including quotes and variable substitutions, so put them in 199 | # double quotes to make sure that they get re-expanded; and 200 | # * put everything else in single quotes, so that it's not re-expanded. 201 | 202 | set -- \ 203 | "-Dorg.gradle.appname=$APP_BASE_NAME" \ 204 | -classpath "$CLASSPATH" \ 205 | org.gradle.wrapper.GradleWrapperMain \ 206 | "$@" 207 | 208 | # Use "xargs" to parse quoted args. 209 | # 210 | # With -n1 it outputs one arg per line, with the quotes and backslashes removed. 211 | # 212 | # In Bash we could simply go: 213 | # 214 | # readarray ARGS < <( xargs -n1 <<<"$var" ) && 215 | # set -- "${ARGS[@]}" "$@" 216 | # 217 | # but POSIX shell has neither arrays nor command substitution, so instead we 218 | # post-process each arg (as a line of input to sed) to backslash-escape any 219 | # character that might be a shell metacharacter, then use eval to reverse 220 | # that process (while maintaining the separation between arguments), and wrap 221 | # the whole thing up as a single "set" statement. 222 | # 223 | # This will of course break if any of these variables contains a newline or 224 | # an unmatched quote. 225 | # 226 | 227 | eval "set -- $( 228 | printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | 229 | xargs -n1 | 230 | sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | 231 | tr '\n' ' ' 232 | )" '"$@"' 233 | 234 | exec "$JAVACMD" "$@" 235 | -------------------------------------------------------------------------------- /FXCamera/gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 34 | 35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 37 | 38 | @rem Find java.exe 39 | if defined JAVA_HOME goto findJavaFromJavaHome 40 | 41 | set JAVA_EXE=java.exe 42 | %JAVA_EXE% -version >NUL 2>&1 43 | if "%ERRORLEVEL%" == "0" goto execute 44 | 45 | echo. 46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 47 | echo. 48 | echo Please set the JAVA_HOME variable in your environment to match the 49 | echo location of your Java installation. 50 | 51 | goto fail 52 | 53 | :findJavaFromJavaHome 54 | set JAVA_HOME=%JAVA_HOME:"=% 55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 56 | 57 | if exist "%JAVA_EXE%" goto execute 58 | 59 | echo. 60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 61 | echo. 62 | echo Please set the JAVA_HOME variable in your environment to match the 63 | echo location of your Java installation. 64 | 65 | goto fail 66 | 67 | :execute 68 | @rem Setup the command line 69 | 70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 71 | 72 | 73 | @rem Execute Gradle 74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 75 | 76 | :end 77 | @rem End local scope for the variables with windows NT shell 78 | if "%ERRORLEVEL%"=="0" goto mainEnd 79 | 80 | :fail 81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 82 | rem the _cmd.exe /c_ return code! 83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 84 | exit /b 1 85 | 86 | :mainEnd 87 | if "%OS%"=="Windows_NT" endlocal 88 | 89 | :omega 90 | -------------------------------------------------------------------------------- /FXCamera/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'FXCamera' 2 | -------------------------------------------------------------------------------- /FXCamera/src/main/java/org/freedesktop/gstreamer/examples/FXCamera.java: -------------------------------------------------------------------------------- 1 | /* 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 | * 4 | * Copyright 2021 Neil C Smith - Codelerity Ltd. 5 | * 6 | * Copying and distribution of this file, with or without modification, 7 | * are permitted in any medium without royalty provided the copyright 8 | * notice and this notice are preserved. This file is offered as-is, 9 | * without any warranty. 10 | * 11 | */ 12 | package org.freedesktop.gstreamer.examples; 13 | 14 | import javafx.application.Application; 15 | import javafx.geometry.Insets; 16 | import javafx.scene.Scene; 17 | import javafx.scene.image.ImageView; 18 | import javafx.scene.layout.Background; 19 | import javafx.scene.layout.BackgroundFill; 20 | import javafx.scene.layout.BorderPane; 21 | import javafx.scene.layout.CornerRadii; 22 | import javafx.scene.paint.Color; 23 | import javafx.stage.Stage; 24 | import org.freedesktop.gstreamer.Bin; 25 | import org.freedesktop.gstreamer.Gst; 26 | import org.freedesktop.gstreamer.Pipeline; 27 | import org.freedesktop.gstreamer.Version; 28 | import org.freedesktop.gstreamer.fx.FXImageSink; 29 | 30 | /** 31 | * Displays camera input to a JavaFX window. By using autovideosrc, this will 32 | * revert to a test video source if no camera is detected. 33 | * 34 | * @author Neil C Smith ( https://www.codelerity.com ) 35 | */ 36 | public class FXCamera extends Application { 37 | 38 | /** 39 | * Always store the top-level pipeline reference to stop it being garbage 40 | * collected. 41 | */ 42 | private Pipeline pipeline; 43 | 44 | @Override 45 | public void init() throws Exception { 46 | /** 47 | * Set up paths to native GStreamer libraries - see adjacent file. 48 | */ 49 | Utils.configurePaths(); 50 | 51 | /** 52 | * Initialize GStreamer. Always pass the lowest version you require - 53 | * Version.BASELINE is GStreamer 1.8. Use Version.of() for higher. 54 | * Features requiring later versions of GStreamer than passed here will 55 | * throw an exception in the bindings even if the actual native library 56 | * is a higher version. 57 | */ 58 | Gst.init(Version.BASELINE, "FXCamera"); 59 | } 60 | 61 | @Override 62 | public void start(Stage stage) throws Exception { 63 | 64 | /** 65 | * FXImageSink from gst1-java-fx wraps the native data from the 66 | * GStreamer AppSink in a JavaFX image. Requested dimensions will be set 67 | * on the caps for the AppSink. 68 | */ 69 | FXImageSink imageSink = new FXImageSink(); 70 | imageSink.requestFrameSize(640, 480); 71 | 72 | /** 73 | * Parse a Bin to contain the autovideosrc from a GStreamer string 74 | * representation. The alternative approach would be to create and link 75 | * the elements in code using ElementFactory::make. 76 | * 77 | * The Bin uses a videoconvert element to convert the video format to 78 | * that required by the FXImageSink, a videoscale in case the source 79 | * does not support the required resolution. 80 | * 81 | * The bin is added to a top-level pipeline and linked to the AppSink 82 | * from the image sink. 83 | */ 84 | Bin bin = Gst.parseBinFromDescription( 85 | "autovideosrc ! videoscale ! videoconvert", 86 | true); 87 | pipeline = new Pipeline(); 88 | pipeline.addMany(bin, imageSink.getSinkElement()); 89 | Pipeline.linkMany(bin, imageSink.getSinkElement()); 90 | 91 | /** 92 | * Set up a simple JavaFX window with ImageView. 93 | */ 94 | stage.setTitle("FX Camera"); 95 | BorderPane pane = new BorderPane(); 96 | pane.setBackground(new Background(new BackgroundFill( 97 | Color.BLACK, CornerRadii.EMPTY, Insets.EMPTY))); 98 | ImageView view = new ImageView(); 99 | pane.setCenter(view); 100 | 101 | /** 102 | * Bind the ImageView to the image from the FXImageSink, and scale the 103 | * view within the window. 104 | */ 105 | view.imageProperty().bind(imageSink.imageProperty()); 106 | view.fitWidthProperty().bind(pane.widthProperty()); 107 | view.fitHeightProperty().bind(pane.heightProperty()); 108 | view.setPreserveRatio(true); 109 | 110 | /** 111 | * Show the window and start the pipeline. 112 | */ 113 | stage.setScene(new Scene(pane, 640, 480)); 114 | stage.show(); 115 | pipeline.play(); 116 | } 117 | 118 | public static void main(String[] args) { 119 | // pass to JavaFX 120 | launch(args); 121 | } 122 | 123 | } 124 | -------------------------------------------------------------------------------- /FXCamera/src/main/java/org/freedesktop/gstreamer/examples/Utils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 | * 4 | * Copyright 2021 Neil C Smith - Codelerity Ltd. 5 | * 6 | * Copying and distribution of this file, with or without modification, 7 | * are permitted in any medium without royalty provided the copyright 8 | * notice and this notice are preserved. This file is offered as-is, 9 | * without any warranty. 10 | * 11 | */ 12 | package org.freedesktop.gstreamer.examples; 13 | 14 | import com.sun.jna.Platform; 15 | import com.sun.jna.platform.win32.Kernel32; 16 | import java.io.File; 17 | import java.util.stream.Stream; 18 | 19 | /** 20 | * Utility methods for use in examples. 21 | */ 22 | class Utils { 23 | 24 | private Utils() { 25 | } 26 | 27 | /** 28 | * Configures paths to the GStreamer libraries. On Windows queries various 29 | * GStreamer environment variables, and then sets up the PATH environment 30 | * variable. On macOS, adds the location to jna.library.path (macOS binaries 31 | * link to each other). On both, the gstreamer.path system property can be 32 | * used to override. On Linux, assumes GStreamer is in the path already. 33 | */ 34 | static void configurePaths() { 35 | if (Platform.isWindows()) { 36 | String gstPath = System.getProperty("gstreamer.path", findWindowsLocation()); 37 | if (!gstPath.isEmpty()) { 38 | String systemPath = System.getenv("PATH"); 39 | if (systemPath == null || systemPath.trim().isEmpty()) { 40 | Kernel32.INSTANCE.SetEnvironmentVariable("PATH", gstPath); 41 | } else { 42 | Kernel32.INSTANCE.SetEnvironmentVariable("PATH", gstPath 43 | + File.pathSeparator + systemPath); 44 | } 45 | } 46 | } else if (Platform.isMac()) { 47 | String gstPath = System.getProperty("gstreamer.path", 48 | "/Library/Frameworks/GStreamer.framework/Libraries/"); 49 | if (!gstPath.isEmpty()) { 50 | String jnaPath = System.getProperty("jna.library.path", "").trim(); 51 | if (jnaPath.isEmpty()) { 52 | System.setProperty("jna.library.path", gstPath); 53 | } else { 54 | System.setProperty("jna.library.path", jnaPath + File.pathSeparator + gstPath); 55 | } 56 | } 57 | 58 | } 59 | } 60 | 61 | /** 62 | * Query over a stream of possible environment variables for GStreamer 63 | * location, filtering on the first non-null result, and adding \bin\ to the 64 | * value. 65 | * 66 | * @return location or empty string 67 | */ 68 | static String findWindowsLocation() { 69 | if (Platform.is64Bit()) { 70 | return Stream.of("GSTREAMER_1_0_ROOT_MSVC_X86_64", 71 | "GSTREAMER_1_0_ROOT_MINGW_X86_64", 72 | "GSTREAMER_1_0_ROOT_X86_64") 73 | .map(System::getenv) 74 | .filter(p -> p != null) 75 | .map(p -> p.endsWith("\\") ? p + "bin\\" : p + "\\bin\\") 76 | .findFirst().orElse(""); 77 | } else { 78 | return ""; 79 | } 80 | } 81 | 82 | } 83 | -------------------------------------------------------------------------------- /FXPlayer/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'application' 3 | id 'org.openjfx.javafxplugin' version '0.0.11' 4 | } 5 | 6 | mainClassName = 'org.freedesktop.gstreamer.examples.FXPlayer' 7 | 8 | repositories { 9 | mavenCentral() 10 | } 11 | 12 | javafx { 13 | version = "17" 14 | modules = [ 'javafx.controls' ] 15 | } 16 | 17 | dependencies { 18 | implementation 'net.java.dev.jna:jna:5.10.0' 19 | implementation 'net.java.dev.jna:jna-platform:5.10.0' 20 | implementation 'org.freedesktop.gstreamer:gst1-java-core:1.4.0' 21 | implementation 'org.freedesktop.gstreamer:gst1-java-fx:0.9.0' 22 | } 23 | -------------------------------------------------------------------------------- /FXPlayer/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gstreamer-java/gst1-java-examples/0667161ac70d3c8394990e8c010947c3a6aed7fe/FXPlayer/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /FXPlayer/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /FXPlayer/gradlew: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # 4 | # Copyright © 2015-2021 the original authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | # 21 | # Gradle start up script for POSIX generated by Gradle. 22 | # 23 | # Important for running: 24 | # 25 | # (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is 26 | # noncompliant, but you have some other compliant shell such as ksh or 27 | # bash, then to run this script, type that shell name before the whole 28 | # command line, like: 29 | # 30 | # ksh Gradle 31 | # 32 | # Busybox and similar reduced shells will NOT work, because this script 33 | # requires all of these POSIX shell features: 34 | # * functions; 35 | # * expansions «$var», «${var}», «${var:-default}», «${var+SET}», 36 | # «${var#prefix}», «${var%suffix}», and «$( cmd )»; 37 | # * compound commands having a testable exit status, especially «case»; 38 | # * various built-in commands including «command», «set», and «ulimit». 39 | # 40 | # Important for patching: 41 | # 42 | # (2) This script targets any POSIX shell, so it avoids extensions provided 43 | # by Bash, Ksh, etc; in particular arrays are avoided. 44 | # 45 | # The "traditional" practice of packing multiple parameters into a 46 | # space-separated string is a well documented source of bugs and security 47 | # problems, so this is (mostly) avoided, by progressively accumulating 48 | # options in "$@", and eventually passing that to Java. 49 | # 50 | # Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, 51 | # and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; 52 | # see the in-line comments for details. 53 | # 54 | # There are tweaks for specific operating systems such as AIX, CygWin, 55 | # Darwin, MinGW, and NonStop. 56 | # 57 | # (3) This script is generated from the Groovy template 58 | # https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt 59 | # within the Gradle project. 60 | # 61 | # You can find Gradle at https://github.com/gradle/gradle/. 62 | # 63 | ############################################################################## 64 | 65 | # Attempt to set APP_HOME 66 | 67 | # Resolve links: $0 may be a link 68 | app_path=$0 69 | 70 | # Need this for daisy-chained symlinks. 71 | while 72 | APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path 73 | [ -h "$app_path" ] 74 | do 75 | ls=$( ls -ld "$app_path" ) 76 | link=${ls#*' -> '} 77 | case $link in #( 78 | /*) app_path=$link ;; #( 79 | *) app_path=$APP_HOME$link ;; 80 | esac 81 | done 82 | 83 | APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit 84 | 85 | APP_NAME="Gradle" 86 | APP_BASE_NAME=${0##*/} 87 | 88 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 89 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 90 | 91 | # Use the maximum available, or set MAX_FD != -1 to use that value. 92 | MAX_FD=maximum 93 | 94 | warn () { 95 | echo "$*" 96 | } >&2 97 | 98 | die () { 99 | echo 100 | echo "$*" 101 | echo 102 | exit 1 103 | } >&2 104 | 105 | # OS specific support (must be 'true' or 'false'). 106 | cygwin=false 107 | msys=false 108 | darwin=false 109 | nonstop=false 110 | case "$( uname )" in #( 111 | CYGWIN* ) cygwin=true ;; #( 112 | Darwin* ) darwin=true ;; #( 113 | MSYS* | MINGW* ) msys=true ;; #( 114 | NONSTOP* ) nonstop=true ;; 115 | esac 116 | 117 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 118 | 119 | 120 | # Determine the Java command to use to start the JVM. 121 | if [ -n "$JAVA_HOME" ] ; then 122 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 123 | # IBM's JDK on AIX uses strange locations for the executables 124 | JAVACMD=$JAVA_HOME/jre/sh/java 125 | else 126 | JAVACMD=$JAVA_HOME/bin/java 127 | fi 128 | if [ ! -x "$JAVACMD" ] ; then 129 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 130 | 131 | Please set the JAVA_HOME variable in your environment to match the 132 | location of your Java installation." 133 | fi 134 | else 135 | JAVACMD=java 136 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 137 | 138 | Please set the JAVA_HOME variable in your environment to match the 139 | location of your Java installation." 140 | fi 141 | 142 | # Increase the maximum file descriptors if we can. 143 | if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then 144 | case $MAX_FD in #( 145 | max*) 146 | MAX_FD=$( ulimit -H -n ) || 147 | warn "Could not query maximum file descriptor limit" 148 | esac 149 | case $MAX_FD in #( 150 | '' | soft) :;; #( 151 | *) 152 | ulimit -n "$MAX_FD" || 153 | warn "Could not set maximum file descriptor limit to $MAX_FD" 154 | esac 155 | fi 156 | 157 | # Collect all arguments for the java command, stacking in reverse order: 158 | # * args from the command line 159 | # * the main class name 160 | # * -classpath 161 | # * -D...appname settings 162 | # * --module-path (only if needed) 163 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. 164 | 165 | # For Cygwin or MSYS, switch paths to Windows format before running java 166 | if "$cygwin" || "$msys" ; then 167 | APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) 168 | CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) 169 | 170 | JAVACMD=$( cygpath --unix "$JAVACMD" ) 171 | 172 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 173 | for arg do 174 | if 175 | case $arg in #( 176 | -*) false ;; # don't mess with options #( 177 | /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath 178 | [ -e "$t" ] ;; #( 179 | *) false ;; 180 | esac 181 | then 182 | arg=$( cygpath --path --ignore --mixed "$arg" ) 183 | fi 184 | # Roll the args list around exactly as many times as the number of 185 | # args, so each arg winds up back in the position where it started, but 186 | # possibly modified. 187 | # 188 | # NB: a `for` loop captures its iteration list before it begins, so 189 | # changing the positional parameters here affects neither the number of 190 | # iterations, nor the values presented in `arg`. 191 | shift # remove old arg 192 | set -- "$@" "$arg" # push replacement arg 193 | done 194 | fi 195 | 196 | # Collect all arguments for the java command; 197 | # * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of 198 | # shell script including quotes and variable substitutions, so put them in 199 | # double quotes to make sure that they get re-expanded; and 200 | # * put everything else in single quotes, so that it's not re-expanded. 201 | 202 | set -- \ 203 | "-Dorg.gradle.appname=$APP_BASE_NAME" \ 204 | -classpath "$CLASSPATH" \ 205 | org.gradle.wrapper.GradleWrapperMain \ 206 | "$@" 207 | 208 | # Use "xargs" to parse quoted args. 209 | # 210 | # With -n1 it outputs one arg per line, with the quotes and backslashes removed. 211 | # 212 | # In Bash we could simply go: 213 | # 214 | # readarray ARGS < <( xargs -n1 <<<"$var" ) && 215 | # set -- "${ARGS[@]}" "$@" 216 | # 217 | # but POSIX shell has neither arrays nor command substitution, so instead we 218 | # post-process each arg (as a line of input to sed) to backslash-escape any 219 | # character that might be a shell metacharacter, then use eval to reverse 220 | # that process (while maintaining the separation between arguments), and wrap 221 | # the whole thing up as a single "set" statement. 222 | # 223 | # This will of course break if any of these variables contains a newline or 224 | # an unmatched quote. 225 | # 226 | 227 | eval "set -- $( 228 | printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | 229 | xargs -n1 | 230 | sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | 231 | tr '\n' ' ' 232 | )" '"$@"' 233 | 234 | exec "$JAVACMD" "$@" 235 | -------------------------------------------------------------------------------- /FXPlayer/gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 34 | 35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 37 | 38 | @rem Find java.exe 39 | if defined JAVA_HOME goto findJavaFromJavaHome 40 | 41 | set JAVA_EXE=java.exe 42 | %JAVA_EXE% -version >NUL 2>&1 43 | if "%ERRORLEVEL%" == "0" goto execute 44 | 45 | echo. 46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 47 | echo. 48 | echo Please set the JAVA_HOME variable in your environment to match the 49 | echo location of your Java installation. 50 | 51 | goto fail 52 | 53 | :findJavaFromJavaHome 54 | set JAVA_HOME=%JAVA_HOME:"=% 55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 56 | 57 | if exist "%JAVA_EXE%" goto execute 58 | 59 | echo. 60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 61 | echo. 62 | echo Please set the JAVA_HOME variable in your environment to match the 63 | echo location of your Java installation. 64 | 65 | goto fail 66 | 67 | :execute 68 | @rem Setup the command line 69 | 70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 71 | 72 | 73 | @rem Execute Gradle 74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 75 | 76 | :end 77 | @rem End local scope for the variables with windows NT shell 78 | if "%ERRORLEVEL%"=="0" goto mainEnd 79 | 80 | :fail 81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 82 | rem the _cmd.exe /c_ return code! 83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 84 | exit /b 1 85 | 86 | :mainEnd 87 | if "%OS%"=="Windows_NT" endlocal 88 | 89 | :omega 90 | -------------------------------------------------------------------------------- /FXPlayer/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'FXPlayer' 2 | -------------------------------------------------------------------------------- /FXPlayer/src/main/java/org/freedesktop/gstreamer/examples/Utils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 | * 4 | * Copyright 2021 Neil C Smith - Codelerity Ltd. 5 | * 6 | * Copying and distribution of this file, with or without modification, 7 | * are permitted in any medium without royalty provided the copyright 8 | * notice and this notice are preserved. This file is offered as-is, 9 | * without any warranty. 10 | * 11 | */ 12 | package org.freedesktop.gstreamer.examples; 13 | 14 | import com.sun.jna.Platform; 15 | import com.sun.jna.platform.win32.Kernel32; 16 | import java.io.File; 17 | import java.util.stream.Stream; 18 | 19 | /** 20 | * Utility methods for use in examples. 21 | */ 22 | class Utils { 23 | 24 | private Utils() { 25 | } 26 | 27 | /** 28 | * Configures paths to the GStreamer libraries. On Windows queries various 29 | * GStreamer environment variables, and then sets up the PATH environment 30 | * variable. On macOS, adds the location to jna.library.path (macOS binaries 31 | * link to each other). On both, the gstreamer.path system property can be 32 | * used to override. On Linux, assumes GStreamer is in the path already. 33 | */ 34 | static void configurePaths() { 35 | if (Platform.isWindows()) { 36 | String gstPath = System.getProperty("gstreamer.path", findWindowsLocation()); 37 | if (!gstPath.isEmpty()) { 38 | String systemPath = System.getenv("PATH"); 39 | if (systemPath == null || systemPath.trim().isEmpty()) { 40 | Kernel32.INSTANCE.SetEnvironmentVariable("PATH", gstPath); 41 | } else { 42 | Kernel32.INSTANCE.SetEnvironmentVariable("PATH", gstPath 43 | + File.pathSeparator + systemPath); 44 | } 45 | } 46 | } else if (Platform.isMac()) { 47 | String gstPath = System.getProperty("gstreamer.path", 48 | "/Library/Frameworks/GStreamer.framework/Libraries/"); 49 | if (!gstPath.isEmpty()) { 50 | String jnaPath = System.getProperty("jna.library.path", "").trim(); 51 | if (jnaPath.isEmpty()) { 52 | System.setProperty("jna.library.path", gstPath); 53 | } else { 54 | System.setProperty("jna.library.path", jnaPath + File.pathSeparator + gstPath); 55 | } 56 | } 57 | 58 | } 59 | } 60 | 61 | /** 62 | * Query over a stream of possible environment variables for GStreamer 63 | * location, filtering on the first non-null result, and adding \bin\ to the 64 | * value. 65 | * 66 | * @return location or empty string 67 | */ 68 | static String findWindowsLocation() { 69 | if (Platform.is64Bit()) { 70 | return Stream.of("GSTREAMER_1_0_ROOT_MSVC_X86_64", 71 | "GSTREAMER_1_0_ROOT_MINGW_X86_64", 72 | "GSTREAMER_1_0_ROOT_X86_64") 73 | .map(System::getenv) 74 | .filter(p -> p != null) 75 | .map(p -> p.endsWith("\\") ? p + "bin\\" : p + "\\bin\\") 76 | .findFirst().orElse(""); 77 | } else { 78 | return ""; 79 | } 80 | } 81 | 82 | } 83 | -------------------------------------------------------------------------------- /HLS/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java' 2 | apply plugin: 'application' 3 | 4 | 5 | mainClassName = 'org.freedesktop.gstreamer.examples.HLS' 6 | 7 | repositories { 8 | mavenCentral() 9 | } 10 | 11 | dependencies { 12 | implementation 'net.java.dev.jna:jna:5.10.0' 13 | implementation 'net.java.dev.jna:jna-platform:5.10.0' 14 | implementation 'org.freedesktop.gstreamer:gst1-java-core:1.4.0' 15 | implementation 'io.javalin:javalin:3.13.7' 16 | implementation 'org.slf4j:slf4j-simple:1.7.30' 17 | implementation 'org.webjars.npm:hls.js:1.0.7' 18 | } 19 | -------------------------------------------------------------------------------- /HLS/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gstreamer-java/gst1-java-examples/0667161ac70d3c8394990e8c010947c3a6aed7fe/HLS/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /HLS/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /HLS/gradlew: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # 4 | # Copyright © 2015-2021 the original authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | # 21 | # Gradle start up script for POSIX generated by Gradle. 22 | # 23 | # Important for running: 24 | # 25 | # (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is 26 | # noncompliant, but you have some other compliant shell such as ksh or 27 | # bash, then to run this script, type that shell name before the whole 28 | # command line, like: 29 | # 30 | # ksh Gradle 31 | # 32 | # Busybox and similar reduced shells will NOT work, because this script 33 | # requires all of these POSIX shell features: 34 | # * functions; 35 | # * expansions «$var», «${var}», «${var:-default}», «${var+SET}», 36 | # «${var#prefix}», «${var%suffix}», and «$( cmd )»; 37 | # * compound commands having a testable exit status, especially «case»; 38 | # * various built-in commands including «command», «set», and «ulimit». 39 | # 40 | # Important for patching: 41 | # 42 | # (2) This script targets any POSIX shell, so it avoids extensions provided 43 | # by Bash, Ksh, etc; in particular arrays are avoided. 44 | # 45 | # The "traditional" practice of packing multiple parameters into a 46 | # space-separated string is a well documented source of bugs and security 47 | # problems, so this is (mostly) avoided, by progressively accumulating 48 | # options in "$@", and eventually passing that to Java. 49 | # 50 | # Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, 51 | # and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; 52 | # see the in-line comments for details. 53 | # 54 | # There are tweaks for specific operating systems such as AIX, CygWin, 55 | # Darwin, MinGW, and NonStop. 56 | # 57 | # (3) This script is generated from the Groovy template 58 | # https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt 59 | # within the Gradle project. 60 | # 61 | # You can find Gradle at https://github.com/gradle/gradle/. 62 | # 63 | ############################################################################## 64 | 65 | # Attempt to set APP_HOME 66 | 67 | # Resolve links: $0 may be a link 68 | app_path=$0 69 | 70 | # Need this for daisy-chained symlinks. 71 | while 72 | APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path 73 | [ -h "$app_path" ] 74 | do 75 | ls=$( ls -ld "$app_path" ) 76 | link=${ls#*' -> '} 77 | case $link in #( 78 | /*) app_path=$link ;; #( 79 | *) app_path=$APP_HOME$link ;; 80 | esac 81 | done 82 | 83 | APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit 84 | 85 | APP_NAME="Gradle" 86 | APP_BASE_NAME=${0##*/} 87 | 88 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 89 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 90 | 91 | # Use the maximum available, or set MAX_FD != -1 to use that value. 92 | MAX_FD=maximum 93 | 94 | warn () { 95 | echo "$*" 96 | } >&2 97 | 98 | die () { 99 | echo 100 | echo "$*" 101 | echo 102 | exit 1 103 | } >&2 104 | 105 | # OS specific support (must be 'true' or 'false'). 106 | cygwin=false 107 | msys=false 108 | darwin=false 109 | nonstop=false 110 | case "$( uname )" in #( 111 | CYGWIN* ) cygwin=true ;; #( 112 | Darwin* ) darwin=true ;; #( 113 | MSYS* | MINGW* ) msys=true ;; #( 114 | NONSTOP* ) nonstop=true ;; 115 | esac 116 | 117 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 118 | 119 | 120 | # Determine the Java command to use to start the JVM. 121 | if [ -n "$JAVA_HOME" ] ; then 122 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 123 | # IBM's JDK on AIX uses strange locations for the executables 124 | JAVACMD=$JAVA_HOME/jre/sh/java 125 | else 126 | JAVACMD=$JAVA_HOME/bin/java 127 | fi 128 | if [ ! -x "$JAVACMD" ] ; then 129 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 130 | 131 | Please set the JAVA_HOME variable in your environment to match the 132 | location of your Java installation." 133 | fi 134 | else 135 | JAVACMD=java 136 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 137 | 138 | Please set the JAVA_HOME variable in your environment to match the 139 | location of your Java installation." 140 | fi 141 | 142 | # Increase the maximum file descriptors if we can. 143 | if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then 144 | case $MAX_FD in #( 145 | max*) 146 | MAX_FD=$( ulimit -H -n ) || 147 | warn "Could not query maximum file descriptor limit" 148 | esac 149 | case $MAX_FD in #( 150 | '' | soft) :;; #( 151 | *) 152 | ulimit -n "$MAX_FD" || 153 | warn "Could not set maximum file descriptor limit to $MAX_FD" 154 | esac 155 | fi 156 | 157 | # Collect all arguments for the java command, stacking in reverse order: 158 | # * args from the command line 159 | # * the main class name 160 | # * -classpath 161 | # * -D...appname settings 162 | # * --module-path (only if needed) 163 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. 164 | 165 | # For Cygwin or MSYS, switch paths to Windows format before running java 166 | if "$cygwin" || "$msys" ; then 167 | APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) 168 | CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) 169 | 170 | JAVACMD=$( cygpath --unix "$JAVACMD" ) 171 | 172 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 173 | for arg do 174 | if 175 | case $arg in #( 176 | -*) false ;; # don't mess with options #( 177 | /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath 178 | [ -e "$t" ] ;; #( 179 | *) false ;; 180 | esac 181 | then 182 | arg=$( cygpath --path --ignore --mixed "$arg" ) 183 | fi 184 | # Roll the args list around exactly as many times as the number of 185 | # args, so each arg winds up back in the position where it started, but 186 | # possibly modified. 187 | # 188 | # NB: a `for` loop captures its iteration list before it begins, so 189 | # changing the positional parameters here affects neither the number of 190 | # iterations, nor the values presented in `arg`. 191 | shift # remove old arg 192 | set -- "$@" "$arg" # push replacement arg 193 | done 194 | fi 195 | 196 | # Collect all arguments for the java command; 197 | # * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of 198 | # shell script including quotes and variable substitutions, so put them in 199 | # double quotes to make sure that they get re-expanded; and 200 | # * put everything else in single quotes, so that it's not re-expanded. 201 | 202 | set -- \ 203 | "-Dorg.gradle.appname=$APP_BASE_NAME" \ 204 | -classpath "$CLASSPATH" \ 205 | org.gradle.wrapper.GradleWrapperMain \ 206 | "$@" 207 | 208 | # Use "xargs" to parse quoted args. 209 | # 210 | # With -n1 it outputs one arg per line, with the quotes and backslashes removed. 211 | # 212 | # In Bash we could simply go: 213 | # 214 | # readarray ARGS < <( xargs -n1 <<<"$var" ) && 215 | # set -- "${ARGS[@]}" "$@" 216 | # 217 | # but POSIX shell has neither arrays nor command substitution, so instead we 218 | # post-process each arg (as a line of input to sed) to backslash-escape any 219 | # character that might be a shell metacharacter, then use eval to reverse 220 | # that process (while maintaining the separation between arguments), and wrap 221 | # the whole thing up as a single "set" statement. 222 | # 223 | # This will of course break if any of these variables contains a newline or 224 | # an unmatched quote. 225 | # 226 | 227 | eval "set -- $( 228 | printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | 229 | xargs -n1 | 230 | sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | 231 | tr '\n' ' ' 232 | )" '"$@"' 233 | 234 | exec "$JAVACMD" "$@" 235 | -------------------------------------------------------------------------------- /HLS/gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 34 | 35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 37 | 38 | @rem Find java.exe 39 | if defined JAVA_HOME goto findJavaFromJavaHome 40 | 41 | set JAVA_EXE=java.exe 42 | %JAVA_EXE% -version >NUL 2>&1 43 | if "%ERRORLEVEL%" == "0" goto execute 44 | 45 | echo. 46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 47 | echo. 48 | echo Please set the JAVA_HOME variable in your environment to match the 49 | echo location of your Java installation. 50 | 51 | goto fail 52 | 53 | :findJavaFromJavaHome 54 | set JAVA_HOME=%JAVA_HOME:"=% 55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 56 | 57 | if exist "%JAVA_EXE%" goto execute 58 | 59 | echo. 60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 61 | echo. 62 | echo Please set the JAVA_HOME variable in your environment to match the 63 | echo location of your Java installation. 64 | 65 | goto fail 66 | 67 | :execute 68 | @rem Setup the command line 69 | 70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 71 | 72 | 73 | @rem Execute Gradle 74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 75 | 76 | :end 77 | @rem End local scope for the variables with windows NT shell 78 | if "%ERRORLEVEL%"=="0" goto mainEnd 79 | 80 | :fail 81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 82 | rem the _cmd.exe /c_ return code! 83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 84 | exit /b 1 85 | 86 | :mainEnd 87 | if "%OS%"=="Windows_NT" endlocal 88 | 89 | :omega 90 | -------------------------------------------------------------------------------- /HLS/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'HLS' 2 | -------------------------------------------------------------------------------- /HLS/src/main/java/org/freedesktop/gstreamer/examples/Utils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 | * 4 | * Copyright 2021 Neil C Smith - Codelerity Ltd. 5 | * 6 | * Copying and distribution of this file, with or without modification, 7 | * are permitted in any medium without royalty provided the copyright 8 | * notice and this notice are preserved. This file is offered as-is, 9 | * without any warranty. 10 | * 11 | */ 12 | package org.freedesktop.gstreamer.examples; 13 | 14 | import com.sun.jna.Platform; 15 | import com.sun.jna.platform.win32.Kernel32; 16 | import java.io.File; 17 | import java.util.stream.Stream; 18 | 19 | /** 20 | * Utility methods for use in examples. 21 | */ 22 | class Utils { 23 | 24 | private Utils() { 25 | } 26 | 27 | /** 28 | * Configures paths to the GStreamer libraries. On Windows queries various 29 | * GStreamer environment variables, and then sets up the PATH environment 30 | * variable. On macOS, adds the location to jna.library.path (macOS binaries 31 | * link to each other). On both, the gstreamer.path system property can be 32 | * used to override. On Linux, assumes GStreamer is in the path already. 33 | */ 34 | static void configurePaths() { 35 | if (Platform.isWindows()) { 36 | String gstPath = System.getProperty("gstreamer.path", findWindowsLocation()); 37 | if (!gstPath.isEmpty()) { 38 | String systemPath = System.getenv("PATH"); 39 | if (systemPath == null || systemPath.trim().isEmpty()) { 40 | Kernel32.INSTANCE.SetEnvironmentVariable("PATH", gstPath); 41 | } else { 42 | Kernel32.INSTANCE.SetEnvironmentVariable("PATH", gstPath 43 | + File.pathSeparator + systemPath); 44 | } 45 | } 46 | } else if (Platform.isMac()) { 47 | String gstPath = System.getProperty("gstreamer.path", 48 | "/Library/Frameworks/GStreamer.framework/Libraries/"); 49 | if (!gstPath.isEmpty()) { 50 | String jnaPath = System.getProperty("jna.library.path", "").trim(); 51 | if (jnaPath.isEmpty()) { 52 | System.setProperty("jna.library.path", gstPath); 53 | } else { 54 | System.setProperty("jna.library.path", jnaPath + File.pathSeparator + gstPath); 55 | } 56 | } 57 | 58 | } 59 | } 60 | 61 | /** 62 | * Query over a stream of possible environment variables for GStreamer 63 | * location, filtering on the first non-null result, and adding \bin\ to the 64 | * value. 65 | * 66 | * @return location or empty string 67 | */ 68 | static String findWindowsLocation() { 69 | if (Platform.is64Bit()) { 70 | return Stream.of("GSTREAMER_1_0_ROOT_MSVC_X86_64", 71 | "GSTREAMER_1_0_ROOT_MINGW_X86_64", 72 | "GSTREAMER_1_0_ROOT_X86_64") 73 | .map(System::getenv) 74 | .filter(p -> p != null) 75 | .map(p -> p.endsWith("\\") ? p + "bin\\" : p + "\\bin\\") 76 | .findFirst().orElse(""); 77 | } else { 78 | return ""; 79 | } 80 | } 81 | 82 | } 83 | -------------------------------------------------------------------------------- /HLS/src/main/resources/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | HLS GStreamer Java example 5 | 6 | 7 | 8 | 9 | 10 |

11 |

HLS GStreamer Java example

12 |

Video may take up to 20s to appear from server start. Playlist file is 13 | only created after some segments are available.
The default latency is 14 | quite high (15-30s). This can be lowered using hlssink2 element 15 | properties and hls.js configuration.

16 | 17 |
18 | 46 | 47 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | GStreamer Java examples 2 | ======================= 3 | 4 | This repository contains a series of example projects for using 5 | [GStreamer 1.x][gstreamer] with Java via the [GStreamer Java][gstreamer-java] 6 | libraries, including [gst1-java-core][gst1-core] and extensions. 7 | 8 | The code steps in each project source file are documented. Any questions, please 9 | use the [mailing list][gstreamer-java-group]. 10 | 11 | ## Requirements 12 | 13 | All examples are self-contained Gradle projects. They should work inside your IDE 14 | or via `./gradlew run` on the command line. 15 | 16 | All the examples require an [installation of GStreamer][gstreamer-download] itself. 17 | Windows users installing GStreamer should select the complete profile, rather than 18 | the typical one. 19 | 20 | Most examples work with JDK 8+. The JavaFX integration example requires JDK 11+ 21 | (and uses JavaFX 15). 22 | 23 | ## Examples 24 | 25 | Inside each example there is an identical `Utils.java` file that contains some 26 | useful code for setting up native paths for an installed version of GStreamer. 27 | This code, and all the example code (aside from some files in the archive), is 28 | free to adapt for your own usage. 29 | 30 | ### Getting started 31 | 32 | - **BasicPipeline** : getting started running a video test source into a GStreamer 33 | output window. 34 | 35 | ### Desktop (Swing / JavaFX) 36 | 37 | - **SwingCamera** : using a camera (or test source) inside a Swing application, 38 | using `gst1-java-swing`. 39 | - **SwingPlayer** : a simple media player with Swing UI, including file selection, 40 | playback controls, seeking and volume meters. 41 | - **FXCamera** : using a camera (or test source) inside a JavaFX application, 42 | using `gst1-java-fx`. 43 | - **FXPlayer** : a simple media player with JavaFX UI, including file selection, 44 | playback controls, seeking and volume meters. 45 | 46 | ### Server / Internet 47 | 48 | - **HLS** : using HTTP Live Streaming to stream live video with Java2D rendered 49 | overlay to browser using Javalin framework. 50 | - **WebRTCSendRecv** : example of sending and receiving via WebRTC using the test 51 | page at https://webrtc.nirbheek.in This is the test server used in upstream 52 | GStreamer examples. You may need to configure permissions in the browser to 53 | always allow audio for that site. You need to pass in the session ID from the 54 | page on the CLI. If running in the terminal via Gradle, it is recommended to 55 | use `./gradlew --console=plain run`. 56 | 57 | ### Miscellaneous 58 | 59 | - **BufferProbe** : using a buffer probe to draw an animation on top of the video 60 | stream using Java2D. 61 | - **Controllers** : configuring controllers to control element properties (ported 62 | from an upstream C example). 63 | 64 | ### Archive 65 | 66 | Inside the archive folder are all the previously available examples, some of which 67 | have not yet been adapted into self-contained example projects. 68 | 69 | [gstreamer]: https://gstreamer.freedesktop.org/ 70 | [gstreamer-download]: https://gstreamer.freedesktop.org/download/ 71 | [gstreamer-java]: https://github.com/gstreamer-java 72 | [gstreamer-java-group]: https://groups.google.com/forum/#!forum/gstreamer-java 73 | [gst1-core]: https://github.com/gstreamer-java/gst1-java-core 74 | -------------------------------------------------------------------------------- /SwingCamera/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java' 2 | apply plugin: 'application' 3 | 4 | 5 | mainClassName = 'org.freedesktop.gstreamer.examples.SwingCamera' 6 | 7 | repositories { 8 | mavenCentral() 9 | } 10 | 11 | dependencies { 12 | implementation 'net.java.dev.jna:jna:5.10.0' 13 | implementation 'net.java.dev.jna:jna-platform:5.10.0' 14 | implementation 'org.freedesktop.gstreamer:gst1-java-core:1.4.0' 15 | implementation 'org.freedesktop.gstreamer:gst1-java-swing:0.9.0' 16 | } 17 | -------------------------------------------------------------------------------- /SwingCamera/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gstreamer-java/gst1-java-examples/0667161ac70d3c8394990e8c010947c3a6aed7fe/SwingCamera/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /SwingCamera/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /SwingCamera/gradlew: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # 4 | # Copyright © 2015-2021 the original authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | # 21 | # Gradle start up script for POSIX generated by Gradle. 22 | # 23 | # Important for running: 24 | # 25 | # (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is 26 | # noncompliant, but you have some other compliant shell such as ksh or 27 | # bash, then to run this script, type that shell name before the whole 28 | # command line, like: 29 | # 30 | # ksh Gradle 31 | # 32 | # Busybox and similar reduced shells will NOT work, because this script 33 | # requires all of these POSIX shell features: 34 | # * functions; 35 | # * expansions «$var», «${var}», «${var:-default}», «${var+SET}», 36 | # «${var#prefix}», «${var%suffix}», and «$( cmd )»; 37 | # * compound commands having a testable exit status, especially «case»; 38 | # * various built-in commands including «command», «set», and «ulimit». 39 | # 40 | # Important for patching: 41 | # 42 | # (2) This script targets any POSIX shell, so it avoids extensions provided 43 | # by Bash, Ksh, etc; in particular arrays are avoided. 44 | # 45 | # The "traditional" practice of packing multiple parameters into a 46 | # space-separated string is a well documented source of bugs and security 47 | # problems, so this is (mostly) avoided, by progressively accumulating 48 | # options in "$@", and eventually passing that to Java. 49 | # 50 | # Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, 51 | # and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; 52 | # see the in-line comments for details. 53 | # 54 | # There are tweaks for specific operating systems such as AIX, CygWin, 55 | # Darwin, MinGW, and NonStop. 56 | # 57 | # (3) This script is generated from the Groovy template 58 | # https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt 59 | # within the Gradle project. 60 | # 61 | # You can find Gradle at https://github.com/gradle/gradle/. 62 | # 63 | ############################################################################## 64 | 65 | # Attempt to set APP_HOME 66 | 67 | # Resolve links: $0 may be a link 68 | app_path=$0 69 | 70 | # Need this for daisy-chained symlinks. 71 | while 72 | APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path 73 | [ -h "$app_path" ] 74 | do 75 | ls=$( ls -ld "$app_path" ) 76 | link=${ls#*' -> '} 77 | case $link in #( 78 | /*) app_path=$link ;; #( 79 | *) app_path=$APP_HOME$link ;; 80 | esac 81 | done 82 | 83 | APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit 84 | 85 | APP_NAME="Gradle" 86 | APP_BASE_NAME=${0##*/} 87 | 88 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 89 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 90 | 91 | # Use the maximum available, or set MAX_FD != -1 to use that value. 92 | MAX_FD=maximum 93 | 94 | warn () { 95 | echo "$*" 96 | } >&2 97 | 98 | die () { 99 | echo 100 | echo "$*" 101 | echo 102 | exit 1 103 | } >&2 104 | 105 | # OS specific support (must be 'true' or 'false'). 106 | cygwin=false 107 | msys=false 108 | darwin=false 109 | nonstop=false 110 | case "$( uname )" in #( 111 | CYGWIN* ) cygwin=true ;; #( 112 | Darwin* ) darwin=true ;; #( 113 | MSYS* | MINGW* ) msys=true ;; #( 114 | NONSTOP* ) nonstop=true ;; 115 | esac 116 | 117 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 118 | 119 | 120 | # Determine the Java command to use to start the JVM. 121 | if [ -n "$JAVA_HOME" ] ; then 122 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 123 | # IBM's JDK on AIX uses strange locations for the executables 124 | JAVACMD=$JAVA_HOME/jre/sh/java 125 | else 126 | JAVACMD=$JAVA_HOME/bin/java 127 | fi 128 | if [ ! -x "$JAVACMD" ] ; then 129 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 130 | 131 | Please set the JAVA_HOME variable in your environment to match the 132 | location of your Java installation." 133 | fi 134 | else 135 | JAVACMD=java 136 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 137 | 138 | Please set the JAVA_HOME variable in your environment to match the 139 | location of your Java installation." 140 | fi 141 | 142 | # Increase the maximum file descriptors if we can. 143 | if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then 144 | case $MAX_FD in #( 145 | max*) 146 | MAX_FD=$( ulimit -H -n ) || 147 | warn "Could not query maximum file descriptor limit" 148 | esac 149 | case $MAX_FD in #( 150 | '' | soft) :;; #( 151 | *) 152 | ulimit -n "$MAX_FD" || 153 | warn "Could not set maximum file descriptor limit to $MAX_FD" 154 | esac 155 | fi 156 | 157 | # Collect all arguments for the java command, stacking in reverse order: 158 | # * args from the command line 159 | # * the main class name 160 | # * -classpath 161 | # * -D...appname settings 162 | # * --module-path (only if needed) 163 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. 164 | 165 | # For Cygwin or MSYS, switch paths to Windows format before running java 166 | if "$cygwin" || "$msys" ; then 167 | APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) 168 | CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) 169 | 170 | JAVACMD=$( cygpath --unix "$JAVACMD" ) 171 | 172 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 173 | for arg do 174 | if 175 | case $arg in #( 176 | -*) false ;; # don't mess with options #( 177 | /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath 178 | [ -e "$t" ] ;; #( 179 | *) false ;; 180 | esac 181 | then 182 | arg=$( cygpath --path --ignore --mixed "$arg" ) 183 | fi 184 | # Roll the args list around exactly as many times as the number of 185 | # args, so each arg winds up back in the position where it started, but 186 | # possibly modified. 187 | # 188 | # NB: a `for` loop captures its iteration list before it begins, so 189 | # changing the positional parameters here affects neither the number of 190 | # iterations, nor the values presented in `arg`. 191 | shift # remove old arg 192 | set -- "$@" "$arg" # push replacement arg 193 | done 194 | fi 195 | 196 | # Collect all arguments for the java command; 197 | # * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of 198 | # shell script including quotes and variable substitutions, so put them in 199 | # double quotes to make sure that they get re-expanded; and 200 | # * put everything else in single quotes, so that it's not re-expanded. 201 | 202 | set -- \ 203 | "-Dorg.gradle.appname=$APP_BASE_NAME" \ 204 | -classpath "$CLASSPATH" \ 205 | org.gradle.wrapper.GradleWrapperMain \ 206 | "$@" 207 | 208 | # Use "xargs" to parse quoted args. 209 | # 210 | # With -n1 it outputs one arg per line, with the quotes and backslashes removed. 211 | # 212 | # In Bash we could simply go: 213 | # 214 | # readarray ARGS < <( xargs -n1 <<<"$var" ) && 215 | # set -- "${ARGS[@]}" "$@" 216 | # 217 | # but POSIX shell has neither arrays nor command substitution, so instead we 218 | # post-process each arg (as a line of input to sed) to backslash-escape any 219 | # character that might be a shell metacharacter, then use eval to reverse 220 | # that process (while maintaining the separation between arguments), and wrap 221 | # the whole thing up as a single "set" statement. 222 | # 223 | # This will of course break if any of these variables contains a newline or 224 | # an unmatched quote. 225 | # 226 | 227 | eval "set -- $( 228 | printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | 229 | xargs -n1 | 230 | sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | 231 | tr '\n' ' ' 232 | )" '"$@"' 233 | 234 | exec "$JAVACMD" "$@" 235 | -------------------------------------------------------------------------------- /SwingCamera/gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 34 | 35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 37 | 38 | @rem Find java.exe 39 | if defined JAVA_HOME goto findJavaFromJavaHome 40 | 41 | set JAVA_EXE=java.exe 42 | %JAVA_EXE% -version >NUL 2>&1 43 | if "%ERRORLEVEL%" == "0" goto execute 44 | 45 | echo. 46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 47 | echo. 48 | echo Please set the JAVA_HOME variable in your environment to match the 49 | echo location of your Java installation. 50 | 51 | goto fail 52 | 53 | :findJavaFromJavaHome 54 | set JAVA_HOME=%JAVA_HOME:"=% 55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 56 | 57 | if exist "%JAVA_EXE%" goto execute 58 | 59 | echo. 60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 61 | echo. 62 | echo Please set the JAVA_HOME variable in your environment to match the 63 | echo location of your Java installation. 64 | 65 | goto fail 66 | 67 | :execute 68 | @rem Setup the command line 69 | 70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 71 | 72 | 73 | @rem Execute Gradle 74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 75 | 76 | :end 77 | @rem End local scope for the variables with windows NT shell 78 | if "%ERRORLEVEL%"=="0" goto mainEnd 79 | 80 | :fail 81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 82 | rem the _cmd.exe /c_ return code! 83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 84 | exit /b 1 85 | 86 | :mainEnd 87 | if "%OS%"=="Windows_NT" endlocal 88 | 89 | :omega 90 | -------------------------------------------------------------------------------- /SwingCamera/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'SwingCamera' 2 | -------------------------------------------------------------------------------- /SwingCamera/src/main/java/org/freedesktop/gstreamer/examples/SwingCamera.java: -------------------------------------------------------------------------------- 1 | /* 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 | * 4 | * Copyright 2021 Neil C Smith - Codelerity Ltd. 5 | * 6 | * Copying and distribution of this file, with or without modification, 7 | * are permitted in any medium without royalty provided the copyright 8 | * notice and this notice are preserved. This file is offered as-is, 9 | * without any warranty. 10 | * 11 | */ 12 | package org.freedesktop.gstreamer.examples; 13 | 14 | import java.awt.Dimension; 15 | import java.awt.EventQueue; 16 | import javax.swing.JFrame; 17 | import org.freedesktop.gstreamer.Bin; 18 | import org.freedesktop.gstreamer.Gst; 19 | import org.freedesktop.gstreamer.Pipeline; 20 | import org.freedesktop.gstreamer.Version; 21 | import org.freedesktop.gstreamer.swing.GstVideoComponent; 22 | 23 | /** 24 | * Displays camera input to a Swing window. By using autovideosrc, this will 25 | * revert to a test video source if no camera is detected. 26 | * 27 | * @author Neil C Smith ( https://www.codelerity.com ) 28 | * 29 | */ 30 | public class SwingCamera { 31 | 32 | /** 33 | * Always store the top-level pipeline reference to stop it being garbage 34 | * collected. 35 | */ 36 | private static Pipeline pipeline; 37 | 38 | /** 39 | * @param args the command line arguments 40 | */ 41 | public static void main(String[] args) { 42 | 43 | /** 44 | * Set up paths to native GStreamer libraries - see adjacent file. 45 | */ 46 | Utils.configurePaths(); 47 | 48 | /** 49 | * Initialize GStreamer. Always pass the lowest version you require - 50 | * Version.BASELINE is GStreamer 1.8. Use Version.of() for higher. 51 | * Features requiring later versions of GStreamer than passed here will 52 | * throw an exception in the bindings even if the actual native library 53 | * is a higher version. 54 | */ 55 | Gst.init(Version.BASELINE, "SwingCamera", args); 56 | 57 | EventQueue.invokeLater(() -> { 58 | 59 | /** 60 | * GstVideoComponent from gst1-java-swing is a Swing component that 61 | * wraps a GStreamer AppSink to display video in a Swing UI. 62 | */ 63 | GstVideoComponent vc = new GstVideoComponent(); 64 | 65 | /** 66 | * Parse a Bin to contain the autovideosrc from a GStreamer string 67 | * representation. The alternative approach would be to create and 68 | * link the elements in code using ElementFactory::make. 69 | * 70 | * The Bin uses a capsfilter to specify a width and height, as well 71 | * as a videoconvert element to convert the video format to that 72 | * required by GstVideoComponent, and a videoscale in case the 73 | * source does not support the required resolution. 74 | * 75 | * The bin is added to a top-level pipeline and linked to the 76 | * AppSink from the Swing component. 77 | */ 78 | Bin bin = Gst.parseBinFromDescription( 79 | "autovideosrc ! " 80 | + "videoscale ! videoconvert ! " 81 | + "capsfilter caps=video/x-raw,width=640,height=480", 82 | true); 83 | pipeline = new Pipeline(); 84 | pipeline.addMany(bin, vc.getElement()); 85 | Pipeline.linkMany(bin, vc.getElement()); 86 | 87 | JFrame f = new JFrame("Camera Test"); 88 | f.add(vc); 89 | vc.setPreferredSize(new Dimension(640, 480)); 90 | f.pack(); 91 | f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 92 | 93 | pipeline.play(); 94 | f.setVisible(true); 95 | 96 | }); 97 | 98 | } 99 | 100 | } 101 | -------------------------------------------------------------------------------- /SwingCamera/src/main/java/org/freedesktop/gstreamer/examples/Utils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 | * 4 | * Copyright 2021 Neil C Smith - Codelerity Ltd. 5 | * 6 | * Copying and distribution of this file, with or without modification, 7 | * are permitted in any medium without royalty provided the copyright 8 | * notice and this notice are preserved. This file is offered as-is, 9 | * without any warranty. 10 | * 11 | */ 12 | package org.freedesktop.gstreamer.examples; 13 | 14 | import com.sun.jna.Platform; 15 | import com.sun.jna.platform.win32.Kernel32; 16 | import java.io.File; 17 | import java.util.stream.Stream; 18 | 19 | /** 20 | * Utility methods for use in examples. 21 | */ 22 | class Utils { 23 | 24 | private Utils() { 25 | } 26 | 27 | /** 28 | * Configures paths to the GStreamer libraries. On Windows queries various 29 | * GStreamer environment variables, and then sets up the PATH environment 30 | * variable. On macOS, adds the location to jna.library.path (macOS binaries 31 | * link to each other). On both, the gstreamer.path system property can be 32 | * used to override. On Linux, assumes GStreamer is in the path already. 33 | */ 34 | static void configurePaths() { 35 | if (Platform.isWindows()) { 36 | String gstPath = System.getProperty("gstreamer.path", findWindowsLocation()); 37 | if (!gstPath.isEmpty()) { 38 | String systemPath = System.getenv("PATH"); 39 | if (systemPath == null || systemPath.trim().isEmpty()) { 40 | Kernel32.INSTANCE.SetEnvironmentVariable("PATH", gstPath); 41 | } else { 42 | Kernel32.INSTANCE.SetEnvironmentVariable("PATH", gstPath 43 | + File.pathSeparator + systemPath); 44 | } 45 | } 46 | } else if (Platform.isMac()) { 47 | String gstPath = System.getProperty("gstreamer.path", 48 | "/Library/Frameworks/GStreamer.framework/Libraries/"); 49 | if (!gstPath.isEmpty()) { 50 | String jnaPath = System.getProperty("jna.library.path", "").trim(); 51 | if (jnaPath.isEmpty()) { 52 | System.setProperty("jna.library.path", gstPath); 53 | } else { 54 | System.setProperty("jna.library.path", jnaPath + File.pathSeparator + gstPath); 55 | } 56 | } 57 | 58 | } 59 | } 60 | 61 | /** 62 | * Query over a stream of possible environment variables for GStreamer 63 | * location, filtering on the first non-null result, and adding \bin\ to the 64 | * value. 65 | * 66 | * @return location or empty string 67 | */ 68 | static String findWindowsLocation() { 69 | if (Platform.is64Bit()) { 70 | return Stream.of("GSTREAMER_1_0_ROOT_MSVC_X86_64", 71 | "GSTREAMER_1_0_ROOT_MINGW_X86_64", 72 | "GSTREAMER_1_0_ROOT_X86_64") 73 | .map(System::getenv) 74 | .filter(p -> p != null) 75 | .map(p -> p.endsWith("\\") ? p + "bin\\" : p + "\\bin\\") 76 | .findFirst().orElse(""); 77 | } else { 78 | return ""; 79 | } 80 | } 81 | 82 | } 83 | -------------------------------------------------------------------------------- /SwingPlayer/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java' 2 | apply plugin: 'application' 3 | 4 | 5 | mainClassName = 'org.freedesktop.gstreamer.examples.SwingPlayer' 6 | 7 | repositories { 8 | mavenCentral() 9 | } 10 | 11 | dependencies { 12 | implementation 'net.java.dev.jna:jna:5.10.0' 13 | implementation 'net.java.dev.jna:jna-platform:5.10.0' 14 | implementation 'org.freedesktop.gstreamer:gst1-java-core:1.4.0' 15 | implementation 'org.freedesktop.gstreamer:gst1-java-swing:0.9.0' 16 | implementation 'com.formdev:flatlaf:2.0' 17 | } 18 | -------------------------------------------------------------------------------- /SwingPlayer/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gstreamer-java/gst1-java-examples/0667161ac70d3c8394990e8c010947c3a6aed7fe/SwingPlayer/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /SwingPlayer/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /SwingPlayer/gradlew: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # 4 | # Copyright © 2015-2021 the original authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | # 21 | # Gradle start up script for POSIX generated by Gradle. 22 | # 23 | # Important for running: 24 | # 25 | # (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is 26 | # noncompliant, but you have some other compliant shell such as ksh or 27 | # bash, then to run this script, type that shell name before the whole 28 | # command line, like: 29 | # 30 | # ksh Gradle 31 | # 32 | # Busybox and similar reduced shells will NOT work, because this script 33 | # requires all of these POSIX shell features: 34 | # * functions; 35 | # * expansions «$var», «${var}», «${var:-default}», «${var+SET}», 36 | # «${var#prefix}», «${var%suffix}», and «$( cmd )»; 37 | # * compound commands having a testable exit status, especially «case»; 38 | # * various built-in commands including «command», «set», and «ulimit». 39 | # 40 | # Important for patching: 41 | # 42 | # (2) This script targets any POSIX shell, so it avoids extensions provided 43 | # by Bash, Ksh, etc; in particular arrays are avoided. 44 | # 45 | # The "traditional" practice of packing multiple parameters into a 46 | # space-separated string is a well documented source of bugs and security 47 | # problems, so this is (mostly) avoided, by progressively accumulating 48 | # options in "$@", and eventually passing that to Java. 49 | # 50 | # Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, 51 | # and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; 52 | # see the in-line comments for details. 53 | # 54 | # There are tweaks for specific operating systems such as AIX, CygWin, 55 | # Darwin, MinGW, and NonStop. 56 | # 57 | # (3) This script is generated from the Groovy template 58 | # https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt 59 | # within the Gradle project. 60 | # 61 | # You can find Gradle at https://github.com/gradle/gradle/. 62 | # 63 | ############################################################################## 64 | 65 | # Attempt to set APP_HOME 66 | 67 | # Resolve links: $0 may be a link 68 | app_path=$0 69 | 70 | # Need this for daisy-chained symlinks. 71 | while 72 | APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path 73 | [ -h "$app_path" ] 74 | do 75 | ls=$( ls -ld "$app_path" ) 76 | link=${ls#*' -> '} 77 | case $link in #( 78 | /*) app_path=$link ;; #( 79 | *) app_path=$APP_HOME$link ;; 80 | esac 81 | done 82 | 83 | APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit 84 | 85 | APP_NAME="Gradle" 86 | APP_BASE_NAME=${0##*/} 87 | 88 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 89 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 90 | 91 | # Use the maximum available, or set MAX_FD != -1 to use that value. 92 | MAX_FD=maximum 93 | 94 | warn () { 95 | echo "$*" 96 | } >&2 97 | 98 | die () { 99 | echo 100 | echo "$*" 101 | echo 102 | exit 1 103 | } >&2 104 | 105 | # OS specific support (must be 'true' or 'false'). 106 | cygwin=false 107 | msys=false 108 | darwin=false 109 | nonstop=false 110 | case "$( uname )" in #( 111 | CYGWIN* ) cygwin=true ;; #( 112 | Darwin* ) darwin=true ;; #( 113 | MSYS* | MINGW* ) msys=true ;; #( 114 | NONSTOP* ) nonstop=true ;; 115 | esac 116 | 117 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 118 | 119 | 120 | # Determine the Java command to use to start the JVM. 121 | if [ -n "$JAVA_HOME" ] ; then 122 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 123 | # IBM's JDK on AIX uses strange locations for the executables 124 | JAVACMD=$JAVA_HOME/jre/sh/java 125 | else 126 | JAVACMD=$JAVA_HOME/bin/java 127 | fi 128 | if [ ! -x "$JAVACMD" ] ; then 129 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 130 | 131 | Please set the JAVA_HOME variable in your environment to match the 132 | location of your Java installation." 133 | fi 134 | else 135 | JAVACMD=java 136 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 137 | 138 | Please set the JAVA_HOME variable in your environment to match the 139 | location of your Java installation." 140 | fi 141 | 142 | # Increase the maximum file descriptors if we can. 143 | if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then 144 | case $MAX_FD in #( 145 | max*) 146 | MAX_FD=$( ulimit -H -n ) || 147 | warn "Could not query maximum file descriptor limit" 148 | esac 149 | case $MAX_FD in #( 150 | '' | soft) :;; #( 151 | *) 152 | ulimit -n "$MAX_FD" || 153 | warn "Could not set maximum file descriptor limit to $MAX_FD" 154 | esac 155 | fi 156 | 157 | # Collect all arguments for the java command, stacking in reverse order: 158 | # * args from the command line 159 | # * the main class name 160 | # * -classpath 161 | # * -D...appname settings 162 | # * --module-path (only if needed) 163 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. 164 | 165 | # For Cygwin or MSYS, switch paths to Windows format before running java 166 | if "$cygwin" || "$msys" ; then 167 | APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) 168 | CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) 169 | 170 | JAVACMD=$( cygpath --unix "$JAVACMD" ) 171 | 172 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 173 | for arg do 174 | if 175 | case $arg in #( 176 | -*) false ;; # don't mess with options #( 177 | /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath 178 | [ -e "$t" ] ;; #( 179 | *) false ;; 180 | esac 181 | then 182 | arg=$( cygpath --path --ignore --mixed "$arg" ) 183 | fi 184 | # Roll the args list around exactly as many times as the number of 185 | # args, so each arg winds up back in the position where it started, but 186 | # possibly modified. 187 | # 188 | # NB: a `for` loop captures its iteration list before it begins, so 189 | # changing the positional parameters here affects neither the number of 190 | # iterations, nor the values presented in `arg`. 191 | shift # remove old arg 192 | set -- "$@" "$arg" # push replacement arg 193 | done 194 | fi 195 | 196 | # Collect all arguments for the java command; 197 | # * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of 198 | # shell script including quotes and variable substitutions, so put them in 199 | # double quotes to make sure that they get re-expanded; and 200 | # * put everything else in single quotes, so that it's not re-expanded. 201 | 202 | set -- \ 203 | "-Dorg.gradle.appname=$APP_BASE_NAME" \ 204 | -classpath "$CLASSPATH" \ 205 | org.gradle.wrapper.GradleWrapperMain \ 206 | "$@" 207 | 208 | # Use "xargs" to parse quoted args. 209 | # 210 | # With -n1 it outputs one arg per line, with the quotes and backslashes removed. 211 | # 212 | # In Bash we could simply go: 213 | # 214 | # readarray ARGS < <( xargs -n1 <<<"$var" ) && 215 | # set -- "${ARGS[@]}" "$@" 216 | # 217 | # but POSIX shell has neither arrays nor command substitution, so instead we 218 | # post-process each arg (as a line of input to sed) to backslash-escape any 219 | # character that might be a shell metacharacter, then use eval to reverse 220 | # that process (while maintaining the separation between arguments), and wrap 221 | # the whole thing up as a single "set" statement. 222 | # 223 | # This will of course break if any of these variables contains a newline or 224 | # an unmatched quote. 225 | # 226 | 227 | eval "set -- $( 228 | printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | 229 | xargs -n1 | 230 | sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | 231 | tr '\n' ' ' 232 | )" '"$@"' 233 | 234 | exec "$JAVACMD" "$@" 235 | -------------------------------------------------------------------------------- /SwingPlayer/gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 34 | 35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 37 | 38 | @rem Find java.exe 39 | if defined JAVA_HOME goto findJavaFromJavaHome 40 | 41 | set JAVA_EXE=java.exe 42 | %JAVA_EXE% -version >NUL 2>&1 43 | if "%ERRORLEVEL%" == "0" goto execute 44 | 45 | echo. 46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 47 | echo. 48 | echo Please set the JAVA_HOME variable in your environment to match the 49 | echo location of your Java installation. 50 | 51 | goto fail 52 | 53 | :findJavaFromJavaHome 54 | set JAVA_HOME=%JAVA_HOME:"=% 55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 56 | 57 | if exist "%JAVA_EXE%" goto execute 58 | 59 | echo. 60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 61 | echo. 62 | echo Please set the JAVA_HOME variable in your environment to match the 63 | echo location of your Java installation. 64 | 65 | goto fail 66 | 67 | :execute 68 | @rem Setup the command line 69 | 70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 71 | 72 | 73 | @rem Execute Gradle 74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 75 | 76 | :end 77 | @rem End local scope for the variables with windows NT shell 78 | if "%ERRORLEVEL%"=="0" goto mainEnd 79 | 80 | :fail 81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 82 | rem the _cmd.exe /c_ return code! 83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 84 | exit /b 1 85 | 86 | :mainEnd 87 | if "%OS%"=="Windows_NT" endlocal 88 | 89 | :omega 90 | -------------------------------------------------------------------------------- /SwingPlayer/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'SwingPlayer' 2 | -------------------------------------------------------------------------------- /SwingPlayer/src/main/java/org/freedesktop/gstreamer/examples/SwingPlayer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 | * 4 | * Copyright 2021 Neil C Smith - Codelerity Ltd. 5 | * 6 | * Copying and distribution of this file, with or without modification, 7 | * are permitted in any medium without royalty provided the copyright 8 | * notice and this notice are preserved. This file is offered as-is, 9 | * without any warranty. 10 | * 11 | */ 12 | package org.freedesktop.gstreamer.examples; 13 | 14 | import com.formdev.flatlaf.FlatDarkLaf; 15 | import java.awt.BorderLayout; 16 | import java.awt.Dimension; 17 | import java.awt.EventQueue; 18 | import java.awt.FileDialog; 19 | import java.io.File; 20 | import java.util.EnumSet; 21 | import java.util.concurrent.TimeUnit; 22 | import javax.swing.Box; 23 | import javax.swing.JButton; 24 | import javax.swing.JFileChooser; 25 | import javax.swing.JFrame; 26 | import javax.swing.JProgressBar; 27 | import javax.swing.JSlider; 28 | import javax.swing.JToggleButton; 29 | import javax.swing.JToolBar; 30 | import javax.swing.Timer; 31 | import org.freedesktop.gstreamer.Bus; 32 | import org.freedesktop.gstreamer.Element; 33 | import org.freedesktop.gstreamer.ElementFactory; 34 | import org.freedesktop.gstreamer.Format; 35 | import org.freedesktop.gstreamer.Gst; 36 | import org.freedesktop.gstreamer.Structure; 37 | import org.freedesktop.gstreamer.Version; 38 | import org.freedesktop.gstreamer.elements.PlayBin; 39 | import org.freedesktop.gstreamer.event.SeekFlags; 40 | import org.freedesktop.gstreamer.message.Message; 41 | import org.freedesktop.gstreamer.swing.GstVideoComponent; 42 | 43 | /** 44 | * A simple PlayBin-based video player with Swing UI for file selection, video 45 | * control, seek and volume meters. 46 | * 47 | * @author Neil C Smith ( https://www.codelerity.com ) 48 | * 49 | */ 50 | public class SwingPlayer { 51 | 52 | /** 53 | * Always store the top-level pipeline (in this case PlayBin) reference to 54 | * stop it being garbage collected. 55 | */ 56 | private static PlayBin playbin; 57 | 58 | /** 59 | * @param args the command line arguments 60 | */ 61 | public static void main(String[] args) { 62 | 63 | /** 64 | * Set up paths to native GStreamer libraries - see adjacent file. 65 | */ 66 | Utils.configurePaths(); 67 | 68 | /** 69 | * Initialize GStreamer. Always pass the lowest version you require - 70 | * Version.BASELINE is GStreamer 1.8. Use Version.of() for higher. 71 | * Features requiring later versions of GStreamer than passed here will 72 | * throw an exception in the bindings even if the actual native library 73 | * is a higher version. 74 | */ 75 | Gst.init(Version.BASELINE, "SwingPlayer", args); 76 | 77 | EventQueue.invokeLater(() -> { 78 | 79 | // It's 2021! Using Apache-licensed FlatLaf. 80 | FlatDarkLaf.install(); 81 | boolean useNativeFileDialog = true; 82 | 83 | /** 84 | * GstVideoComponent from gst1-java-swing is a Swing component that 85 | * wraps a GStreamer AppSink to display video in a Swing UI. 86 | */ 87 | GstVideoComponent vc = new GstVideoComponent(); 88 | 89 | /** 90 | * Create a PlayBin element and set the AppSink from the Swing 91 | * component as the video sink. 92 | */ 93 | playbin = new PlayBin("playbin"); 94 | playbin.setVideoSink(vc.getElement()); 95 | 96 | /** 97 | * Create a level component and set it as the audio-filter property 98 | * on the playbin - this will post audio level messages to the bus - 99 | * see below how to display them. 100 | */ 101 | Element level = ElementFactory.make("level", "level"); 102 | playbin.set("audio-filter", level); 103 | 104 | /** 105 | * A basic Swing UI. 106 | */ 107 | JFrame window = new JFrame("Video Player"); 108 | window.add(vc); 109 | vc.setPreferredSize(new Dimension(800, 600)); 110 | JToolBar buttons = new JToolBar(); 111 | 112 | JButton fileButton = new JButton("File..."); 113 | fileButton.addActionListener(e -> { 114 | File file = null; 115 | if (useNativeFileDialog) { 116 | FileDialog fileDialog = new FileDialog(window); 117 | fileDialog.setVisible(true); 118 | File[] files = fileDialog.getFiles(); 119 | if (files.length > 0) { 120 | file = files[0]; 121 | } 122 | } else { 123 | JFileChooser fileChooser = new JFileChooser(); 124 | if (fileChooser.showOpenDialog(window) == JFileChooser.APPROVE_OPTION) { 125 | file = fileChooser.getSelectedFile(); 126 | } 127 | } 128 | if (file != null) { 129 | playbin.stop(); 130 | playbin.setURI(file.toURI()); 131 | playbin.play(); 132 | } 133 | 134 | }); 135 | 136 | // playback controls 137 | JButton playButton = new JButton("Play"); 138 | playButton.addActionListener(e -> playbin.play()); 139 | JButton pauseButton = new JButton("Pause"); 140 | pauseButton.addActionListener(e -> playbin.pause()); 141 | JToggleButton loopButton = new JToggleButton("Loop", true); 142 | 143 | // position slider 144 | JSlider position = new JSlider(0, 1000, 0); 145 | position.addChangeListener(e -> { 146 | if (position.getValueIsAdjusting()) { 147 | long dur = playbin.queryDuration(Format.TIME); 148 | if (dur > 0) { 149 | double relPos = position.getValue() / 1000.0; 150 | playbin.seekSimple(Format.TIME, 151 | EnumSet.of(SeekFlags.FLUSH), 152 | (long) (relPos * dur)); 153 | } 154 | } 155 | }); 156 | // sync slider position to video when not dragging 157 | new Timer(50, e -> { 158 | if (!position.getValueIsAdjusting()) { 159 | long dur = playbin.queryDuration(Format.TIME); 160 | long pos = playbin.queryPosition(Format.TIME); 161 | if (dur > 0) { 162 | double relPos = (double) pos / dur; 163 | position.setValue((int) (relPos * 1000)); 164 | } 165 | } 166 | }).start(); 167 | 168 | // quick and dirty level display using JProgressBar 169 | Box levels = Box.createVerticalBox(); 170 | JProgressBar leftLevel = new JProgressBar(); 171 | leftLevel.setMaximumSize(new Dimension(200, 20)); 172 | JProgressBar rightLevel = new JProgressBar(); 173 | rightLevel.setMaximumSize(new Dimension(200, 20)); 174 | levels.add(leftLevel); 175 | levels.add(rightLevel); 176 | 177 | buttons.add(fileButton); 178 | buttons.addSeparator(); 179 | buttons.add(playButton); 180 | buttons.add(pauseButton); 181 | buttons.addSeparator(); 182 | buttons.add(position); 183 | buttons.add(loopButton); 184 | buttons.addSeparator(); 185 | buttons.add(levels); 186 | window.add(buttons, BorderLayout.SOUTH); 187 | 188 | window.pack(); 189 | window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 190 | 191 | playbin.getBus().connect((Bus.EOS) source -> { 192 | // handle on Swing thread! 193 | EventQueue.invokeLater(() -> { 194 | if (loopButton.isSelected()) { 195 | playbin.seekSimple(Format.TIME, 196 | EnumSet.of(SeekFlags.FLUSH), 197 | 0); 198 | } else { 199 | playbin.stop(); 200 | position.setValue(0); 201 | } 202 | }); 203 | }); 204 | 205 | // listen for level messages on the bus 206 | playbin.getBus().connect("element", new Bus.MESSAGE() { 207 | 208 | @Override 209 | public void busMessage(Bus arg0, Message message) { 210 | if (message.getSource() == level) { 211 | Structure struct = message.getStructure(); 212 | // We can get either rms or peak 213 | double[] levels = struct.getDoubles("peak"); 214 | // Calculate the time offset required to get the level 215 | // information in sync with the video display 216 | long timeDelay = getTimeOffset(struct); 217 | Gst.getExecutor().schedule( 218 | () -> EventQueue.invokeLater(() -> updateLevelDisplay(levels)), 219 | timeDelay, TimeUnit.NANOSECONDS); 220 | } 221 | } 222 | 223 | private long getTimeOffset(Structure struct) { 224 | long actualTime = playbin.getClock().getTime() 225 | - playbin.getBaseTime(); 226 | long runningTime = (long) struct.getValue("running-time"); 227 | long duration = (long) struct.getValue("duration"); 228 | long messageTime = runningTime + (duration / 2); 229 | return messageTime - actualTime; 230 | } 231 | 232 | private void updateLevelDisplay(double[] levels) { 233 | if (playbin.isPlaying() && levels.length > 0) { 234 | // convert levels for display 235 | for (int i = 0; i < levels.length; i++) { 236 | levels[i] = Math.pow(10, levels[i] / 20); 237 | } 238 | if (levels.length >= 2) { 239 | leftLevel.setValue((int) Math.max(0, Math.min(levels[0] * 100, 100))); 240 | rightLevel.setValue((int) Math.max(0, Math.min(levels[1] * 100, 100))); 241 | } else { 242 | leftLevel.setValue((int) Math.max(0, Math.min(levels[0] * 100, 100))); 243 | rightLevel.setValue((int) Math.max(0, Math.min(levels[0] * 100, 100))); 244 | } 245 | } else { 246 | leftLevel.setValue(0); 247 | rightLevel.setValue(0); 248 | } 249 | } 250 | }); 251 | 252 | window.setVisible(true); 253 | 254 | }); 255 | 256 | } 257 | 258 | } 259 | -------------------------------------------------------------------------------- /SwingPlayer/src/main/java/org/freedesktop/gstreamer/examples/Utils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 | * 4 | * Copyright 2021 Neil C Smith - Codelerity Ltd. 5 | * 6 | * Copying and distribution of this file, with or without modification, 7 | * are permitted in any medium without royalty provided the copyright 8 | * notice and this notice are preserved. This file is offered as-is, 9 | * without any warranty. 10 | * 11 | */ 12 | package org.freedesktop.gstreamer.examples; 13 | 14 | import com.sun.jna.Platform; 15 | import com.sun.jna.platform.win32.Kernel32; 16 | import java.io.File; 17 | import java.util.stream.Stream; 18 | 19 | /** 20 | * Utility methods for use in examples. 21 | */ 22 | class Utils { 23 | 24 | private Utils() { 25 | } 26 | 27 | /** 28 | * Configures paths to the GStreamer libraries. On Windows queries various 29 | * GStreamer environment variables, and then sets up the PATH environment 30 | * variable. On macOS, adds the location to jna.library.path (macOS binaries 31 | * link to each other). On both, the gstreamer.path system property can be 32 | * used to override. On Linux, assumes GStreamer is in the path already. 33 | */ 34 | static void configurePaths() { 35 | if (Platform.isWindows()) { 36 | String gstPath = System.getProperty("gstreamer.path", findWindowsLocation()); 37 | if (!gstPath.isEmpty()) { 38 | String systemPath = System.getenv("PATH"); 39 | if (systemPath == null || systemPath.trim().isEmpty()) { 40 | Kernel32.INSTANCE.SetEnvironmentVariable("PATH", gstPath); 41 | } else { 42 | Kernel32.INSTANCE.SetEnvironmentVariable("PATH", gstPath 43 | + File.pathSeparator + systemPath); 44 | } 45 | } 46 | } else if (Platform.isMac()) { 47 | String gstPath = System.getProperty("gstreamer.path", 48 | "/Library/Frameworks/GStreamer.framework/Libraries/"); 49 | if (!gstPath.isEmpty()) { 50 | String jnaPath = System.getProperty("jna.library.path", "").trim(); 51 | if (jnaPath.isEmpty()) { 52 | System.setProperty("jna.library.path", gstPath); 53 | } else { 54 | System.setProperty("jna.library.path", jnaPath + File.pathSeparator + gstPath); 55 | } 56 | } 57 | 58 | } 59 | } 60 | 61 | /** 62 | * Query over a stream of possible environment variables for GStreamer 63 | * location, filtering on the first non-null result, and adding \bin\ to the 64 | * value. 65 | * 66 | * @return location or empty string 67 | */ 68 | static String findWindowsLocation() { 69 | if (Platform.is64Bit()) { 70 | return Stream.of("GSTREAMER_1_0_ROOT_MSVC_X86_64", 71 | "GSTREAMER_1_0_ROOT_MINGW_X86_64", 72 | "GSTREAMER_1_0_ROOT_X86_64") 73 | .map(System::getenv) 74 | .filter(p -> p != null) 75 | .map(p -> p.endsWith("\\") ? p + "bin\\" : p + "\\bin\\") 76 | .findFirst().orElse(""); 77 | } else { 78 | return ""; 79 | } 80 | } 81 | 82 | } 83 | -------------------------------------------------------------------------------- /WebRTCSendRecv/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java' 2 | apply plugin: 'application' 3 | 4 | 5 | mainClassName = 'org.freedesktop.gstreamer.examples.WebRTCSendRecv' 6 | 7 | repositories { 8 | mavenCentral() 9 | } 10 | 11 | dependencies { 12 | 13 | implementation 'net.java.dev.jna:jna:5.10.0' 14 | implementation 'net.java.dev.jna:jna-platform:5.10.0' 15 | implementation 'org.freedesktop.gstreamer:gst1-java-core:1.4.0' 16 | 17 | implementation 'org.asynchttpclient:async-http-client:2.12.3' 18 | implementation 'com.fasterxml.jackson.core:jackson-databind:2.12.3' 19 | implementation 'org.slf4j:slf4j-jdk14:1.7.30' 20 | 21 | } 22 | 23 | run { 24 | standardInput = System.in 25 | } 26 | -------------------------------------------------------------------------------- /WebRTCSendRecv/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gstreamer-java/gst1-java-examples/0667161ac70d3c8394990e8c010947c3a6aed7fe/WebRTCSendRecv/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /WebRTCSendRecv/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /WebRTCSendRecv/gradlew: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # 4 | # Copyright © 2015-2021 the original authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | # 21 | # Gradle start up script for POSIX generated by Gradle. 22 | # 23 | # Important for running: 24 | # 25 | # (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is 26 | # noncompliant, but you have some other compliant shell such as ksh or 27 | # bash, then to run this script, type that shell name before the whole 28 | # command line, like: 29 | # 30 | # ksh Gradle 31 | # 32 | # Busybox and similar reduced shells will NOT work, because this script 33 | # requires all of these POSIX shell features: 34 | # * functions; 35 | # * expansions «$var», «${var}», «${var:-default}», «${var+SET}», 36 | # «${var#prefix}», «${var%suffix}», and «$( cmd )»; 37 | # * compound commands having a testable exit status, especially «case»; 38 | # * various built-in commands including «command», «set», and «ulimit». 39 | # 40 | # Important for patching: 41 | # 42 | # (2) This script targets any POSIX shell, so it avoids extensions provided 43 | # by Bash, Ksh, etc; in particular arrays are avoided. 44 | # 45 | # The "traditional" practice of packing multiple parameters into a 46 | # space-separated string is a well documented source of bugs and security 47 | # problems, so this is (mostly) avoided, by progressively accumulating 48 | # options in "$@", and eventually passing that to Java. 49 | # 50 | # Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, 51 | # and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; 52 | # see the in-line comments for details. 53 | # 54 | # There are tweaks for specific operating systems such as AIX, CygWin, 55 | # Darwin, MinGW, and NonStop. 56 | # 57 | # (3) This script is generated from the Groovy template 58 | # https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt 59 | # within the Gradle project. 60 | # 61 | # You can find Gradle at https://github.com/gradle/gradle/. 62 | # 63 | ############################################################################## 64 | 65 | # Attempt to set APP_HOME 66 | 67 | # Resolve links: $0 may be a link 68 | app_path=$0 69 | 70 | # Need this for daisy-chained symlinks. 71 | while 72 | APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path 73 | [ -h "$app_path" ] 74 | do 75 | ls=$( ls -ld "$app_path" ) 76 | link=${ls#*' -> '} 77 | case $link in #( 78 | /*) app_path=$link ;; #( 79 | *) app_path=$APP_HOME$link ;; 80 | esac 81 | done 82 | 83 | APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit 84 | 85 | APP_NAME="Gradle" 86 | APP_BASE_NAME=${0##*/} 87 | 88 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 89 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 90 | 91 | # Use the maximum available, or set MAX_FD != -1 to use that value. 92 | MAX_FD=maximum 93 | 94 | warn () { 95 | echo "$*" 96 | } >&2 97 | 98 | die () { 99 | echo 100 | echo "$*" 101 | echo 102 | exit 1 103 | } >&2 104 | 105 | # OS specific support (must be 'true' or 'false'). 106 | cygwin=false 107 | msys=false 108 | darwin=false 109 | nonstop=false 110 | case "$( uname )" in #( 111 | CYGWIN* ) cygwin=true ;; #( 112 | Darwin* ) darwin=true ;; #( 113 | MSYS* | MINGW* ) msys=true ;; #( 114 | NONSTOP* ) nonstop=true ;; 115 | esac 116 | 117 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 118 | 119 | 120 | # Determine the Java command to use to start the JVM. 121 | if [ -n "$JAVA_HOME" ] ; then 122 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 123 | # IBM's JDK on AIX uses strange locations for the executables 124 | JAVACMD=$JAVA_HOME/jre/sh/java 125 | else 126 | JAVACMD=$JAVA_HOME/bin/java 127 | fi 128 | if [ ! -x "$JAVACMD" ] ; then 129 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 130 | 131 | Please set the JAVA_HOME variable in your environment to match the 132 | location of your Java installation." 133 | fi 134 | else 135 | JAVACMD=java 136 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 137 | 138 | Please set the JAVA_HOME variable in your environment to match the 139 | location of your Java installation." 140 | fi 141 | 142 | # Increase the maximum file descriptors if we can. 143 | if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then 144 | case $MAX_FD in #( 145 | max*) 146 | MAX_FD=$( ulimit -H -n ) || 147 | warn "Could not query maximum file descriptor limit" 148 | esac 149 | case $MAX_FD in #( 150 | '' | soft) :;; #( 151 | *) 152 | ulimit -n "$MAX_FD" || 153 | warn "Could not set maximum file descriptor limit to $MAX_FD" 154 | esac 155 | fi 156 | 157 | # Collect all arguments for the java command, stacking in reverse order: 158 | # * args from the command line 159 | # * the main class name 160 | # * -classpath 161 | # * -D...appname settings 162 | # * --module-path (only if needed) 163 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. 164 | 165 | # For Cygwin or MSYS, switch paths to Windows format before running java 166 | if "$cygwin" || "$msys" ; then 167 | APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) 168 | CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) 169 | 170 | JAVACMD=$( cygpath --unix "$JAVACMD" ) 171 | 172 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 173 | for arg do 174 | if 175 | case $arg in #( 176 | -*) false ;; # don't mess with options #( 177 | /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath 178 | [ -e "$t" ] ;; #( 179 | *) false ;; 180 | esac 181 | then 182 | arg=$( cygpath --path --ignore --mixed "$arg" ) 183 | fi 184 | # Roll the args list around exactly as many times as the number of 185 | # args, so each arg winds up back in the position where it started, but 186 | # possibly modified. 187 | # 188 | # NB: a `for` loop captures its iteration list before it begins, so 189 | # changing the positional parameters here affects neither the number of 190 | # iterations, nor the values presented in `arg`. 191 | shift # remove old arg 192 | set -- "$@" "$arg" # push replacement arg 193 | done 194 | fi 195 | 196 | # Collect all arguments for the java command; 197 | # * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of 198 | # shell script including quotes and variable substitutions, so put them in 199 | # double quotes to make sure that they get re-expanded; and 200 | # * put everything else in single quotes, so that it's not re-expanded. 201 | 202 | set -- \ 203 | "-Dorg.gradle.appname=$APP_BASE_NAME" \ 204 | -classpath "$CLASSPATH" \ 205 | org.gradle.wrapper.GradleWrapperMain \ 206 | "$@" 207 | 208 | # Use "xargs" to parse quoted args. 209 | # 210 | # With -n1 it outputs one arg per line, with the quotes and backslashes removed. 211 | # 212 | # In Bash we could simply go: 213 | # 214 | # readarray ARGS < <( xargs -n1 <<<"$var" ) && 215 | # set -- "${ARGS[@]}" "$@" 216 | # 217 | # but POSIX shell has neither arrays nor command substitution, so instead we 218 | # post-process each arg (as a line of input to sed) to backslash-escape any 219 | # character that might be a shell metacharacter, then use eval to reverse 220 | # that process (while maintaining the separation between arguments), and wrap 221 | # the whole thing up as a single "set" statement. 222 | # 223 | # This will of course break if any of these variables contains a newline or 224 | # an unmatched quote. 225 | # 226 | 227 | eval "set -- $( 228 | printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | 229 | xargs -n1 | 230 | sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | 231 | tr '\n' ' ' 232 | )" '"$@"' 233 | 234 | exec "$JAVACMD" "$@" 235 | -------------------------------------------------------------------------------- /WebRTCSendRecv/gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 34 | 35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 37 | 38 | @rem Find java.exe 39 | if defined JAVA_HOME goto findJavaFromJavaHome 40 | 41 | set JAVA_EXE=java.exe 42 | %JAVA_EXE% -version >NUL 2>&1 43 | if "%ERRORLEVEL%" == "0" goto execute 44 | 45 | echo. 46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 47 | echo. 48 | echo Please set the JAVA_HOME variable in your environment to match the 49 | echo location of your Java installation. 50 | 51 | goto fail 52 | 53 | :findJavaFromJavaHome 54 | set JAVA_HOME=%JAVA_HOME:"=% 55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 56 | 57 | if exist "%JAVA_EXE%" goto execute 58 | 59 | echo. 60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 61 | echo. 62 | echo Please set the JAVA_HOME variable in your environment to match the 63 | echo location of your Java installation. 64 | 65 | goto fail 66 | 67 | :execute 68 | @rem Setup the command line 69 | 70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 71 | 72 | 73 | @rem Execute Gradle 74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 75 | 76 | :end 77 | @rem End local scope for the variables with windows NT shell 78 | if "%ERRORLEVEL%"=="0" goto mainEnd 79 | 80 | :fail 81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 82 | rem the _cmd.exe /c_ return code! 83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 84 | exit /b 1 85 | 86 | :mainEnd 87 | if "%OS%"=="Windows_NT" endlocal 88 | 89 | :omega 90 | -------------------------------------------------------------------------------- /WebRTCSendRecv/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'WebRTCSendRecv' 2 | 3 | -------------------------------------------------------------------------------- /WebRTCSendRecv/src/main/java/org/freedesktop/gstreamer/examples/Utils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 | * 4 | * Copyright 2021 Neil C Smith - Codelerity Ltd. 5 | * 6 | * Copying and distribution of this file, with or without modification, 7 | * are permitted in any medium without royalty provided the copyright 8 | * notice and this notice are preserved. This file is offered as-is, 9 | * without any warranty. 10 | * 11 | */ 12 | package org.freedesktop.gstreamer.examples; 13 | 14 | import com.sun.jna.Platform; 15 | import com.sun.jna.platform.win32.Kernel32; 16 | import java.io.File; 17 | import java.util.stream.Stream; 18 | 19 | /** 20 | * Utility methods for use in examples. 21 | */ 22 | class Utils { 23 | 24 | private Utils() { 25 | } 26 | 27 | /** 28 | * Configures paths to the GStreamer libraries. On Windows queries various 29 | * GStreamer environment variables, and then sets up the PATH environment 30 | * variable. On macOS, adds the location to jna.library.path (macOS binaries 31 | * link to each other). On both, the gstreamer.path system property can be 32 | * used to override. On Linux, assumes GStreamer is in the path already. 33 | */ 34 | static void configurePaths() { 35 | if (Platform.isWindows()) { 36 | String gstPath = System.getProperty("gstreamer.path", findWindowsLocation()); 37 | if (!gstPath.isEmpty()) { 38 | String systemPath = System.getenv("PATH"); 39 | if (systemPath == null || systemPath.trim().isEmpty()) { 40 | Kernel32.INSTANCE.SetEnvironmentVariable("PATH", gstPath); 41 | } else { 42 | Kernel32.INSTANCE.SetEnvironmentVariable("PATH", gstPath 43 | + File.pathSeparator + systemPath); 44 | } 45 | } 46 | } else if (Platform.isMac()) { 47 | String gstPath = System.getProperty("gstreamer.path", 48 | "/Library/Frameworks/GStreamer.framework/Libraries/"); 49 | if (!gstPath.isEmpty()) { 50 | String jnaPath = System.getProperty("jna.library.path", "").trim(); 51 | if (jnaPath.isEmpty()) { 52 | System.setProperty("jna.library.path", gstPath); 53 | } else { 54 | System.setProperty("jna.library.path", jnaPath + File.pathSeparator + gstPath); 55 | } 56 | } 57 | 58 | } 59 | } 60 | 61 | /** 62 | * Query over a stream of possible environment variables for GStreamer 63 | * location, filtering on the first non-null result, and adding \bin\ to the 64 | * value. 65 | * 66 | * @return location or empty string 67 | */ 68 | static String findWindowsLocation() { 69 | if (Platform.is64Bit()) { 70 | return Stream.of("GSTREAMER_1_0_ROOT_MSVC_X86_64", 71 | "GSTREAMER_1_0_ROOT_MINGW_X86_64", 72 | "GSTREAMER_1_0_ROOT_X86_64") 73 | .map(System::getenv) 74 | .filter(p -> p != null) 75 | .map(p -> p.endsWith("\\") ? p + "bin\\" : p + "\\bin\\") 76 | .findFirst().orElse(""); 77 | } else { 78 | return ""; 79 | } 80 | } 81 | 82 | } 83 | -------------------------------------------------------------------------------- /archive/nbactions.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | run 5 | 6 | jar 7 | 8 | 9 | process-classes 10 | org.codehaus.mojo:exec-maven-plugin:1.2.1:exec 11 | 12 | 13 | -Djna.nosys=true -classpath %classpath ${packageClassName} 14 | java 15 | 16 | 17 | 18 | debug 19 | 20 | jar 21 | 22 | 23 | process-classes 24 | org.codehaus.mojo:exec-maven-plugin:1.2.1:exec 25 | 26 | 27 | -Xdebug -Xrunjdwp:transport=dt_socket,server=n,address=${jpda.address} -Djna.nosys=true -classpath %classpath ${packageClassName} 28 | java 29 | true 30 | 31 | 32 | 33 | profile 34 | 35 | jar 36 | 37 | 38 | process-classes 39 | org.codehaus.mojo:exec-maven-plugin:1.2.1:exec 40 | 41 | 42 | -Djna.nosys=true -classpath %classpath ${packageClassName} 43 | java 44 | 45 | 46 | 47 | run.single.main 48 | 49 | * 50 | 51 | 52 | process-classes 53 | org.codehaus.mojo:exec-maven-plugin:1.2.1:exec 54 | 55 | 56 | -Djna.nosys=true -classpath %classpath ${packageClassName} 57 | java 58 | ${classPathScope} 59 | 60 | 61 | 62 | debug.single.main 63 | 64 | * 65 | 66 | 67 | process-classes 68 | org.codehaus.mojo:exec-maven-plugin:1.2.1:exec 69 | 70 | 71 | -Djna.nosys=true -Xdebug -Xrunjdwp:transport=dt_socket,server=n,address=${jpda.address} -classpath %classpath ${packageClassName} 72 | java 73 | ${classPathScope} 74 | true 75 | 76 | 77 | 78 | -------------------------------------------------------------------------------- /archive/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | org.freedesktop.gstreamer 5 | gst1-java-examples 6 | 1.0.0-SNAPSHOT 7 | jar 8 | gst1-java-examples (archived) 9 | 10 | 11 | net.java.dev.jna 12 | jna 13 | 5.6.0 14 | 15 | 16 | org.freedesktop.gstreamer 17 | gst1-java-core 18 | 1.4.0 19 | 20 | 21 | 22 | UTF-8 23 | 1.8 24 | 1.8 25 | 26 | -------------------------------------------------------------------------------- /archive/src/main/java/org/freedesktop/gstreamer/examples/AppSrcToAppSinkExample.java: -------------------------------------------------------------------------------- 1 | /* 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 | * 4 | * Copyright 2018 Kyle R Wenholz. 5 | * 6 | * Copying and distribution of this file, with or without modification, 7 | * are permitted in any medium without royalty provided the copyright 8 | * notice and this notice are preserved. This file is offered as-is, 9 | * without any warranty. 10 | * 11 | */ 12 | package org.freedesktop.gstreamer.examples; 13 | 14 | 15 | import java.io.File; 16 | import java.io.FileInputStream; 17 | import java.io.FileOutputStream; 18 | import java.io.IOException; 19 | import java.nio.ByteBuffer; 20 | import java.nio.channels.FileChannel; 21 | 22 | import org.freedesktop.gstreamer.Buffer; 23 | import org.freedesktop.gstreamer.Bus; 24 | import org.freedesktop.gstreamer.Element; 25 | import org.freedesktop.gstreamer.ElementFactory; 26 | import org.freedesktop.gstreamer.FlowReturn; 27 | import org.freedesktop.gstreamer.Gst; 28 | import org.freedesktop.gstreamer.GstObject; 29 | import org.freedesktop.gstreamer.Pad; 30 | import org.freedesktop.gstreamer.PadLinkException; 31 | import org.freedesktop.gstreamer.Pipeline; 32 | import org.freedesktop.gstreamer.Sample; 33 | import org.freedesktop.gstreamer.elements.AppSink; 34 | import org.freedesktop.gstreamer.elements.AppSrc; 35 | import org.freedesktop.gstreamer.lowlevel.MainLoop; 36 | 37 | /** 38 | * 39 | * A simple example of transcoding an audio file using AppSink and AppSrc. 40 | * 41 | * Depending on which gstreamer plugins are installed, this example can decode 42 | * nearly any media type, likewise for encoding. Specify the input and output 43 | * file locations with the first two arguments. 44 | * 45 | * @author Kyle R Wenholz (http://krwenholz.com) 46 | */ 47 | public class AppSrcToAppSinkExample { 48 | 49 | private static Pipeline pipe; 50 | private static byte[] soundBytes = null; 51 | 52 | public static void main(String[] args) throws Exception { 53 | Gst.init(); 54 | String inputFileLocation = args[0]; 55 | String outputFileLocation = args[1]; 56 | File soundFile = new File(inputFileLocation); 57 | FileInputStream inStream = null; 58 | FileChannel output = 59 | new FileOutputStream(outputFileLocation).getChannel(); 60 | final MainLoop loop = new MainLoop(); 61 | 62 | if (soundFile.exists()){ 63 | try { 64 | System.out.println("Read media file."); 65 | long fileSize = soundFile.length(); 66 | soundBytes = new byte[(int)fileSize]; 67 | inStream = new FileInputStream(soundFile); 68 | int byteCount = inStream.read(soundBytes); 69 | System.out.println("Number of bytes read: " + byteCount); 70 | } catch (IOException e) { 71 | e.printStackTrace(); 72 | return; 73 | } 74 | } 75 | 76 | AppSrc source = (AppSrc)ElementFactory.make("appsrc", "app-source"); 77 | Element decoder = ElementFactory.make("decodebin", "decoder"); 78 | Element converter = ElementFactory.make("audioconvert", "converter"); 79 | // The encoder element determines your output format. 80 | Element encoder = ElementFactory.make("wavenc", "wavenc"); 81 | //Element encoder = ElementFactory.make("lamemp3enc", "mp3enc"); 82 | AppSink sink = (AppSink)ElementFactory.make("appsink", "audio-output"); 83 | 84 | Pipeline pipe = new Pipeline(); 85 | // We connect to EOS and ERROR on the bus for cleanup and error messages. 86 | Bus bus = pipe.getBus(); 87 | bus.connect(new Bus.EOS() { 88 | 89 | @Override 90 | public void endOfStream(GstObject source) { 91 | System.out.println("Reached end of stream"); 92 | loop.quit(); 93 | } 94 | 95 | }); 96 | 97 | bus.connect(new Bus.ERROR() { 98 | 99 | @Override 100 | public void errorMessage(GstObject source, int code, String message) { 101 | System.out.println("Error detected"); 102 | System.out.println("Error source: " + source.getName()); 103 | System.out.println("Error code: " + code); 104 | System.out.println("Message: " + message); 105 | loop.quit(); 106 | } 107 | }); 108 | 109 | source.set("emit-signals", true); 110 | source.connect(new AppSrc.NEED_DATA() { 111 | 112 | private final ByteBuffer bb = ByteBuffer.wrap(soundBytes); 113 | 114 | @Override 115 | public void needData(AppSrc elem, int size) { 116 | if (bb.hasRemaining()) { 117 | System.out.println("needData: size = " + size); 118 | byte[] tempBuffer; 119 | Buffer buf; 120 | int copyLength = (bb.remaining() >= size) ? size : bb.remaining(); 121 | tempBuffer = new byte[copyLength]; 122 | buf = new Buffer(copyLength); 123 | bb.get(tempBuffer); 124 | System.out.println("Temp Buffer remaining bytes: " + bb.remaining()); 125 | buf.map(true).put(ByteBuffer.wrap(tempBuffer)); 126 | elem.pushBuffer(buf); 127 | } else { 128 | elem.endOfStream(); 129 | } 130 | } 131 | }); 132 | 133 | // We connect to NEW_SAMPLE and NEW_PREROLL because either can come up 134 | // as sources of data, although usually just one does. 135 | sink.set("emit-signals", true); 136 | // sync=false lets us run the pipeline faster than real (based on the file) 137 | // time 138 | sink.set("sync", false); 139 | sink.connect(new AppSink.NEW_SAMPLE() { 140 | @Override 141 | public FlowReturn newSample(AppSink elem) { 142 | Sample sample = elem.pullSample(); 143 | ByteBuffer bytes = sample.getBuffer().map(false); 144 | try { 145 | output.write(bytes); 146 | } catch (Exception e) { 147 | System.err.println(e); 148 | } 149 | sample.dispose(); 150 | return FlowReturn.OK; 151 | } 152 | }); 153 | 154 | sink.connect(new AppSink.NEW_PREROLL() { 155 | 156 | @Override 157 | public FlowReturn newPreroll(AppSink elem) { 158 | Sample sample = elem.pullPreroll(); 159 | ByteBuffer bytes = sample.getBuffer().map(false); 160 | try { 161 | output.write(bytes); 162 | } catch (Exception e) { 163 | System.err.println(e); 164 | } 165 | sample.dispose(); 166 | return FlowReturn.OK; 167 | } 168 | }); 169 | 170 | pipe.addMany(source, decoder, converter, encoder, sink); 171 | source.link(decoder); 172 | converter.link(encoder); 173 | encoder.link(sink); 174 | decoder.connect(new Element.PAD_ADDED() { 175 | 176 | @Override 177 | public void padAdded(Element element, Pad pad) { 178 | System.out.println("Dynamic pad created, linking decoder/converter"); 179 | System.out.println("Pad name: " + pad.getName()); 180 | System.out.println("Pad type: " + pad.getTypeName()); 181 | Pad sinkPad = converter.getStaticPad("sink"); 182 | try { 183 | pad.link(sinkPad); 184 | System.out.println("Pad linked."); 185 | } catch (PadLinkException ex) { 186 | System.out.println("Pad link failed : " + ex.getLinkResult()); 187 | } 188 | } 189 | 190 | }); 191 | 192 | System.out.println("Playing..."); 193 | pipe.play(); 194 | System.out.println("Running..."); 195 | loop.run(); 196 | System.out.println("Returned, stopping playback"); 197 | pipe.stop(); 198 | Gst.deinit(); 199 | Gst.quit(); 200 | 201 | output.close(); 202 | } 203 | } 204 | 205 | -------------------------------------------------------------------------------- /archive/src/main/java/org/freedesktop/gstreamer/examples/BatchTranscode.java: -------------------------------------------------------------------------------- 1 | package org.freedesktop.gstreamer.examples; 2 | 3 | import org.freedesktop.gstreamer.*; 4 | 5 | import java.io.*; 6 | 7 | public class BatchTranscode 8 | { 9 | public static void main(String[] argv) 10 | { 11 | Gst.init(); 12 | 13 | if (argv.length!=2) { 14 | usage(System.err); 15 | System.exit(1); 16 | } 17 | 18 | String srcVideo = argv[0]; 19 | String outFile = argv[1]; 20 | 21 | System.out.println(srcVideo+" -> "+outFile); 22 | 23 | String pipeSpec = "filesrc name=src ! decodebin name=dec ! " + 24 | ("audioconvert ! faac ! queue ! mux. " + 25 | "dec. ! videoconvert " + 26 | "! videorate ! video/x-raw,framerate=30/1 " + 27 | "! x264enc pass=4 quantizer=21 ! queue ! mux. " + 28 | "mpegtsmux name=mux") + 29 | " ! filesink name=dst"; 30 | Pipeline pipe = (Pipeline) Gst.parseLaunch(pipeSpec); 31 | 32 | pipe.getElementByName("src").set("location", srcVideo); 33 | pipe.getElementByName("dst").set("location", outFile); 34 | 35 | Bus bus = pipe.getBus(); 36 | bus.connect((Bus.EOS) gstObject -> System.out.println("EOS "+gstObject)); 37 | bus.connect((Bus.ERROR) (gstObject, i, s) -> System.out.println("ERROR "+i+" "+s+" "+gstObject)); 38 | bus.connect((Bus.WARNING) (gstObject, i, s) -> System.out.println("WARN "+i+" "+s+" "+gstObject)); 39 | bus.connect((Bus.EOS) obj -> Gst.quit() ); 40 | 41 | pipe.play(); 42 | 43 | Gst.main(); 44 | } 45 | 46 | public static void usage(PrintStream ps) 47 | { 48 | ps.println("usage:\n java "+BatchTranscode.class.getName()+" inputVideo output.ts"); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /archive/src/main/java/org/freedesktop/gstreamer/examples/CameraTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 | * 4 | * Copyright 2019 Neil C Smith. 5 | * 6 | * Copying and distribution of this file, with or without modification, 7 | * are permitted in any medium without royalty provided the copyright 8 | * notice and this notice are preserved. This file is offered as-is, 9 | * without any warranty. 10 | * 11 | */ 12 | package org.freedesktop.gstreamer.examples; 13 | 14 | import java.awt.Dimension; 15 | import java.awt.EventQueue; 16 | import javax.swing.JFrame; 17 | import org.freedesktop.gstreamer.Bin; 18 | import org.freedesktop.gstreamer.Gst; 19 | import org.freedesktop.gstreamer.Pipeline; 20 | 21 | /** 22 | * 23 | * @author Neil C Smith (http://neilcsmith.net) 24 | */ 25 | public class CameraTest { 26 | 27 | /** 28 | * @param args the command line arguments 29 | */ 30 | 31 | private static Pipeline pipe; 32 | 33 | public static void main(String[] args) { 34 | 35 | Gst.init("CameraTest", args); 36 | EventQueue.invokeLater(new Runnable() { 37 | 38 | @Override 39 | public void run() { 40 | SimpleVideoComponent vc = new SimpleVideoComponent(); 41 | Bin bin = Gst.parseBinFromDescription( 42 | "autovideosrc ! videoconvert ! capsfilter caps=video/x-raw,width=640,height=480", 43 | true); 44 | pipe = new Pipeline(); 45 | pipe.addMany(bin, vc.getElement()); 46 | Pipeline.linkMany(bin, vc.getElement()); 47 | 48 | JFrame f = new JFrame("Camera Test"); 49 | f.add(vc); 50 | vc.setPreferredSize(new Dimension(640, 480)); 51 | f.pack(); 52 | f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 53 | 54 | pipe.play(); 55 | f.setVisible(true); 56 | } 57 | }); 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /archive/src/main/java/org/freedesktop/gstreamer/examples/MultiSinkExample.java: -------------------------------------------------------------------------------- 1 | /* 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 | * 4 | * Copyright 2018 Neil C Smith. 5 | * 6 | * Copying and distribution of this file, with or without modification, 7 | * are permitted in any medium without royalty provided the copyright 8 | * notice and this notice are preserved. This file is offered as-is, 9 | * without any warranty. 10 | * 11 | */ 12 | package org.freedesktop.gstreamer.examples; 13 | 14 | import java.awt.BorderLayout; 15 | import java.awt.Dimension; 16 | import java.awt.EventQueue; 17 | import javax.swing.Box; 18 | import javax.swing.JButton; 19 | import javax.swing.JFileChooser; 20 | import javax.swing.JFrame; 21 | import org.freedesktop.gstreamer.Bin; 22 | import org.freedesktop.gstreamer.Gst; 23 | import org.freedesktop.gstreamer.elements.AppSink; 24 | import org.freedesktop.gstreamer.elements.PlayBin; 25 | 26 | /** 27 | * 28 | * An example of playing back a video in multiple sinks - one full size in a native 29 | * window, one scaled in a Swing window. Use the File... button to choose a local video file. 30 | * 31 | * @author Neil C Smith (http://neilcsmith.net) 32 | */ 33 | public class MultiSinkExample { 34 | 35 | /** 36 | * @param args the command line arguments 37 | */ 38 | private static PlayBin playbin; 39 | 40 | public static void main(String[] args) { 41 | 42 | Gst.init(); 43 | EventQueue.invokeLater(() -> { 44 | Bin bin = Gst.parseBinFromDescription("tee name=t t. ! queue ! videoconvert ! autovideosink " 45 | + "t. ! queue ! videoconvert ! videoscale ! capsfilter caps=video/x-raw,width=640,height=480 ! appsink name=appsink", true); 46 | 47 | SimpleVideoComponent vc = new SimpleVideoComponent((AppSink) bin.getElementByName("appsink")); 48 | 49 | playbin = new PlayBin("playbin"); 50 | playbin.setVideoSink(bin); 51 | 52 | // set PlayBin flags to not use internal video scaling otherwise don't get native resolution output 53 | // no mapping for GstPlayFlags in bindings yet! 54 | int flags = (int) playbin.get("flags"); 55 | flags |= (1 << 6); 56 | playbin.set("flags", flags); 57 | // 58 | // uncomment this section to switch off audio playback - again GstPlayFlags 59 | // int flags = (int) playbin.get("flags"); 60 | // flags &= ~(1 << 1); 61 | // playbin.set("flags", flags); 62 | // 63 | JFrame window = new JFrame("Multiple Sinks"); 64 | window.add(vc); 65 | vc.setPreferredSize(new Dimension(640, 480)); 66 | Box buttons = Box.createHorizontalBox(); 67 | JButton fileButton = new JButton("File..."); 68 | buttons.add(fileButton); 69 | window.add(buttons, BorderLayout.SOUTH); 70 | window.pack(); 71 | window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 72 | 73 | fileButton.addActionListener(e -> { 74 | JFileChooser fileChooser = new JFileChooser(); 75 | int returnValue = fileChooser.showOpenDialog(window); 76 | if (returnValue == JFileChooser.APPROVE_OPTION) { 77 | playbin.stop(); 78 | playbin.setURI(fileChooser.getSelectedFile().toURI()); 79 | playbin.play(); 80 | } 81 | }); 82 | 83 | window.setVisible(true); 84 | }); 85 | } 86 | 87 | } 88 | -------------------------------------------------------------------------------- /archive/src/main/java/org/freedesktop/gstreamer/examples/PlayBinVideoPlayer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 | * 4 | * Copyright 2021 Neil C Smith. 5 | * 6 | * Copying and distribution of this file, with or without modification, 7 | * are permitted in any medium without royalty provided the copyright 8 | * notice and this notice are preserved. This file is offered as-is, 9 | * without any warranty. 10 | * 11 | */ 12 | package org.freedesktop.gstreamer.examples; 13 | 14 | import java.awt.BorderLayout; 15 | import java.awt.Dimension; 16 | import java.awt.EventQueue; 17 | import java.util.EnumSet; 18 | import java.util.concurrent.TimeUnit; 19 | 20 | import javax.swing.Box; 21 | import javax.swing.JButton; 22 | import javax.swing.JFileChooser; 23 | import javax.swing.JFrame; 24 | import javax.swing.JProgressBar; 25 | import javax.swing.JSlider; 26 | import javax.swing.Timer; 27 | import org.freedesktop.gstreamer.Bus; 28 | import org.freedesktop.gstreamer.Element; 29 | import org.freedesktop.gstreamer.ElementFactory; 30 | import org.freedesktop.gstreamer.Format; 31 | import org.freedesktop.gstreamer.Gst; 32 | import org.freedesktop.gstreamer.Structure; 33 | import org.freedesktop.gstreamer.elements.PlayBin; 34 | import org.freedesktop.gstreamer.event.SeekFlags; 35 | import org.freedesktop.gstreamer.message.Message; 36 | import org.freedesktop.gstreamer.message.MessageType; 37 | 38 | /** 39 | * 40 | * A simple video player using PlayBin and Swing. 41 | * 42 | * @author Neil C Smith (http://neilcsmith.net) 43 | */ 44 | public class PlayBinVideoPlayer { 45 | 46 | public static void main(String[] args) { 47 | 48 | Gst.init(); 49 | EventQueue.invokeLater(() -> { 50 | 51 | // Create a Swing video display component 52 | SimpleVideoComponent vc = new SimpleVideoComponent(); 53 | 54 | // Create a PlayBin element and set the AppSink from the Swing component 55 | // as the video sink. 56 | PlayBin playbin = new PlayBin("playbin"); 57 | playbin.setVideoSink(vc.getElement()); 58 | 59 | // Create a level component and set it as the audio-filter property 60 | // on the playbin - this will post audio level messages to the bus - 61 | // see below how to display them. 62 | Element level = ElementFactory.make("level", "level"); 63 | playbin.set("audio-filter", level); 64 | 65 | // Create a GUI 66 | JFrame window = new JFrame("Video Player"); 67 | window.add(vc); 68 | vc.setPreferredSize(new Dimension(800, 600)); 69 | Box buttons = Box.createHorizontalBox(); 70 | 71 | JButton fileButton = new JButton("File..."); 72 | fileButton.addActionListener(e -> { 73 | JFileChooser fileChooser = new JFileChooser(); 74 | int returnValue = fileChooser.showOpenDialog(window); 75 | if (returnValue == JFileChooser.APPROVE_OPTION) { 76 | playbin.stop(); 77 | playbin.setURI(fileChooser.getSelectedFile().toURI()); 78 | playbin.play(); 79 | } 80 | }); 81 | 82 | JButton playButton = new JButton("Play"); 83 | playButton.addActionListener(e -> playbin.play()); 84 | JButton pauseButton = new JButton("Pause"); 85 | pauseButton.addActionListener(e -> playbin.pause()); 86 | 87 | // position slider 88 | JSlider position = new JSlider(0, 1000); 89 | position.addChangeListener(e -> { 90 | if (position.getValueIsAdjusting()) { 91 | long dur = playbin.queryDuration(Format.TIME); 92 | long pos = playbin.queryPosition(Format.TIME); 93 | if (dur > 0) { 94 | double relPos = position.getValue() / 1000.0; 95 | playbin.seekSimple(Format.TIME, 96 | EnumSet.of(SeekFlags.FLUSH, SeekFlags.KEY_UNIT), 97 | (long) (relPos * dur)); 98 | } 99 | } 100 | }); 101 | // sync slider position to video when not dragging 102 | new Timer(50, e -> { 103 | if (!position.getValueIsAdjusting()) { 104 | long dur = playbin.queryDuration(Format.TIME); 105 | long pos = playbin.queryPosition(Format.TIME); 106 | if (dur > 0) { 107 | double relPos = (double) pos / dur; 108 | position.setValue((int) (relPos * 1000)); 109 | } 110 | } 111 | }).start(); 112 | 113 | // quick and dirty level display using JProgressBar 114 | Box levels = Box.createVerticalBox(); 115 | JProgressBar leftLevel = new JProgressBar(); 116 | leftLevel.setMaximumSize(new Dimension(200, 20)); 117 | JProgressBar rightLevel = new JProgressBar(); 118 | rightLevel.setMaximumSize(new Dimension(200, 20)); 119 | levels.add(leftLevel); 120 | levels.add(rightLevel); 121 | 122 | buttons.add(fileButton); 123 | buttons.add(playButton); 124 | buttons.add(pauseButton); 125 | buttons.add(position); 126 | buttons.add(levels); 127 | window.add(buttons, BorderLayout.SOUTH); 128 | 129 | window.pack(); 130 | window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 131 | 132 | // listen for level messages on the bus 133 | playbin.getBus().connect("element", new Bus.MESSAGE() { 134 | 135 | @Override 136 | public void busMessage(Bus arg0, Message message) { 137 | if (message.getType() == MessageType.ELEMENT 138 | && message.getSource().getName().startsWith("level")) { 139 | Structure struct = message.getStructure(); 140 | // We can get either rms or peak 141 | double[] levels = struct.getDoubles("peak"); 142 | // Calculate the time offset required to get the level 143 | // information in sync with the video display 144 | long timeDelay = getTimeOffset(struct); 145 | Gst.getExecutor().schedule( 146 | () -> EventQueue.invokeLater(() -> updateLevelDisplay(levels)), 147 | timeDelay, TimeUnit.NANOSECONDS); 148 | } 149 | message.dispose(); 150 | } 151 | 152 | private long getTimeOffset(Structure struct) { 153 | long actualTime = playbin.getClock().getTime() 154 | - playbin.getBaseTime(); 155 | long runningTime = (long) struct.getValue("running-time"); 156 | long duration = (long) struct.getValue("duration"); 157 | long messageTime = runningTime + (duration / 2); 158 | return messageTime - actualTime; 159 | } 160 | 161 | private void updateLevelDisplay(double[] levels) { 162 | if (playbin.isPlaying() && levels.length > 0) { 163 | // convert levels for display 164 | for (int i = 0; i < levels.length; i++) { 165 | levels[i] = Math.pow(10, levels[i] / 20); 166 | } 167 | if (levels.length >= 2) { 168 | leftLevel.setValue((int) Math.max(0, Math.min(levels[0] * 100, 100))); 169 | rightLevel.setValue((int) Math.max(0, Math.min(levels[1] * 100, 100))); 170 | } else { 171 | leftLevel.setValue((int) Math.max(0, Math.min(levels[0] * 100, 100))); 172 | rightLevel.setValue((int) Math.max(0, Math.min(levels[0] * 100, 100))); 173 | } 174 | } else { 175 | leftLevel.setValue(0); 176 | rightLevel.setValue(0); 177 | } 178 | } 179 | }); 180 | 181 | window.setVisible(true); 182 | 183 | }); 184 | } 185 | 186 | } 187 | -------------------------------------------------------------------------------- /archive/src/main/java/org/freedesktop/gstreamer/examples/TextColorControlExample.java: -------------------------------------------------------------------------------- 1 | /* 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 | * 4 | * Copyright 2021 Neil C Smith / Tim-Philipp Müller 5 | * 6 | * Copying and distribution of this file, with or without modification, 7 | * are permitted in any medium without royalty provided the copyright 8 | * notice and this notice are preserved. This file is offered as-is, 9 | * without any warranty. 10 | * 11 | */ 12 | package org.freedesktop.gstreamer.examples; 13 | 14 | import java.util.concurrent.TimeUnit; 15 | import org.freedesktop.gstreamer.Element; 16 | import org.freedesktop.gstreamer.ElementFactory; 17 | import org.freedesktop.gstreamer.Gst; 18 | import org.freedesktop.gstreamer.Pipeline; 19 | import org.freedesktop.gstreamer.controller.ARGBControlBinding; 20 | import org.freedesktop.gstreamer.controller.DirectControlBinding; 21 | import org.freedesktop.gstreamer.controller.LFOControlSource; 22 | 23 | /** 24 | * Adapted from the C example by Tim-Philipp Müller at 25 | * 26 | * https://gitlab.freedesktop.org/gstreamer/gstreamer/blob/master/tests/examples/controller/text-color-example.c 27 | */ 28 | public class TextColorControlExample { 29 | 30 | private static Pipeline pipe; 31 | 32 | public static void main(String[] args) { 33 | Gst.init("TextColorExample"); 34 | 35 | pipe = new Pipeline(); 36 | 37 | Element src = ElementFactory.make("videotestsrc", "src"); 38 | src.set("pattern", 11); 39 | 40 | Element text = ElementFactory.make("textoverlay", "text"); 41 | text.set("text", "GStreamer rocks!"); 42 | text.set("font-desc", "Sans, 42"); 43 | text.set("halignment", 4); 44 | text.set("valignment", 3); 45 | 46 | Element capsfilter = ElementFactory.make("capsfilter", "caps"); 47 | capsfilter.setAsString("caps", "video/x-raw, width=800, height=600"); 48 | 49 | Element sink = ElementFactory.make("autovideosink", "sink"); 50 | 51 | pipe.addMany(src, text, capsfilter, sink); 52 | Pipeline.linkMany(src, text, capsfilter, sink); 53 | 54 | 55 | LFOControlSource csXPos = new LFOControlSource(); 56 | csXPos.setFrequency(0.11).setAmplitude(0.2).setOffset(0.5); 57 | text.addControlBinding(DirectControlBinding.create(text, "xpos", csXPos)); 58 | 59 | LFOControlSource csYPos = new LFOControlSource(); 60 | csYPos.setFrequency(0.04).setAmplitude(0.4).setOffset(0.5); 61 | text.addControlBinding(DirectControlBinding.create(text, "ypos", csYPos)); 62 | 63 | LFOControlSource csR = new LFOControlSource(); 64 | csR.setFrequency(0.19).setAmplitude(0.5).setOffset(0.5); 65 | LFOControlSource csG = new LFOControlSource(); 66 | csG.setFrequency(0.27).setAmplitude(0.5).setOffset(0.5); 67 | LFOControlSource csB = new LFOControlSource(); 68 | csB.setFrequency(0.13).setAmplitude(0.5).setOffset(0.5); 69 | text.addControlBinding(ARGBControlBinding.create(text, "color", null, csR, csG, csB)); 70 | 71 | pipe.play(); 72 | 73 | Gst.getExecutor().schedule(Gst::quit, 15, TimeUnit.SECONDS); 74 | 75 | Gst.main(); 76 | 77 | } 78 | 79 | } 80 | --------------------------------------------------------------------------------