Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

applied goimports/gofmt #58

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 45 additions & 45 deletions av/av.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

// Package av defines basic interfaces and data structures of container demux/mux and audio encode/decode.
package av

Expand All @@ -11,17 +10,17 @@ import (
type SampleFormat uint8

const (
U8 = SampleFormat(iota + 1) // 8-bit unsigned integer
S16 // signed 16-bit integer
S32 // signed 32-bit integer
FLT // 32-bit float
DBL // 64-bit float
U8P // 8-bit unsigned integer in planar
S16P // signed 16-bit integer in planar
S32P // signed 32-bit integer in planar
FLTP // 32-bit float in planar
DBLP // 64-bit float in planar
U32 // unsigned 32-bit integer
U8 = SampleFormat(iota + 1) // 8-bit unsigned integer
S16 // signed 16-bit integer
S32 // signed 32-bit integer
FLT // 32-bit float
DBL // 64-bit float
U8P // 8-bit unsigned integer in planar
S16P // signed 16-bit integer in planar
S32P // signed 32-bit integer in planar
FLTP // 32-bit float in planar
DBLP // 64-bit float in planar
U32 // unsigned 32-bit integer
)

func (self SampleFormat) BytesPerSample() int {
Expand Down Expand Up @@ -116,11 +115,11 @@ func (self ChannelLayout) Count() (n int) {
type CodecType uint32

var (
H264 = MakeVideoCodecType(avCodecTypeMagic + 1)
AAC = MakeAudioCodecType(avCodecTypeMagic + 1)
PCM_MULAW = MakeAudioCodecType(avCodecTypeMagic + 2)
PCM_ALAW = MakeAudioCodecType(avCodecTypeMagic + 3)
SPEEX = MakeAudioCodecType(avCodecTypeMagic + 4)
H264 = MakeVideoCodecType(avCodecTypeMagic + 1)
AAC = MakeAudioCodecType(avCodecTypeMagic + 1)
PCM_MULAW = MakeAudioCodecType(avCodecTypeMagic + 2)
PCM_ALAW = MakeAudioCodecType(avCodecTypeMagic + 3)
SPEEX = MakeAudioCodecType(avCodecTypeMagic + 4)
NELLYMOSER = MakeAudioCodecType(avCodecTypeMagic + 5)
)

Expand Down Expand Up @@ -171,23 +170,25 @@ const avCodecTypeMagic = 233333
// can be converted to VideoCodecData or AudioCodecData using:
//
// codecdata.(AudioCodecData) or codecdata.(VideoCodecData)
//
//
// for H264, CodecData is AVCDecoderConfigure bytes, includes SPS/PPS.
type CodecData interface {
Type() CodecType // Video/Audio codec type
}

type VideoCodecData interface {
CodecData
Width() int // Video height
Height() int // Video width
Width() int // Video width
Height() int // Video height
}

type AudioCodecData interface {
CodecData
SampleFormat() SampleFormat // audio sample format
SampleRate() int // audio sample rate
ChannelLayout() ChannelLayout // audio channel layout
SampleFormat() SampleFormat // audio sample format
SampleRate() int // audio sample rate
ChannelLayout() ChannelLayout // audio channel layout
PacketDuration([]byte) (time.Duration, error) // get audio compressed packet duration
}

Expand All @@ -196,16 +197,16 @@ type PacketWriter interface {
}

type PacketReader interface {
ReadPacket() (Packet,error)
ReadPacket() (Packet, error)
}

// Muxer describes the steps of writing compressed audio/video packets into container formats like MP4/FLV/MPEG-TS.
//
//
// Container formats, rtmp.Conn, and transcode.Muxer implements Muxer interface.
type Muxer interface {
WriteHeader([]CodecData) error // write the file header
PacketWriter // write compressed audio/video packets
WriteTrailer() error // finish writing file, this func can be called only once
PacketWriter // write compressed audio/video packets
WriteTrailer() error // finish writing file, this func can be called only once
}

// Muxer with Close() method
Expand All @@ -216,7 +217,7 @@ type MuxCloser interface {

// Demuxer can read compressed audio/video packets from container formats like MP4/FLV/MPEG-TS.
type Demuxer interface {
PacketReader // read compressed audio/video packets
PacketReader // read compressed audio/video packets
Streams() ([]CodecData, error) // reads the file header, contains video/audio meta infomations
}

Expand All @@ -228,20 +229,20 @@ type DemuxCloser interface {

// Packet stores compressed audio/video data.
type Packet struct {
IsKeyFrame bool // video packet is key frame
Idx int8 // stream index in container format
IsKeyFrame bool // video packet is key frame
Idx int8 // stream index in container format
CompositionTime time.Duration // packet presentation time minus decode time for H264 B-Frame
Time time.Duration // packet decode time
Data []byte // packet data
Time time.Duration // packet decode time
Data []byte // packet data
}

// Raw audio frame.
type AudioFrame struct {
SampleFormat SampleFormat // audio sample format, e.g: S16,FLTP,...
SampleFormat SampleFormat // audio sample format, e.g: S16,FLTP,...
ChannelLayout ChannelLayout // audio channel layout, e.g: CH_MONO,CH_STEREO,...
SampleCount int // sample count in this frame
SampleRate int // sample rate
Data [][]byte // data array for planar format len(Data) > 1
SampleCount int // sample count in this frame
SampleRate int // sample rate
Data [][]byte // data array for planar format len(Data) > 1
}

func (self AudioFrame) Duration() time.Duration {
Expand Down Expand Up @@ -291,26 +292,25 @@ func (self AudioFrame) Concat(in AudioFrame) (out AudioFrame) {
// AudioEncoder can encode raw audio frame into compressed audio packets.
// cgo/ffmpeg inplements AudioEncoder, using ffmpeg.NewAudioEncoder to create it.
type AudioEncoder interface {
CodecData() (AudioCodecData, error) // encoder's codec data can put into container
Encode(AudioFrame) ([][]byte, error) // encode raw audio frame into compressed pakcet(s)
Close() // close encoder, free cgo contexts
SetSampleRate(int) (error) // set encoder sample rate
SetChannelLayout(ChannelLayout) (error) // set encoder channel layout
SetSampleFormat(SampleFormat) (error) // set encoder sample format
SetBitrate(int) (error) // set encoder bitrate
SetOption(string,interface{}) (error) // encoder setopt, in ffmpeg is av_opt_set_dict()
GetOption(string,interface{}) (error) // encoder getopt
CodecData() (AudioCodecData, error) // encoder's codec data can put into container
Encode(AudioFrame) ([][]byte, error) // encode raw audio frame into compressed pakcet(s)
Close() // close encoder, free cgo contexts
SetSampleRate(int) error // set encoder sample rate
SetChannelLayout(ChannelLayout) error // set encoder channel layout
SetSampleFormat(SampleFormat) error // set encoder sample format
SetBitrate(int) error // set encoder bitrate
SetOption(string, interface{}) error // encoder setopt, in ffmpeg is av_opt_set_dict()
GetOption(string, interface{}) error // encoder getopt
}

// AudioDecoder can decode compressed audio packets into raw audio frame.
// use ffmpeg.NewAudioDecoder to create it.
type AudioDecoder interface {
Decode([]byte) (bool, AudioFrame, error) // decode one compressed audio packet
Close() // close decode, free cgo contexts
Close() // close decode, free cgo contexts
}

// AudioResampler can convert raw audio frames in different sample rate/format/channel layout.
type AudioResampler interface {
Resample(AudioFrame) (AudioFrame, error) // convert raw audio frames
}

22 changes: 11 additions & 11 deletions av/avconv/avconv.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"fmt"
"io"
"time"
"github.com/nareix/joy4/av/avutil"

"github.com/nareix/joy4/av"
"github.com/nareix/joy4/av/avutil"
"github.com/nareix/joy4/av/pktque"
"github.com/nareix/joy4/av/transcode"
)
Expand All @@ -14,7 +15,7 @@ var Debug bool

type Option struct {
Transcode bool
Args []string
Args []string
}

type Options struct {
Expand All @@ -23,7 +24,7 @@ type Options struct {

type Demuxer struct {
transdemux *transcode.Demuxer
streams []av.CodecData
streams []av.CodecData
Options
Demuxer av.Demuxer
}
Expand Down Expand Up @@ -56,10 +57,10 @@ func (self *Demuxer) prepare() (err error) {
}

/*
var streams []av.CodecData
if streams, err = self.Demuxer.Streams(); err != nil {
return
}
var streams []av.CodecData
if streams, err = self.Demuxer.Streams(); err != nil {
return
}
*/

supports := self.Options.OutputCodecTypes
Expand All @@ -83,7 +84,7 @@ func (self *Demuxer) prepare() (err error) {
ok = true

var enctype av.CodecType
for _, typ:= range supports {
for _, typ := range supports {
if typ.IsAudio() {
if enc, _ = avutil.DefaultHandlers.NewAudioEncoder(typ); enc != nil {
enctype = typ
Expand Down Expand Up @@ -152,7 +153,7 @@ func ConvertCmdline(args []string) (err error) {
flagt = false
var f float64
fmt.Sscanf(arg, "%f", &f)
duration = time.Duration(f*float64(time.Second))
duration = time.Duration(f * float64(time.Second))

default:
output = arg
Expand Down Expand Up @@ -223,7 +224,7 @@ func ConvertCmdline(args []string) (err error) {
}
filterdemux := &pktque.FilterDemuxer{
Demuxer: convdemux,
Filter: filters,
Filter: filters,
}

for {
Expand Down Expand Up @@ -252,4 +253,3 @@ func ConvertCmdline(args []string) (err error) {

return
}

41 changes: 21 additions & 20 deletions av/avutil/avutil.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package avutil

import (
"io"
"strings"
"fmt"
"bytes"
"github.com/nareix/joy4/av"
"fmt"
"io"
"net/url"
"os"
"path"
"strings"

"github.com/nareix/joy4/av"
)

type HandlerDemuxer struct {
Expand All @@ -22,7 +23,7 @@ func (self *HandlerDemuxer) Close() error {

type HandlerMuxer struct {
av.Muxer
w io.WriteCloser
w io.WriteCloser
stage int
}

Expand Down Expand Up @@ -54,18 +55,18 @@ func (self *HandlerMuxer) Close() (err error) {
}

type RegisterHandler struct {
Ext string
ReaderDemuxer func(io.Reader)av.Demuxer
WriterMuxer func(io.Writer)av.Muxer
UrlMuxer func(string)(bool,av.MuxCloser,error)
UrlDemuxer func(string)(bool,av.DemuxCloser,error)
UrlReader func(string)(bool,io.ReadCloser,error)
Probe func([]byte)bool
AudioEncoder func(av.CodecType)(av.AudioEncoder,error)
AudioDecoder func(av.AudioCodecData)(av.AudioDecoder,error)
ServerDemuxer func(string)(bool,av.DemuxCloser,error)
ServerMuxer func(string)(bool,av.MuxCloser,error)
CodecTypes []av.CodecType
Ext string
ReaderDemuxer func(io.Reader) av.Demuxer
WriterMuxer func(io.Writer) av.Muxer
UrlMuxer func(string) (bool, av.MuxCloser, error)
UrlDemuxer func(string) (bool, av.DemuxCloser, error)
UrlReader func(string) (bool, io.ReadCloser, error)
Probe func([]byte) bool
AudioEncoder func(av.CodecType) (av.AudioEncoder, error)
AudioDecoder func(av.AudioCodecData) (av.AudioDecoder, error)
ServerDemuxer func(string) (bool, av.DemuxCloser, error)
ServerMuxer func(string) (bool, av.MuxCloser, error)
CodecTypes []av.CodecType
}

type Handlers struct {
Expand Down Expand Up @@ -167,7 +168,7 @@ func (self *Handlers) Open(uri string) (demuxer av.DemuxCloser, err error) {
}
demuxer = &HandlerDemuxer{
Demuxer: handler.ReaderDemuxer(r),
r: r,
r: r,
}
return
}
Expand Down Expand Up @@ -196,7 +197,7 @@ func (self *Handlers) Open(uri string) (demuxer av.DemuxCloser, err error) {
}
demuxer = &HandlerDemuxer{
Demuxer: handler.ReaderDemuxer(_r),
r: r,
r: r,
}
return
}
Expand Down Expand Up @@ -254,7 +255,7 @@ func (self *Handlers) FindCreate(uri string) (handler RegisterHandler, muxer av.
}
muxer = &HandlerMuxer{
Muxer: handler.WriterMuxer(w),
w: w,
w: w,
}
return
}
Expand Down
17 changes: 8 additions & 9 deletions av/pktque/filters.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

// Package pktque provides packet Filter interface and structures used by other components.
package pktque

import (
"time"

"github.com/nareix/joy4/av"
)

Expand All @@ -30,8 +30,8 @@ func (self Filters) ModifyPacket(pkt *av.Packet, streams []av.CodecData, videoid
// Wrap origin Demuxer and Filter into a new Demuxer, when read this Demuxer filters will be called.
type FilterDemuxer struct {
av.Demuxer
Filter Filter
streams []av.CodecData
Filter Filter
streams []av.CodecData
videoidx int
audioidx int
}
Expand Down Expand Up @@ -81,9 +81,9 @@ func (self *WaitKeyFrame) ModifyPacket(pkt *av.Packet, streams []av.CodecData, v

// Fix incorrect packet timestamps.
type FixTime struct {
zerobase time.Duration
incrbase time.Duration
lasttime time.Duration
zerobase time.Duration
incrbase time.Duration
lasttime time.Duration
StartFromZero bool // make timestamp start from zero
MakeIncrement bool // force timestamp increment
}
Expand Down Expand Up @@ -114,14 +114,14 @@ func (self *FixTime) ModifyPacket(pkt *av.Packet, streams []av.CodecData, videoi
// Drop incorrect packets to make A/V sync.
type AVSync struct {
MaxTimeDiff time.Duration
time []time.Duration
time []time.Duration
}

func (self *AVSync) ModifyPacket(pkt *av.Packet, streams []av.CodecData, videoidx int, audioidx int) (drop bool, err error) {
if self.time == nil {
self.time = make([]time.Duration, len(streams))
if self.MaxTimeDiff == 0 {
self.MaxTimeDiff = time.Millisecond*500
self.MaxTimeDiff = time.Millisecond * 500
}
}

Expand Down Expand Up @@ -188,4 +188,3 @@ func (self *Walltime) ModifyPacket(pkt *av.Packet, streams []av.CodecData, video
}
return
}

Loading