github.com/cnotch/ipchub@v1.1.0/av/format/flv/flv_test.go (about)

     1  // Copyright (c) 2019,CAOHONGJU All rights reserved.
     2  // Use of this source code is governed by a MIT-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package flv
     6  
     7  import (
     8  	"bufio"
     9  	"io"
    10  	"io/ioutil"
    11  	"os"
    12  	"testing"
    13  	"time"
    14  
    15  	"github.com/cnotch/ipchub/av/codec"
    16  	"github.com/cnotch/ipchub/av/format/rtp"
    17  	"github.com/cnotch/ipchub/av/format/sdp"
    18  	"github.com/cnotch/xlog"
    19  )
    20  
    21  var muxerTestCases = []struct {
    22  	sdpFile string
    23  	rtpFile string
    24  	flvFile string
    25  }{
    26  	{"game.sdp", "game.rtp", "game.flv"},
    27  	{"music.sdp", "music.rtp", "music.flv"},
    28  	{"265.sdp", "265.rtp", "265.flv"},
    29  }
    30  
    31  func TestFlvWriter(t *testing.T) {
    32  	assertsPath := "../../../test/asserts/"
    33  
    34  	for _, tt := range muxerTestCases {
    35  		t.Run(tt.rtpFile, func(t *testing.T) {
    36  
    37  			sdpraw, err := ioutil.ReadFile(assertsPath + tt.sdpFile)
    38  			if err != nil {
    39  				panic("Couldn't open sdp")
    40  			}
    41  
    42  			file, err := os.Open(assertsPath + tt.rtpFile)
    43  			if err != nil {
    44  				panic("Couldn't open rtp")
    45  			}
    46  			defer file.Close()
    47  			reader := bufio.NewReader(file)
    48  
    49  			out, err := os.OpenFile(assertsPath+tt.flvFile, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, os.ModePerm)
    50  			if err != nil {
    51  				panic("Couldn't open flv")
    52  			}
    53  			defer out.Close()
    54  			var video codec.VideoMeta
    55  			var audio codec.AudioMeta
    56  			sdp.ParseMetadata(string(sdpraw), &video, &audio)
    57  			writer, err := NewWriter(out, 5)
    58  			flvMuxer, _ := NewMuxer(&video, &audio, writer, xlog.L())
    59  
    60  			rtpDemuxer, _ := rtp.NewDemuxer(&video, &audio, flvMuxer, xlog.L())
    61  			channels := []int{int(rtp.ChannelVideo), int(rtp.ChannelVideoControl), int(rtp.ChannelAudio), int(rtp.ChannelAudioControl)}
    62  			for {
    63  				packet, err := rtp.ReadPacket(reader, channels)
    64  				if err == io.EOF {
    65  					break
    66  				}
    67  				if err != nil {
    68  					t.Logf("read packet error :%s", err.Error())
    69  				}
    70  				rtpDemuxer.WriteRtpPacket(packet)
    71  			}
    72  
    73  			<-time.After(time.Millisecond * 1000)
    74  			rtpDemuxer.Close()
    75  			flvMuxer.Close()
    76  		})
    77  	}
    78  }