Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.60 KB | None | 0 0
  1. package main
  2. //import block====================================================================================================================================================================//
  3. import (
  4. "os"
  5. "net"
  6. "fmt"
  7. "golang.org/x/net/ipv4"
  8. "bytes"
  9. "bufio"
  10. "sync"
  11. "time"
  12. "strings"
  13. "encoding/binary"
  14. )
  15. //var declarations====================================================================================================================================================================//
  16. var (
  17. FilterPID = 0
  18. FilterFlag = false
  19. StreamLock = sync.RWMutex{}
  20. ProgramLock = sync.RWMutex{}
  21. Programs = make(map[uint16]uint16)
  22. Streams = make(map[uint16]uint16)
  23. RFILTER = ""
  24. )
  25. //Main function====================================================================================================================================================================//
  26. func main() {
  27.  
  28.  
  29. ArgLen := len(os.Args)
  30. var FPID = "0x0000"
  31. var ETH = "eth0"
  32. var SOURCE string
  33. var GROUP string
  34. var PORT string
  35. for i:= 0;i < ArgLen;i++ {
  36. val := os.Args[i]
  37. if strings.Contains(val, "-P") {
  38. FPID = os.Args[i+1]
  39. }
  40. if strings.Contains(val, "-I") {
  41. ETH = os.Args[i+1]
  42. }
  43. if strings.Contains(val, "-r") {
  44. RFILTER = os.Args[i+1]
  45. }
  46. if strings.Contains(val, "-s") {
  47. SOURCE = os.Args[i+1]
  48. }
  49. if strings.Contains(val, "-g") {
  50. GROUP = os.Args[i+1]
  51. }
  52. if strings.Contains(val, "-p") {
  53. PORT = os.Args[i+1]
  54. }
  55. if strings.Contains(val, "-h") {
  56. fmt.Println(`
  57. =====================
  58. *CLTSAnalyzer v0.9b *
  59. *Yashi Sharma @VIPER*
  60. *2017-03-27 *
  61. =====================
  62. Options:
  63. -P - Specify the PID to filter to. Defaults to 0(PAT)
  64. -I - Specify the interface to use. Defaults to 'eth0'
  65. -r - Specify a Regex filter to filter the output.
  66. -s - Specify Address to use as Source
  67. -g - Specify Address to use as Group
  68. -p - Specify Port
  69. -h - Display this help message
  70.  
  71. WARNING: Using this on a high throughput PID(e. Video Streams) and outputting to the console can overrun the program. I recommend piping out to a file to reduce load.
  72.  
  73. `)
  74. return
  75. }
  76. }
  77.  
  78.  
  79. //options block===================================================
  80. //Mainly for enabling debug output for now.===================================================
  81. //===================================================
  82.  
  83. fmt.Sscanf(FPID, "0x%X", &FilterPID)
  84. en0, _:= net.InterfaceByName(ETH) // Get interface. This will be an option at some point
  85.  
  86. fmt.Printf("Filter PID: 0x%X\n", FilterPID)
  87. fmt.Printf("Interface: %s\n", ETH)
  88. fmt.Printf("RegexFilter: %s\n", RFILTER)
  89. fmt.Printf("Source: %s\n", SOURCE)
  90. fmt.Printf("Group: %s\n", GROUP)
  91. fmt.Printf("Port: %s\n", PORT)
  92.  
  93. c, _ := net.ListenPacket("udp4", "0.0.0.0:"+PORT) //Start up connection
  94. p := ipv4.NewPacketConn(c) //Actually connect to the SGP.
  95. if err := p.JoinSourceSpecificGroup(en0, &net.UDPAddr{IP: net.ParseIP(GROUP)}, &net.UDPAddr{IP: net.ParseIP(SOURCE)}); err != nil {
  96. fmt.Println(err)
  97. }
  98.  
  99. for ;; { //loop until done.
  100. payload:= make([]byte, 5000) //Too big. Will get resized in 2 lines
  101. n, _, _ := c.ReadFrom(payload) // get real length of the payload
  102. payload = payload[:n] // Resize Payload and ultimately deallocate the unused reserved memory
  103.  
  104. for ;len(payload)>0;payload= payload[188:] {
  105. go MTSDecode(payload[:188]) //start decoding the Payload, which is a misnomer, because it should be called "packet".
  106. }
  107.  
  108. }
  109.  
  110. if err := p.LeaveSourceSpecificGroup(en0, &net.UDPAddr{IP: net.ParseIP(GROUP)}, &net.UDPAddr{IP: net.ParseIP(SOURCE)}); err != nil { //Leave SGP join
  111. fmt.Println(err)
  112. }//*/
  113.  
  114. fmt.Println("End") //Print "end" so people know you're done.
  115. }
  116. //Start Decode====================================================================================================================================================================//
  117. func MTSDecode(b []byte) { //gothread placeholder method
  118. packet := *bytes.NewBuffer(b) // Buffers make bytes easier to work with.
  119. responseStr := new(bytes.Buffer)
  120. responseWriter := bufio.NewWriter(responseStr)
  121. MpegTSHeaderAnalyze(packet, responseWriter) //start analysis
  122. responseWriter.Flush()
  123. fmt.Print(responseStr)
  124. }
  125.  
  126. func getProgramMapping(PID uint16) uint16 {
  127. ProgramLock.RLock()
  128. defer ProgramLock.RUnlock()
  129. return Programs[PID]
  130. }
  131. func getStreamMapping(PID uint16) uint16 {
  132. StreamLock.RLock()
  133. defer StreamLock.RUnlock()
  134. return Streams[PID]
  135. }
  136. func setProgramMapping(PID uint16, value uint16) {
  137. ProgramLock.Lock()
  138. defer ProgramLock.Unlock()
  139. Programs[PID] = value
  140. }
  141. func setStreamMapping(PID uint16, value uint16) {
  142. StreamLock.Lock()
  143. defer StreamLock.Unlock()
  144. Streams[PID] = value
  145. }
  146.  
  147. //Analyze TS Header====================================================================================================================================================================//
  148. func MpegTSHeaderAnalyze(packet bytes.Buffer, response *bufio.Writer) int {
  149. SYNC, _ := packet.ReadByte() //Sync Byte
  150.  
  151. HeaderSecond, _ := packet.ReadByte() //Second part of the header
  152. HeaderThird, _ := packet.ReadByte()//third part of the header
  153. HeaderFourth, _ := packet.ReadByte() // Fourth part of the header
  154.  
  155. TEI := HeaderSecond&0x80>>7 //Transport Error Indicator
  156. PUSI := HeaderSecond&0x40>>6 //Payload Unit Start Indicator
  157. TSPri := HeaderSecond&0x20>>5 //TS Private Data Indicator
  158. PID := binary.BigEndian.Uint16([]byte{HeaderSecond&0x1F, HeaderThird&0xFF}) //Program Identifier
  159.  
  160. TSC := HeaderFourth&0xC0>>6 //Transport Scrambling Control
  161. AFC := HeaderFourth&0x30>>4 //Adaptation Field Control bit 1 = Adaptation Field Presence, Bit 2 = Payload Presence. b00 is reserved
  162. CC := HeaderFourth&0x0F //Continuity Counter
  163.  
  164. //fmt.Fprintf(response, "%04X, %04X\n", PID, FilterPID)
  165.  
  166. if PID == uint16(FilterPID) {
  167. FilterFlag = true
  168. }else {
  169. FilterFlag = false
  170. }
  171.  
  172. StartTime := time.Now()
  173.  
  174. if FilterFlag ==true { //Print if debug text enabled
  175. fmt.Fprintf(response, "\n###############################################################################################################################################################\n")
  176. fmt.Fprintf(response, "\n\tHeader: [Recieved Time: %s\t\tSync Byte: x%X\t\tTransport Error: b%X\t\tPayload Start: b%X\n\t\tTS Priority: b%X\t\tPID: x%X\t\tScrambling Control: d%d\t\tAdaptation Field Control: d%d\n\t\tContinuity Counter: d%d]\n", StartTime, SYNC, TEI, PUSI, TSPri, PID, TSC, AFC, CC)
  177. }
  178.  
  179. if AFC == 0x02 || AFC == 0x03 { //b10 and b11
  180. MpegAdaptationFieldAnalyze(packet, response) //analyze Adaptation Field
  181. }
  182. if AFC == 0x01 || AFC == 0x03 { //b01 and b11
  183. if PID == 0x00 { //if PAT..
  184. MpegPATAnalyze(packet, response)//Analyze PSI. NEEDS TO BE REPLACED BY ANALYZE PAT HELPER++++++++++++++++++++++++++++++++++++++++++++++
  185. }
  186. if getProgramMapping(uint16(PID)) != 0x00 { //if PMT..
  187. MpegPMTAnalyze(packet, response)//Analyze PSI. NEEDS TO BE REPLACED BY ANALYZE PAT HELPER++++++++++++++++++++++++++++++++++++++++++++++
  188. }
  189. if getStreamMapping(uint16((PID))) == 0x86 { //if PMT..
  190. MpegSCTE35Analyze(packet, response )//Analyze PSI. NEEDS TO BE REPLACED BY ANALYZE PAT HELPER++++++++++++++++++++++++++++++++++++++++++++++
  191. }
  192. }
  193. EndTime := time.Now()
  194. DiffTime := EndTime.Sub(StartTime)
  195. if FilterFlag ==true { //Print if debug text enabled
  196. fmt.Fprintf(response, "\n\tDecode Time: %s\n", DiffTime)
  197. fmt.Fprintf(response, "###############################################################################################################################################################\n")
  198. }
  199.  
  200. return 1 //return blah
  201. }
  202. //Adaptation Field Analyze====================================================================================================================================================================//
  203. func MpegAdaptationFieldAnalyze(packet bytes.Buffer, response *bufio.Writer) {
  204.  
  205. AdaptationFieldLength, _ := packet.ReadByte() //Important! Determines the number of bytes following this one.
  206. if AdaptationFieldLength > 0 { //(Assuming the AFLen is longer than 0(Which does happen sometimes. RESEARCH THIS)
  207. HeaderSecond, _ := packet.ReadByte() //Second Byte. Provides the following:
  208. count := 1 //Byte count, starting after AFLen.
  209. DCI := HeaderSecond&0x80>>7 //Discontinuity indicator
  210. RAI := HeaderSecond&0x40>>6 //Random Access Indicator
  211. ESPri := HeaderSecond&0x20>>5 //Elementary Stream Private Data
  212. PCRFlag := HeaderSecond&0x10>>4 //PCR indicator
  213. OPCRFlag := HeaderSecond&0x08>>3 //Original PCR Indicator
  214. SpliceFlag := HeaderSecond&0x04>>2 //Splice Flag
  215. PrivFlag := HeaderSecond&0x02>>1 //Private Data Flag
  216. AExFlag := HeaderSecond&0x01 //Adaptation Extension Flag
  217.  
  218. if PCRFlag == 1 { //If there is a PCR indicated..
  219. PCR := packet.Next(6)//PCR = 6 Bytes. 33/9 bits for the two different clocks
  220. if FilterFlag == true {
  221. fmt.Fprintf(response, "\t\tPCR: x%X\n", PCR)//print if debug text
  222. }
  223. count += 6 //inc by 6 bytes
  224. }
  225.  
  226. if OPCRFlag == 1 {// If there is an OPCR indicated..
  227. OPCR := packet.Next(6) //Same as PCR
  228. if FilterFlag == true {
  229. fmt.Fprintf(response, "\t\tOPCR: x%X\n", OPCR)//Print if debug text
  230. }
  231. count += 6//inc by 6 bytes
  232. }
  233.  
  234. AdFiNulls := packet.Next(int(AdaptationFieldLength)-count) //The remainder of the packet. Should equal xFF through and through. (Null stuffing section) (Its important for advancing the buffer.)
  235. if FilterFlag == true {
  236. fmt.Fprintf(response, "\tAdaptation: [Adaptation Field Length: x%X\t\tDiscontinuity: b%b\t\tRandom Access Indicator: b%b\t\tElementary Stream Priority: b%b\t\tPCR Flag: b%b\n\t\tOPCR Flag: b%b\t\tSplicing Flag: b%b\t\tPrivate Data Flag: b%b\t\tAdaptation Extension: b%b]\n",AdaptationFieldLength, DCI , RAI, ESPri, PCRFlag, OPCRFlag, SpliceFlag, PrivFlag, AExFlag)
  237. fmt.Fprintf(response, "\t\tAdaptation Field Extra Bytes: x%X\n", AdFiNulls) //print if debug text
  238. }
  239. }
  240. }
  241. //Analyze PSI====================================================================================================================================================================//
  242. func MpegPATAnalyze(packet bytes.Buffer, response *bufio.Writer ) {
  243.  
  244. PointerField := packet.Next(1)
  245.  
  246. for i:=0;i<int(PointerField[0]);i++ {
  247. packet.Next(1)
  248. }
  249.  
  250. PATHeader := packet.Next(8)
  251.  
  252. TableID := PATHeader[0]
  253. SectionSyntaxIndicator := PATHeader[1]&0x80>>7
  254. Zero := PATHeader[1]&0x40>>6
  255. Reserved := PATHeader[1]&0x30>>4
  256. SectionLength := binary.BigEndian.Uint16([]byte{PATHeader[1]&0x0F, PATHeader[2]})
  257. TSID := binary.BigEndian.Uint16([]byte{PATHeader[3], PATHeader[4]})
  258. Reserved2 := PATHeader[5]&0xC0>>6
  259. VersionNumber := PATHeader[5]&0x3E>>1
  260. CurrentNextIndicator := PATHeader[5]&0x01
  261. SectionNumber := PATHeader[6]
  262. LastSectionNumber := PATHeader[7]
  263.  
  264. if FilterFlag == true {
  265. fmt.Fprintf(response, "\t\t\tPAT: [Table ID: x%X\t\t Section Syntax Indicator: b%b\t\tZero: x%X\t\tReserved: b%b\n\t\t\t\tSection Length: d%d Bits\t\tTSID: x%X\t\tReserved2: b%b\t\tVersion Number: x%X\n\t\t\t\tCurrent Next Indicator: x%X\t\tSection Number: x%X\t\tLast Section Number: x%X]\n", TableID, SectionSyntaxIndicator, Zero, Reserved, SectionLength, TSID, Reserved2, VersionNumber, CurrentNextIndicator, SectionNumber, LastSectionNumber)
  266. }
  267.  
  268. for count:=5;count<(int(SectionLength)-5);count += 4 {
  269. table := packet.Next(4)
  270. ProgramNumber := binary.BigEndian.Uint16([]byte{table[0], table[1]})
  271. ReservedMapped := table[2]&0xE0>>5
  272. MappedPID := binary.BigEndian.Uint16([]byte{table[2]&0x1F, table[3]})
  273. if getProgramMapping(MappedPID) == 0x00 { //if PMT..
  274. setProgramMapping(MappedPID, ProgramNumber)
  275. }
  276. if FilterFlag == true {
  277. fmt.Fprintf(response, "\t\t\t\tDescriptor: [Program Number: x%X\t\tReserved: b%b\t\tPMT PID: x%X]\n", ProgramNumber, ReservedMapped, MappedPID)
  278. }
  279. }
  280.  
  281. CRC32 := packet.Next(int(SectionLength)-count)
  282.  
  283. if FilterFlag == true {
  284. fmt.Fprintf(response, "\t\t\tCRC32: x%X\n", CRC32)
  285. }//*/
  286.  
  287. }
  288. //Analyze PMT====================================================================================================================================================================//
  289. func MpegPMTAnalyze(packet bytes.Buffer, response *bufio.Writer ) {
  290.  
  291. PointerField := packet.Next(1)
  292.  
  293. for i:=0;i<int(PointerField[0]);i++ {
  294. packet.Next(1)
  295. }
  296.  
  297. PATHeader := packet.Next(12)
  298.  
  299. TableID := PATHeader[0]
  300. SectionSyntaxIndicator := PATHeader[1]&0x80>>7
  301. Zero := PATHeader[1]&0x40>>6
  302. Reserved := PATHeader[1]&0x30>>4
  303. SectionLength := binary.BigEndian.Uint16([]byte{PATHeader[1]&0x0F, PATHeader[2]})
  304. count := 0
  305. ProgramNumber := binary.BigEndian.Uint16([]byte{PATHeader[3], PATHeader[4]})
  306. Reserved2 := PATHeader[5]&0xC0>>6
  307. VersionNumber := PATHeader[5]&0x3E>>1
  308. CurrentNextIndicator := PATHeader[5]&0x01
  309. SectionNumber := PATHeader[6]
  310. LastSectionNumber := PATHeader[7]
  311. Reserved3 := PATHeader[8]&0xE0>>5
  312. PCRPID := binary.BigEndian.Uint16([]byte{PATHeader[8]&0x1F, PATHeader[9]})
  313. Reserved4 := PATHeader[10]&0xF0>>4
  314. ProgramInfoLength := binary.BigEndian.Uint16([]byte{PATHeader[10]&0x0F, PATHeader[11]})
  315. ProgramInfo := packet.Next(int(ProgramInfoLength))
  316. count +=9+int(ProgramInfoLength)
  317.  
  318. if FilterFlag == true {
  319. fmt.Fprintf(response, "\t\t\tPMT: [Table ID: x%X\t\t Section Syntax Indicator: b%b\t\tZero: x%X\t\tReserved: b%b\n\t\t\t\tSection Length: d%d Bits\t\tProgram Number: x%X\t\tReserved2: b%b\t\tVersion Number: x%X\n\t\t\t\tCurrent Next Indicator: x%X\t\tSection Number: x%X\t\tLast Section Number: x%X\t\tReserved: b%b\n\t\t\t\tPCR PID: x%X\t\tReserved: b%b\t\tProgram Info Length: d%d Bytes\t\tProgram Info: x%X]\n", TableID, SectionSyntaxIndicator, Zero, Reserved, SectionLength, ProgramNumber, Reserved2, VersionNumber, CurrentNextIndicator, SectionNumber, LastSectionNumber, Reserved3, PCRPID, Reserved4, ProgramInfoLength, ProgramInfo)
  320. }
  321.  
  322. for ;count<(int(SectionLength)-4); {
  323. table := packet.Next(5)
  324. StreamType := table[0]
  325. StreamReserved1 := table[1]&0xE0>>5
  326. ESPID := binary.BigEndian.Uint16([]byte{table[1]&0x1F, table[2]})
  327.  
  328. StreamReserved2 := table[3]&0xF0>>4
  329. ESInfoLen := binary.BigEndian.Uint16([]byte{table[3]&0x0F, table[4]})
  330.  
  331. if getStreamMapping(ESPID) == 0x00 { //if PMT..
  332. setStreamMapping(ESPID, uint16(StreamType))
  333. }
  334.  
  335. if FilterFlag == true {
  336. fmt.Fprintf(response, "\t\t\t\tStream: [Stream Type: x%02X\t\tStream Reserved: b%03b\t\tElementary Stream PID: x%04X\t\tStream Reserved: b%04b\n\t\t\t\t\tElementary Stream Info Length: d%d]\n", StreamType, StreamReserved1, ESPID, StreamReserved2, ESInfoLen)
  337. }
  338.  
  339. for i:=0;i<int(ESInfoLen); {
  340. Tag := packet.Next(1)[0]
  341. Len := packet.Next(1)[0]
  342. Data := packet.Next(int(Len))
  343.  
  344. i+= 2+int(Len)
  345.  
  346. if FilterFlag == true {
  347. fmt.Fprintf(response, "\t\t\t\t\tElementary Stream Descriptor: [Descriptor Tag: x%X\t\tDescriptor Length: d%d\t\tDescriptorData: x%X]\n", Tag, Len, Data)
  348. }
  349. }
  350.  
  351. count = count + 5+int(ESInfoLen)
  352.  
  353. }
  354.  
  355.  
  356. CRC32 := packet.Next(int(SectionLength)-count)
  357.  
  358. if FilterFlag == true {
  359. fmt.Fprintf(response, "\t\t\tCRC32: x%X\n", CRC32)
  360. }
  361.  
  362. }
  363. //====================================================================================================================================================================//
  364. func MpegSCTE35Analyze(packet bytes.Buffer, response *bufio.Writer ) {
  365.  
  366. PointerField := packet.Next(1)
  367.  
  368. for i:=0;i<int(PointerField[0]);i++ {
  369. packet.Next(1)
  370. }
  371.  
  372. SCTE35Header := packet.Next(14)
  373.  
  374. TableID := SCTE35Header[0]
  375. SectionSyntaxIndicator := SCTE35Header[1]&0x80>>7
  376. Zero := SCTE35Header[1]&0x40>>6
  377. Reserved := SCTE35Header[1]&0x30>>4
  378. SectionLength := binary.BigEndian.Uint16([]byte{SCTE35Header[1]&0x0F, SCTE35Header[2]})
  379. count := 0
  380. ProtoVer := SCTE35Header[3]
  381. EncryptedFlag := SCTE35Header[4]&0x80>>7
  382. EncryptedAlg := SCTE35Header[4]&0x7E>>1
  383. PTSAdj := []byte{SCTE35Header[4]&0x01, SCTE35Header[5], SCTE35Header[6], SCTE35Header[7], SCTE35Header[8]}
  384. CWIndex := SCTE35Header[9]
  385. Tier := binary.BigEndian.Uint16([]byte{SCTE35Header[10], SCTE35Header[11]&0xF0})>>4
  386. SpliceCommandLen := binary.BigEndian.Uint16([]byte{SCTE35Header[11]&0x0F, SCTE35Header[12]})
  387. SpliceCommandType := SCTE35Header[13]
  388. count +=11
  389.  
  390. if FilterFlag == true {
  391. fmt.Fprintf(response, "\t\t\tSCTE35 Header: [Pointer Length: x%X\t\tTable ID: x%X\t\tSection Syntax Indicator: b%b\t\tZero: x%X\t\tReserved: b%b\n\t\t\t\tSection Length: d%d Bits\t\tProtocol Version: x%X\t\tEncrypted Packet Flag: b%b\t\tEncryption Algorithm: x%X\n\t\t\t\tPTS Adjustment: x%X\t\tCW Index: x%X\t\tTier: x%X\t\tSplice Command Length: d%d\n\t\t\t\tSplice Command Type: x%X]\n", PointerField, TableID, SectionSyntaxIndicator, Zero, Reserved, SectionLength, ProtoVer, EncryptedFlag, EncryptedAlg, PTSAdj, CWIndex, Tier, SpliceCommandLen, SpliceCommandType)
  392. }
  393.  
  394. if SpliceCommandType == 0x0 {
  395. }
  396. if SpliceCommandType == 0x04 {
  397. }
  398. if SpliceCommandType == 0x05 {
  399. SpliceInsertAnalyze(packet, response)
  400. }
  401. if SpliceCommandType == 0x06 {
  402. SpliceTimeSignalAnalyze(packet, response)
  403. }
  404. if SpliceCommandType == 0x07 {
  405. }
  406. if SpliceCommandType == 0xFF {
  407. }
  408.  
  409. _ = packet.Next(int(SpliceCommandLen))
  410.  
  411. DescriptorLoopLength := binary.BigEndian.Uint16(packet.Next(2))
  412. if FilterFlag == true {
  413. fmt.Fprintf(response, "\t\t\t\tSplice Descriptor Loop: [Descriptor Loop Length: d%d]\n",DescriptorLoopLength)
  414. }
  415. for i:=0;i<int(DescriptorLoopLength); {
  416. spliceDescriptorTag := packet.Next(1)[0]
  417. descriptorLength := packet.Next(1)[0]
  418. identifier := packet.Next(4)
  419. i=i+6
  420. if FilterFlag == true {
  421. fmt.Fprintf(response, "\t\t\t\t\tSplice Descriptor: [Descriptor Tag: x%X\t\tDescriptor Length: x%X\t\tIdentifier: x%X]\n", spliceDescriptorTag, descriptorLength, identifier)
  422. }
  423.  
  424. if spliceDescriptorTag == 0x00 {
  425. AvailDescriptor(packet, response)
  426. } else if spliceDescriptorTag == 0x01 {
  427. DTMFDescriptor(packet, response)
  428. } else if spliceDescriptorTag == 0x02 {
  429. SegmentationDescriptor(packet, response)
  430. } else if spliceDescriptorTag == 0x03 {
  431. TimeDescriptor(packet, response)
  432. } else {
  433. fmt.Print()
  434. }
  435. }
  436.  
  437.  
  438.  
  439. }
  440. //====================================================================================================================================================================//
  441. func AvailDescriptor(packet bytes.Buffer, response *bufio.Writer ) {
  442. ProviderAvailID := packet.Next(4)
  443. if FilterFlag == true {
  444. fmt.Fprintf(response, "\t\t\t\t\t\tAvail Descriptor: [Identifier: x%X]\n", ProviderAvailID)
  445. }
  446.  
  447. }
  448. //====================================================================================================================================================================//
  449. func DTMFDescriptor(packet bytes.Buffer, response *bufio.Writer ) {
  450. header := packet.Next(2)
  451. preroll := header[0]
  452. dtmfCount := header[1]&0xE0
  453. reserved := header[1]&0x1F
  454. if FilterFlag == true {
  455. fmt.Fprintf(response, "\t\t\t\t\t\tDTMF Descriptor: [Preroll: x%X\t\tDTMF Count: x%X\t\tReserved: x%X]\n", preroll, dtmfCount, reserved)
  456. for i:=0;i<int(dtmfCount);i++ {
  457. //fmt.Fprintf(response, "\t\t\t\t\t\tDTMF Char: x%X]\n", packet.Next(1)[0])
  458. }
  459. }
  460.  
  461. }
  462. //====================================================================================================================================================================//
  463. func SegmentationDescriptor(packet bytes.Buffer, response *bufio.Writer ) int {
  464. SegEventID := packet.Next(4)
  465. Second := packet.Next(1)[0]
  466. SegEventCancelFlag := Second&0x80>>7
  467. Reserved := Second&0xEF
  468.  
  469. count := 5
  470.  
  471. if FilterFlag == true {
  472. fmt.Fprintf(response, "\t\t\t\t\t\tSegmentation Descriptor: [Segmentation Event ID: x%X\t\tSegmentation Event Cancel Flag: b%b\t\tReserved: b%b]\n",SegEventID, SegEventCancelFlag, Reserved)
  473. }
  474. if SegEventCancelFlag == 0 {
  475. Third := packet.Next(1)[0]
  476. ProgSegFlag := Third&0x80>>7
  477. SegDurFlag := Third&0x40>>6
  478. DNRFlag := Third&0x20>>5
  479.  
  480. if FilterFlag == true {
  481. fmt.Fprintf(response, "\t\t\t\t\t\t\tSegmentation Flags: [Program Segmentation Flag: b%b\t\tSegment Duration Flag: b%b\t\tDelivery Not Restricted Flag: b%b]\n", ProgSegFlag, SegDurFlag, DNRFlag)
  482. }
  483.  
  484. if DNRFlag == 0 {
  485. WDAFlag := Third&0x10>>4
  486. NRBFlag := Third&0x08>>3
  487. ArchiveFlag := Third&0x04>>3
  488. DevRestrictions := Third&0x03
  489. if FilterFlag == true {
  490. fmt.Fprintf(response, "\t\t\t\t\t\t\tDelivery Not Restricted SubFlags: [Web Delivery Allowed Flag: b%b\t\tNo Regional Blackout Flag: b%b\t\tArchive Allowed Flag: b%b\t\tDevice Restrictions: b%b]\n", WDAFlag, NRBFlag, ArchiveFlag, DevRestrictions)
  491. }
  492. } else {
  493. Reserved2 := Third&0x1F
  494. fmt.Fprintf(response, "\t\t\t\t\t\t\tReserved: b%b\n", Reserved2)
  495. }
  496.  
  497. count++
  498.  
  499. if ProgSegFlag == 0 {
  500. ComponentCount := packet.Next(1)[0]
  501. count++
  502. for i:=0;i<int(ComponentCount);i++ {
  503. ComponentTag := packet.Next(1)[0]
  504. Fourth := packet.Next(1)[0]
  505. Reserved3 := Fourth&0xF7
  506. PTSMain := packet.Next(4)
  507. PTSOffset := []byte{Fourth&0x01, PTSMain[0], PTSMain[1], PTSMain[2], PTSMain[3]}
  508. count = count +6
  509. if FilterFlag == true {
  510. fmt.Fprintf(response, "\t\t\t\t\t\t\tProgram Segmentation Data: [Component Tag: b%X\t\tReserved: b%b\t\tPTS Offset: x%X]\n", ComponentTag, Reserved3, PTSOffset)
  511. }
  512. }
  513. }
  514. if SegDurFlag == 1 {
  515. SegDur := packet.Next(5)
  516. count+=5
  517. if FilterFlag == true {
  518. fmt.Fprintf(response, "\t\t\t\t\t\t\t\tSegmentation duration: x%X\n", SegDur)
  519. }
  520. }
  521. SegUPIDType := packet.Next(1)[0]
  522. SegUPIDLen := packet.Next(1)[0]
  523. SegUPID := packet.Next(int(SegUPIDLen))
  524. SegTypeID := packet.Next(1)[0]
  525. SegNum := packet.Next(1)[0]
  526. SegExp := packet.Next(1)[0]
  527. count +=5
  528. if FilterFlag == true {
  529. fmt.Fprintf(response, "\t\t\t\t\t\t\tSegmentation Data: [Segmentation UPID Type: x%X\t\tSegmentation UPID Length: x%X\t\tSegmentation UPID: x%X\t\tSegmentation Type ID: x%X\t\tSegment number: x%X\t\tSegments Expected: x%X\t\t]\n", SegUPIDType, SegUPIDLen, SegUPID, SegTypeID, SegNum, SegExp)
  530. }
  531. if SegTypeID == 0x34 || SegTypeID == 0x36 {
  532. SubSegNum := packet.Next(1)[0]
  533. SubSegExp := packet.Next(1)[0]
  534. if FilterFlag == true {
  535. fmt.Fprintf(response, "\t\t\t\t\t\t\tSub Segment Data: [SubSegment Number: x%X\t\tSubSegments Expected: x%X]\n", SubSegNum, SubSegExp)
  536. }
  537. count +=2
  538. }
  539. }
  540. return count
  541.  
  542. }
  543. //====================================================================================================================================================================//
  544. func TimeDescriptor(packet bytes.Buffer, response *bufio.Writer ) {
  545.  
  546. }
  547. //====================================================================================================================================================================//
  548. func SpliceInsertAnalyze(packet bytes.Buffer, response *bufio.Writer ) {
  549. SCTE35Header := packet.Next(6)
  550. SpliceEventID := []byte{SCTE35Header[0], SCTE35Header[1], SCTE35Header[2], SCTE35Header[3]}
  551. SpliceEventCancel := SCTE35Header[4]&0x80>>7
  552. Reserved := SCTE35Header[4]&0xEF
  553. if FilterFlag == true {
  554. fmt.Fprintf(response, "\t\t\t\t\tSplice Insert: [Splice Event ID: x%08X\t\tSplice Event Cancel Flag: b%b\t\tReserved: b%07b]\n", SpliceEventID, SpliceEventCancel, Reserved)
  555. }
  556. if SpliceEventCancel == 0x00 {
  557. OONFlag := SCTE35Header[5]&0x80>>7
  558. SpliceFlag := SCTE35Header[5]&0x40>>6
  559. DurationFlag := SCTE35Header[5]&0x20>>5
  560. SpliceImmediateFlag := SCTE35Header[5]&0x10>>4
  561. Reserved2 := SCTE35Header[5]&0x0F
  562. if FilterFlag == true {
  563. fmt.Fprintf(response, "\t\t\t\t\tNew Splice Event: [Out Of Network Indicator: b%b\t\tSpliceFlag: b%b\t\tDurationFlag: b%b\t\tSplice Immediate Flag: b%b\n\t\t\t\t\t\tReserved: b%04b]\n", OONFlag, SpliceFlag, DurationFlag, SpliceImmediateFlag, Reserved2)
  564. }
  565. if SpliceFlag == 0x01 && SpliceImmediateFlag == 0x00 {
  566. SpliceTimeSignalAnalyze(packet, response)
  567. }
  568. if SpliceFlag == 0x00 {
  569. ComponentCount := int(packet.Next(1)[0])
  570. for ind := 0;ind<ComponentCount;ind++ {
  571. fmt.Printf("x%X", packet.Next(1)[0])
  572. }
  573. }
  574. }
  575.  
  576. }
  577. func SpliceTimeSignalAnalyze(packet bytes.Buffer, response *bufio.Writer ) {
  578. Header := packet.Next(1)[0]
  579. TimeSpecifiedFlag := Header&0x80>>7
  580. if TimeSpecifiedFlag == 1 {
  581. TimestampData := packet.Next(4)
  582. Reserved := Header&0x7E>>1
  583. Timestamp := []byte{Header&0x01, TimestampData[0], TimestampData[1], TimestampData[2], TimestampData[3] }
  584. if FilterFlag == true {
  585. fmt.Fprintf(response, "\t\t\t\t\tSplice Time Signal Command : [Time Specified Flag: x%X\t\tReserved: b%b\t\tTimestamp: x%X\n]", TimeSpecifiedFlag, Reserved, Timestamp)
  586. }
  587. } else {
  588. Reserved := Header&0x7F
  589. if FilterFlag == true {
  590. fmt.Fprintf(response, "\t\t\t\t\tSplice Time Signal Command : [Time Specified Flag: x%X\t\tReserved: b%b\n]", TimeSpecifiedFlag, Reserved)
  591. }
  592. }
  593.  
  594. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement