github.com/cnotch/ipchub@v1.1.0/av/format/rtp/demuxer_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 rtp
     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/sdp"
    17  	"github.com/cnotch/xlog"
    18  	"github.com/stretchr/testify/assert"
    19  )
    20  
    21  var demuxerTestCases = []struct {
    22  	sdpFile string
    23  	rtpFile string
    24  	frames  frameWriter
    25  }{
    26  	{"game.sdp", "game.rtp", frameWriter{1354, 1937}},
    27  	{"music.sdp", "music.rtp", frameWriter{1505, 2569}},
    28  	{"265.sdp", "265.rtp", frameWriter{5828, 11395}},
    29  	// {"4k.rtp", frameWriter{898, 1359, 28, 0, 27}},
    30  }
    31  
    32  func TestDemuxer(t *testing.T) {
    33  	assertsPath := "../../../test/asserts/"
    34  	channels := []int{int(ChannelVideo), int(ChannelVideoControl), int(ChannelAudio), int(ChannelAudioControl)}
    35  	for _, tt := range demuxerTestCases {
    36  		t.Run(tt.rtpFile, func(t *testing.T) {
    37  			sdpbytes, err := ioutil.ReadFile(assertsPath + tt.sdpFile)
    38  			if err != nil {
    39  				t.Error(err)
    40  				return
    41  			}
    42  			var video codec.VideoMeta
    43  			var audio codec.AudioMeta
    44  			err = sdp.ParseMetadata(string(sdpbytes), &video, &audio)
    45  			if err != nil {
    46  				t.Error(err)
    47  				return
    48  			}
    49  
    50  			file, err := os.Open(assertsPath + tt.rtpFile)
    51  			if err != nil {
    52  				t.Error(err)
    53  				return
    54  			}
    55  			defer file.Close()
    56  
    57  			reader := bufio.NewReader(file)
    58  			fw := &frameWriter{}
    59  			demuxer, err := NewDemuxer(&video, &audio, fw, xlog.L())
    60  			if err != nil {
    61  				t.Error(err)
    62  			}
    63  			defer demuxer.Close()
    64  
    65  			for {
    66  				packet, err := ReadPacket(reader, channels)
    67  				if err == io.EOF {
    68  					break
    69  				}
    70  				if err != nil {
    71  					t.Errorf("read packet error :%s", err.Error())
    72  					break
    73  				}
    74  				demuxer.WriteRtpPacket(packet)
    75  			}
    76  			<-time.After(time.Second)
    77  
    78  			assert.Equal(t, tt.frames, *fw)
    79  		})
    80  	}
    81  }
    82  
    83  type frameWriter struct {
    84  	videoFrames int
    85  	audioFrames int
    86  }
    87  
    88  func (fw *frameWriter) WriteFrame(frame *codec.Frame) (err error) {
    89  	dts := frame.Dts / int64(time.Millisecond)
    90  	pts := frame.Pts / int64(time.Millisecond)
    91  	_ = dts
    92  	_ = pts
    93  	if frame.MediaType == codec.MediaTypeVideo {
    94  		fw.videoFrames++
    95  	} else {
    96  		fw.audioFrames++
    97  	}
    98  	return
    99  }