Giter VIP home page Giter VIP logo

omxplayer's Introduction

omxplayer

Build Status GoDoc

omxplayer is a simple library for controlling omxplayer on your Raspberry Pi from a Go application.

Table of Contents

Requirements

  • The omxplayer library requires a build of the omxplayer application later than the 1d20cdc1be commit.

Getting Started

The omxplayer library follows the standard Go package format, allowing you to start using it with a single command:

go get github.com/jleight/omxplayer

Once the library has been downloaded to your Go path, you can import it in your project to start using it:

import (
	"github.com/jleight/omxplayer"
)

Note on Design Choices

The omxplayer application and D-Bus were designed such that:

  1. The omxplayer application provides multi-user support by appending the name of the user that is running the process to the file containing the D-Bus connection information.
  2. D-Bus requires authentication in which the user's name and home directory need to be provided.

The normal way of getting the current user's username and home directory would be to use the os/user package. However, as of the time of writing this, the os/user package does not work on the Raspberry Pi.

For this reason, we have opted to use os.Getenv to get the USER and HOME environment variables in order to connect to omxplayer via D-Bus.

If, for any reason, either the USER or HOME environment variables are not available, you must specify the user and the user's home directory before attempting to start a new omxplayer instance. This can be done with the SetUser method.

omxplayer.SetUser("someuser", "/home/someuser")

If you are using this library to build an executable to run as a service, you could choose to pass in the user and home directory as command line arguments:

if len(os.Args) == 3 {
	omxplayer.SetUser(os.Args[1], os.Args[2])
}

Usage

Now that you've downloaded and imported the omxplayer library in your application, you can use it to start up a new instance of omxplayer, providing a path to the file you would like to play:

player, err := omxplayer.New("/path/to/video.mp4", "--no-osd")

This will start a new omxplayer process that will play the specified video file. The original purpose of this library required that the omxplayer instance have time to buffer the video before playing it, so this library starts the omxplayer instance and immediately pauses it.

Sometimes it takes a while (a few hundred milliseconds) for omxplayer to write its D-Bus information to a file. As a precaution, this library includes both an IsReady and WaitForReady method. These can be used to check if the Player instance is ready to start accepting D-Bus commands, or to wait until the Player is ready, respectively. It is recommended that you either make sure that the Player instance IsReady before issuing any other commands, or that you WaitForReady if you cannot do anything else.

Now that you have a Player instance, you can control it through any of the D-Bus methods described in the D-Bus Control section of the omxplayer application's README.

Example

Here's an example to bring it all together:

omxplayer.SetUser("root", "/root")
player, err := omxplayer.New("/root/testvideo.mp4", "--no-osd")

player.WaitForReady()
err = player.PlayPause()

time.Sleep(5 * time.Second)
err = player.ShowSubtitles()

time.Sleep(5 * time.Second)
err = player.Quit()

Of course, all of the D-Bus methods return errors, so you should make sure handle them appropriately.

License

The omxplayer library is available under the MIT license. See the LICENSE file for the full license text.

omxplayer's People

Contributors

jleight avatar wildsurfer avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.