mjpeg

package module
v0.0.0-...-54b8ada Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 13, 2020 License: MIT Imports: 5 Imported by: 1

README

Simple Motion JPEG Streaming

Docs Build
GoDoc Build Status

Super simple mjpeg streaming in Go.

Getting Started

Get to package with go get github.com/nsmith5/mjpeg. An MJPeg stream can be built using any function that takes no arguments and returns an image.

package main

import (
    "log"
    "net/http"

    "github.com/nsmith5/mjpeg"
)

func main() {
    stream := mjpeg.Handler{
        Next: func()  (image.Image, error) {
            img := image.NewGray(image.Rect(0, 0, 100, 100))
            for i := 0; i < 100; i++ {
                for j := 0; j < 100; j++ {
                    n := rand.Intn(256)
                    gray := color.Gray{uint8(n)}
                    img.SetGray(i, j, gray)
                }
            }
            return img, nil
        },
        Options: &jpeg.Options{Quality: 80},
    }

    mux := http.NewServeMux()
    mux.Handle("/stream", stream)
    log.Fatal(http.ListenAndServe(":8080", mux))
}

Documentation

Overview

Package mjpeg implements mjpeg streaming handlers with a simple API.

Example
stream := func() (image.Image, error) {
	img := image.NewGray(image.Rect(0, 0, 100, 100))
	for i := 0; i < 100; i++ {
		for j := 0; j < 100; j++ {
			n := rand.Intn(256)
			gray := color.Gray{uint8(n)}
			img.SetGray(i, j, gray)
		}
	}
	return img, nil
}

handler := Handler{
	Next:    stream,
	Options: &jpeg.Options{Quality: 60},
}

mux := http.NewServeMux()
mux.Handle("/stream", handler)
log.Fatal(http.ListenAndServe(":8080", mux))
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrorEndOfStream = errors.New("End of Motion JPEG Stream")

ErrorEndOfStream signals the end of the Motion JPEG frames from a MJPEG stream

Functions

This section is empty.

Types

type Handler

type Handler struct {
	Next    func() (image.Image, error)
	Options *jpeg.Options
}

A Handler is an http.Handler that streams mjpeg using an image stream. Encoding quality can be controlled using the Options parameters.

func (Handler) ServeHTTP

func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL