-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstream.go
More file actions
113 lines (86 loc) · 2.08 KB
/
stream.go
File metadata and controls
113 lines (86 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package gmf
/*
#cgo pkg-config: libavformat
#include "libavformat/avformat.h"
*/
import "C"
import (
"fmt"
)
type Stream struct {
avStream *C.struct_AVStream
cc *CodecCtx
Pts int64
CgoMemoryManage
}
func (s *Stream) Free() {
// nothing to do
}
func (s *Stream) DumpContexCodec(codec *CodecCtx) {
ret := C.avcodec_copy_context(s.avStream.codec, codec.avCodecCtx)
if ret < 0 {
panic("Failed to copy context from input to output stream codec context\n")
}
}
func (s *Stream) SetCodecFlags() {
s.avStream.codec.flags |= C.CODEC_FLAG_GLOBAL_HEADER
}
func (s *Stream) CodecCtx() *CodecCtx {
if s.IsCodecCtxSet() {
return s.cc
}
// @todo make explicit decoder/encoder definition
// If the codec context wasn't set, it means that it's called from InputCtx
// and it should be decoder.
c, err := FindDecoder(int(s.avStream.codec.codec_id))
if err != nil {
panic(fmt.Errorf("error initializing codec for stream '%d' - %s", s.Index(), err))
}
s.cc = &CodecCtx{
codec: c,
avCodecCtx: s.avStream.codec,
}
s.cc.Open(nil)
return s.cc
}
func (s *Stream) SetCodecCtx(cc *CodecCtx) {
if cc == nil {
// don't sure that it should panic...
panic("Codec context is not initialized.")
}
Retain(cc) //just Retain .not need Release,it can free memory by C.avformat_free_context() @ format.go Free().
s.avStream.codec = cc.avCodecCtx
if s.cc != nil {
s.cc.avCodecCtx = cc.avCodecCtx
}
}
func (s *Stream) IsCodecCtxSet() bool {
return (s.cc != nil)
}
func (s *Stream) Index() int {
return int(s.avStream.index)
}
func (s *Stream) Id() int {
return int(s.avStream.id)
}
func (s *Stream) NbFrames() int {
if int(s.avStream.nb_frames) == 0 {
return 1
}
return int(s.avStream.nb_frames)
}
func (s *Stream) TimeBase() AVRational {
return AVRational(s.avStream.time_base)
}
func (s *Stream) Type() int32 {
return s.CodecCtx().Type()
}
func (s *Stream) IsAudio() bool {
return (s.Type() == AVMEDIA_TYPE_AUDIO)
}
func (s *Stream) IsVideo() bool {
return (s.Type() == AVMEDIA_TYPE_VIDEO)
}
func (s *Stream) Duration() int64 {
return int64(s.avStream.duration)
}