-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #77 from OneHeng/opus
Opus
- Loading branch information
Showing
15 changed files
with
1,255 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package muxer | ||
|
||
import ( | ||
"io" | ||
|
||
"github.com/abema/go-mp4" | ||
"github.com/q191201771/naza/pkg/nazalog" | ||
) | ||
|
||
type Init struct { | ||
Tracks []*Track | ||
} | ||
|
||
func (i *Init) Encode(w io.WriteSeeker) error { | ||
/* | ||
|ftyp| | ||
|moov| | ||
| |mvhd| | ||
| |trak| | ||
| |trak| | ||
| |....| | ||
| |mvex| | ||
| | |trex| | ||
| | |trex| | ||
| | |....| | ||
*/ | ||
|
||
mw := newMP4Writer(w) | ||
|
||
// ftyp box | ||
_, err := mw.writeBox(&mp4.Ftyp{ | ||
MajorBrand: [4]byte{'m', 'p', '4', '2'}, | ||
MinorVersion: 1, | ||
CompatibleBrands: []mp4.CompatibleBrandElem{ | ||
{ | ||
CompatibleBrand: [4]byte{'m', 'p', '4', '1'}, | ||
}, | ||
{ | ||
CompatibleBrand: [4]byte{'m', 'p', '4', '2'}, | ||
}, | ||
{ | ||
CompatibleBrand: [4]byte{'i', 's', 'o', 'm'}, | ||
}, | ||
{ | ||
CompatibleBrand: [4]byte{'h', 'l', 's', 'f'}, | ||
}, | ||
}, | ||
}) | ||
|
||
if err != nil { | ||
nazalog.Error("write ftyp box failed, err:", err) | ||
return err | ||
} | ||
|
||
// moov box | ||
_, err = mw.writeBoxStart(&mp4.Moov{}) | ||
if err != nil { | ||
nazalog.Error("write moov box failed, err:", err) | ||
return err | ||
} | ||
|
||
// mvhd box | ||
_, err = mw.writeBox(&mp4.Mvhd{ | ||
Timescale: 1000, | ||
Rate: 65536, | ||
Volume: 256, | ||
Matrix: [9]int32{0x00010000, 0, 0, 0, 0x00010000, 0, 0, 0, 0x40000000}, | ||
NextTrackID: 4294967295, | ||
}) | ||
|
||
if err != nil { | ||
nazalog.Error("write mvhd box failed, err:", err) | ||
return err | ||
} | ||
|
||
// track box | ||
for _, track := range i.Tracks { | ||
err = track.Encode(mw) | ||
if err != nil { | ||
nazalog.Error("track encode failed, err:", err) | ||
return err | ||
} | ||
} | ||
|
||
// mvex box | ||
_, err = mw.writeBoxStart(&mp4.Mvex{}) | ||
if err != nil { | ||
nazalog.Error("write mvex box failed, err:", err) | ||
return err | ||
} | ||
|
||
// trex box | ||
for _, track := range i.Tracks { | ||
_, err = mw.writeBox(&mp4.Trex{ | ||
TrackID: uint32(track.ID), | ||
DefaultSampleDescriptionIndex: 1, | ||
}) | ||
|
||
if err != nil { | ||
nazalog.Error("write trex box failed, err:", err) | ||
return err | ||
} | ||
} | ||
|
||
// </mvex> | ||
err = mw.writeBoxEnd() | ||
if err != nil { | ||
nazalog.Error("write mvex end box failed, err:", err) | ||
return err | ||
} | ||
|
||
// </moov> | ||
err = mw.writeBoxEnd() | ||
if err != nil { | ||
nazalog.Error("write moov end box failed, err:", err) | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package muxer | ||
|
||
import ( | ||
"io" | ||
|
||
"github.com/abema/go-mp4" | ||
) | ||
|
||
type mp4Writer struct { | ||
w *mp4.Writer | ||
} | ||
|
||
func newMP4Writer(w io.WriteSeeker) *mp4Writer { | ||
return &mp4Writer{ | ||
w: mp4.NewWriter(w), | ||
} | ||
} | ||
|
||
func (w *mp4Writer) writeBoxStart(box mp4.IImmutableBox) (int, error) { | ||
bi := &mp4.BoxInfo{ | ||
Type: box.GetType(), | ||
} | ||
var err error | ||
bi, err = w.w.StartBox(bi) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
_, err = mp4.Marshal(w.w, box, mp4.Context{}) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
return int(bi.Offset), nil | ||
} | ||
|
||
func (w *mp4Writer) writeBoxEnd() error { | ||
_, err := w.w.EndBox() | ||
return err | ||
} | ||
|
||
func (w *mp4Writer) writeBox(box mp4.IImmutableBox) (int, error) { | ||
off, err := w.writeBoxStart(box) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
err = w.writeBoxEnd() | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
return off, nil | ||
} | ||
|
||
func (w *mp4Writer) rewriteBox(off int, box mp4.IImmutableBox) error { | ||
prevOff, err := w.w.Seek(0, io.SeekCurrent) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = w.w.Seek(int64(off), io.SeekStart) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = w.writeBoxStart(box) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = w.writeBoxEnd() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = w.w.Seek(prevOff, io.SeekStart) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.