media

package
v0.1.33 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package media defines operation-neutral image, audio, and video value types. It deliberately does not depend on the parent inference package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AudioDurationMillis added in v0.1.3

func AudioDurationMillis(data []byte, format AudioFormat) (int64, bool)

AudioDurationMillis derives the duration of an encoded audio payload in milliseconds when it can be computed without a full decoder. Raw PCM families are exact from the byte count; MPEG audio (mp3) is estimated from its frame headers. ok reports false for encodings that require a real decoder (opus, aac, flac) or for malformed payloads, so callers can distinguish "unknown" from a real zero.

Types

type AspectRatio

type AspectRatio string

func (AspectRatio) Validate

func (r AspectRatio) Validate() error

type AudioChunk

type AudioChunk struct {
	Data     []byte `json:"data"`
	Sequence uint64 `json:"sequence,omitempty"`
}

func (AudioChunk) Clone

func (c AudioChunk) Clone() AudioChunk

func (AudioChunk) Validate

func (c AudioChunk) Validate() error

type AudioEncoding

type AudioEncoding string
const (
	AudioEncodingPCM16   AudioEncoding = "pcm16"
	AudioEncodingPCM24   AudioEncoding = "pcm24"
	AudioEncodingFloat32 AudioEncoding = "float32"
	AudioEncodingMP3     AudioEncoding = "mp3"
	AudioEncodingOpus    AudioEncoding = "opus"
	AudioEncodingAAC     AudioEncoding = "aac"
	AudioEncodingFLAC    AudioEncoding = "flac"
)

func (AudioEncoding) MediaType

func (e AudioEncoding) MediaType() string

type AudioFormat

type AudioFormat struct {
	Encoding     AudioEncoding `json:"encoding"`
	SampleRateHz int           `json:"sample_rate_hz,omitempty"`
	Channels     int           `json:"channels,omitempty"`
}

func (AudioFormat) Validate

func (f AudioFormat) Validate() error

type AudioSource

type AudioSource struct {
	// contains filtered or unexported fields
}

func NewAudioBytes

func NewAudioBytes(data []byte, mediaType string) (AudioSource, error)

func NewAudioStream added in v0.1.17

func NewAudioStream[T any](stream Stream[T], mediaType string) (AudioSource, error)

NewAudioStream wraps a live, pull-based stream as an audio source. The stream's item type is caller-defined (the message package instantiates it as message.Stream, i.e. media.Stream[Part]); mediaType declares the audio codec family and is required.

func NewAudioURL

func NewAudioURL(rawURL, mediaType string) (AudioSource, error)

func (AudioSource) BaseMediaType

func (s AudioSource) BaseMediaType() string

func (AudioSource) Bytes

func (s AudioSource) Bytes() []byte

func (AudioSource) Clone

func (s AudioSource) Clone() AudioSource

func (AudioSource) Kind

func (s AudioSource) Kind() SourceKind

func (AudioSource) MarshalJSON

func (s AudioSource) MarshalJSON() ([]byte, error)

func (AudioSource) MediaType

func (s AudioSource) MediaType() string

func (AudioSource) Stream added in v0.1.17

func (s AudioSource) Stream() any

Stream returns the live item stream carried by a stream-kind source, or nil for URL and inline sources. The concrete item type is caller-defined; a message.Stream caller asserts the returned handle to message.Stream.

func (AudioSource) URL

func (s AudioSource) URL() string

func (*AudioSource) UnmarshalJSON

func (s *AudioSource) UnmarshalJSON(data []byte) error

func (AudioSource) Validate

func (s AudioSource) Validate() error

type ImageFormat

type ImageFormat string
const (
	ImageFormatPNG  ImageFormat = "png"
	ImageFormatJPEG ImageFormat = "jpeg"
	ImageFormatWebP ImageFormat = "webp"
	ImageFormatGIF  ImageFormat = "gif"
	ImageFormatAVIF ImageFormat = "avif"
)

func (ImageFormat) MediaType

func (f ImageFormat) MediaType() string

func (ImageFormat) Validate

func (f ImageFormat) Validate() error

type ImageSize

type ImageSize struct {
	Width  int `json:"width"`
	Height int `json:"height"`
}

func (ImageSize) Validate

func (s ImageSize) Validate() error

type ImageSource

type ImageSource struct {
	// contains filtered or unexported fields
}

func NewImageBytes

func NewImageBytes(data []byte, mediaType string) (ImageSource, error)

func NewImageURL

func NewImageURL(rawURL, mediaType string) (ImageSource, error)

func (ImageSource) BaseMediaType

func (s ImageSource) BaseMediaType() string

func (ImageSource) Bytes

func (s ImageSource) Bytes() []byte

func (ImageSource) Clone

func (s ImageSource) Clone() ImageSource

func (ImageSource) Kind

func (s ImageSource) Kind() SourceKind

func (ImageSource) MarshalJSON

func (s ImageSource) MarshalJSON() ([]byte, error)

func (ImageSource) MediaType

func (s ImageSource) MediaType() string

func (ImageSource) Stream added in v0.1.17

func (s ImageSource) Stream() any

Stream returns the live item stream carried by a stream-kind source, or nil for URL and inline sources. The concrete item type is caller-defined; a message.Stream caller asserts the returned handle to message.Stream.

func (ImageSource) URL

func (s ImageSource) URL() string

func (*ImageSource) UnmarshalJSON

func (s *ImageSource) UnmarshalJSON(data []byte) error

func (ImageSource) Validate

func (s ImageSource) Validate() error

type Pipe added in v0.1.17

type Pipe[T any] struct {
	// contains filtered or unexported fields
}

Pipe is the standard implementation of Stream[T]: a typed, bounded pipe with explicit normal-close and interrupt semantics.

Producers call Send; it blocks when the buffer is full, which is how backpressure propagates from a slow consumer. Consumers call Read. Close ends the stream normally (Read drains buffered items, then returns io.EOF). Interrupt aborts the stream: Read returns context.Canceled immediately, even when buffered items remain.

func NewPipe added in v0.1.17

func NewPipe[T any](bufferSize int) *Pipe[T]

NewPipe creates a buffered Pipe with the given capacity. bufferSize must be non-negative.

func (*Pipe[T]) Close added in v0.1.17

func (p *Pipe[T]) Close()

Close signals normal end of stream. After all buffered values are consumed, Read returns io.EOF. Safe to call multiple times (idempotent via sync.Once).

func (*Pipe[T]) Interrupt added in v0.1.17

func (p *Pipe[T]) Interrupt()

Interrupt signals abnormal end of stream. Read returns context.Canceled immediately, even if buffered values remain. Idempotent.

func (*Pipe[T]) Read added in v0.1.17

func (p *Pipe[T]) Read(ctx context.Context) (T, error)

Read returns the next value from the pipe.

  • Returns io.EOF after Close once all buffered values are consumed.
  • Returns context.Canceled immediately after Interrupt, skipping buffered values.
  • Returns ctx.Err() when the caller's context is canceled. This is a per-call cancellation: the stream itself stays usable.

After Read returns a stream-level error (io.EOF or context.Canceled), all subsequent Read calls return the same error.

func (*Pipe[T]) Send added in v0.1.17

func (p *Pipe[T]) Send(v T) bool

Send writes a value into the pipe, blocking while the buffer is full (backpressure). It returns false when the pipe has been interrupted. Callers must not call Send after Close: sending on a closed channel panics per Go semantics.

func (*Pipe[T]) TrySend added in v0.1.17

func (p *Pipe[T]) TrySend(v T) bool

TrySend writes a value into the pipe without blocking. It returns false when the pipe is interrupted or the buffer is full.

type SourceKind

type SourceKind string
const (
	SourceURL    SourceKind = "url"
	SourceInline SourceKind = "inline"
	// SourceStream marks a live, pull-based source of typed items (see
	// [Stream]). It is the transport form of a media part: valid while a
	// message is in flight, never in durable context or history — callers
	// materialize it (message.MaterializeContent) before storage.
	SourceStream SourceKind = "stream"
)

func (SourceKind) Validate

func (k SourceKind) Validate() error

type Stream added in v0.1.17

type Stream[T any] interface {
	Read(ctx context.Context) (T, error)
}

Stream is a pull-based source of typed items.

Contract:

  • Read returns (value, nil) for each available item.
  • Read returns (zero, io.EOF) when the stream ends normally.
  • Read returns (zero, err) when the stream is interrupted or the caller's context is canceled.
  • After returning a non-nil error caused by the stream itself (EOF or interrupt), all subsequent Read calls return the same error.

The item type is instantiated by the owner of the stream: the message package exposes Stream[Part] as the canonical live-content transport.

type VideoFrame

type VideoFrame struct {
	Source          ImageSource `json:"source"`
	TimestampMillis int64       `json:"timestamp_millis,omitempty"`
}

VideoFrame is a timestamped image frame for live multimodal input. Complete video-generation request and response types are intentionally out of scope.

func (VideoFrame) Clone

func (f VideoFrame) Clone() VideoFrame

func (VideoFrame) Validate

func (f VideoFrame) Validate() error

type VideoSource

type VideoSource struct {
	// contains filtered or unexported fields
}

func NewVideoBytes

func NewVideoBytes(data []byte, mediaType string) (VideoSource, error)

func NewVideoStream added in v0.1.17

func NewVideoStream[T any](stream Stream[T], mediaType string) (VideoSource, error)

NewVideoStream wraps a live, pull-based stream as a video source. The stream's item type is caller-defined; mediaType declares the video container family and is required.

func NewVideoURL

func NewVideoURL(rawURL, mediaType string) (VideoSource, error)

func (VideoSource) BaseMediaType

func (s VideoSource) BaseMediaType() string

func (VideoSource) Bytes

func (s VideoSource) Bytes() []byte

func (VideoSource) Clone

func (s VideoSource) Clone() VideoSource

func (VideoSource) Kind

func (s VideoSource) Kind() SourceKind

func (VideoSource) MarshalJSON

func (s VideoSource) MarshalJSON() ([]byte, error)

func (VideoSource) MediaType

func (s VideoSource) MediaType() string

func (VideoSource) Stream added in v0.1.17

func (s VideoSource) Stream() any

Stream returns the live item stream carried by a stream-kind source, or nil for URL and inline sources. The concrete item type is caller-defined; a message.Stream caller asserts the returned handle to message.Stream.

func (VideoSource) URL

func (s VideoSource) URL() string

func (*VideoSource) UnmarshalJSON

func (s *VideoSource) UnmarshalJSON(data []byte) error

func (VideoSource) Validate

func (s VideoSource) Validate() error

type VoiceSpec

type VoiceSpec struct {
	ID       string `json:"id"`
	Language string `json:"language,omitempty"`
}

func (VoiceSpec) Validate

func (v VoiceSpec) Validate() error

Jump to

Keyboard shortcuts

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