github.com/cnotch/ipchub@v1.1.0/av/format/mpegts/mpegts_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 mpegts
     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  func TestMpegtsWriter(t *testing.T) {
    22  	assertsPath := "../../../test/asserts/"
    23  	sdpraw, err := ioutil.ReadFile(assertsPath + "music.sdp")
    24  	if err != nil {
    25  		panic("Couldn't open sdp")
    26  	}
    27  
    28  	file, err := os.Open(assertsPath + "music.rtp")
    29  	if err != nil {
    30  		panic("Couldn't open rtp")
    31  	}
    32  	defer file.Close()
    33  	reader := bufio.NewReader(file)
    34  
    35  	out, err := os.OpenFile(assertsPath+"music.ts", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, os.ModePerm)
    36  	if err != nil {
    37  		panic("Couldn't open ts")
    38  	}
    39  	defer out.Close()
    40  	var video codec.VideoMeta
    41  	var audio codec.AudioMeta
    42  	sdp.ParseMetadata(string(sdpraw), &video, &audio)
    43  	writer, err := NewWriter(out)
    44  	tsMuxer, _ := NewMuxer(&video, &audio, writer, xlog.L())
    45  
    46  	rtpDemuxer, _ := rtp.NewDemuxer(&video, &audio, tsMuxer, xlog.L())
    47  	channels := []int{int(rtp.ChannelVideo), int(rtp.ChannelVideoControl), int(rtp.ChannelAudio), int(rtp.ChannelAudioControl)}
    48  	for {
    49  		packet, err := rtp.ReadPacket(reader, channels)
    50  		if err == io.EOF {
    51  			break
    52  		}
    53  		if err != nil {
    54  			t.Logf("read packet error :%s", err.Error())
    55  		}
    56  		rtpDemuxer.WriteRtpPacket(packet)
    57  	}
    58  
    59  	<-time.After(time.Millisecond * 1000)
    60  	rtpDemuxer.Close()
    61  	tsMuxer.Close()
    62  }