-
Notifications
You must be signed in to change notification settings - Fork 88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Moves cli helpers to pkgs #31
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
304b442
move cli PAT and PMT extract functions into psi package
davemt e2811fd
cleanup PMT/PAT reader functions
davemt d6f3f79
move cli sync function to packet package
davemt 78eb3e9
cleanup packet FindNextSync
davemt eb806eb
add tests for functions moved out of cli
davemt 464a66b
move FindNextSync to a new file, add more tests
davemt 6f36c12
change sync method to place reader at pkt start
davemt e34e117
read pat: break from loop so required return not useless
davemt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,62 @@ | ||
/* | ||
MIT License | ||
|
||
Copyright 2016 Comcast Cable Communications Management, LLC | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ | ||
|
||
package packet | ||
|
||
import ( | ||
"io" | ||
|
||
"github.com/Comcast/gots" | ||
) | ||
|
||
// Sync finds the offset of the next packet sync byte and returns the offset of | ||
// the sync w.r.t. the original reader position. It also checks the next 188th | ||
// byte to ensure a sync is found. | ||
func FindNextSync(r io.Reader) (int64, error) { | ||
data := make([]byte, 1) | ||
for i := int64(0); ; i++ { | ||
_, err := io.ReadFull(r, data) | ||
if err == io.EOF || err == io.ErrUnexpectedEOF { | ||
break | ||
} | ||
if err != nil { | ||
return 0, err | ||
} | ||
if int(data[0]) == SyncByte { | ||
// check next 188th byte | ||
nextData := make([]byte, PacketSize) | ||
_, err := io.ReadFull(r, nextData) | ||
if err == io.EOF || err == io.ErrUnexpectedEOF { | ||
break | ||
} | ||
if err != nil { | ||
return 0, err | ||
} | ||
if nextData[187] == SyncByte { | ||
return i, nil | ||
} | ||
} | ||
} | ||
return 0, gots.ErrSyncByteNotFound | ||
} |
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,80 @@ | ||
/* | ||
MIT License | ||
|
||
Copyright 2016 Comcast Cable Communications Management, LLC | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ | ||
|
||
package packet | ||
|
||
import ( | ||
"bytes" | ||
"encoding/hex" | ||
"testing" | ||
) | ||
|
||
func TestFindNextSyncForSmoke(t *testing.T) { | ||
bs, _ := hex.DecodeString("474000100000b00d0001c100000001e256f803e71bfffffff" + | ||
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" + | ||
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" + | ||
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" + | ||
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" + | ||
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" + | ||
"ff4742") | ||
r := bytes.NewReader(bs) | ||
|
||
offset, err := FindNextSync(r) | ||
if err != nil { | ||
t.Errorf("Unexpected error: %v", err) | ||
} | ||
if offset != 0 { | ||
t.Errorf("Incorrect offset returned for next sync marker") | ||
} | ||
} | ||
|
||
func TestFindNextSyncNonZeroOffset(t *testing.T) { | ||
bs, _ := hex.DecodeString("ffffff474000100000b00d0001c100000001e256f803e71bfffffff" + | ||
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" + | ||
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" + | ||
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" + | ||
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" + | ||
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" + | ||
"ff4742") | ||
r := bytes.NewReader(bs) | ||
|
||
offset, err := FindNextSync(r) | ||
if err != nil { | ||
t.Errorf("Unexpected error: %v", err) | ||
} | ||
if offset != 3 { | ||
t.Errorf("Incorrect offset returned for next sync marker") | ||
} | ||
} | ||
|
||
func TestFindNextSyncNotFound(t *testing.T) { | ||
// no sync byte here | ||
bs, _ := hex.DecodeString("ff4000100000b00d0001c100000001e256f803e71bfffffff") | ||
r := bytes.NewReader(bs) | ||
|
||
_, err := FindNextSync(r) | ||
if err == nil { | ||
t.Errorf("Expected there to be an error, but there was not") | ||
} | ||
} |
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 |
---|---|---|
|
@@ -25,6 +25,8 @@ SOFTWARE. | |
package psi | ||
|
||
import ( | ||
"io" | ||
|
||
"github.com/Comcast/gots" | ||
"github.com/Comcast/gots/packet" | ||
) | ||
|
@@ -124,3 +126,38 @@ func (pat pat) ProgramMap() map[uint16]uint16 { | |
|
||
return m | ||
} | ||
|
||
// ReadPAT extracts a PAT from a reader of a TS stream. It will read until a | ||
// PAT packet is found or EOF is reached. | ||
// It returns a new PAT object parsed from the packet, if found, and otherwise | ||
// returns an error. | ||
func ReadPAT(r io.Reader) (PAT, error) { | ||
pkt := make(packet.Packet, packet.PacketSize) | ||
var pat PAT | ||
for pat == nil { | ||
if _, err := io.ReadFull(r, pkt); err != nil { | ||
if err == io.EOF || err == io.ErrUnexpectedEOF { | ||
return nil, gots.ErrPATNotFound | ||
} | ||
return nil, err | ||
} | ||
isPat, err := packet.IsPat(pkt) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if isPat { | ||
pay, err := packet.Payload(pkt) | ||
if err != nil { | ||
return nil, err | ||
} | ||
cp := make([]byte, len(pay)) | ||
copy(cp, pay) | ||
pat, err := NewPAT(cp) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return pat, nil | ||
} | ||
} | ||
return nil, gots.ErrPATNotFound | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this line not needed |
||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comment should start with
FindNextSync
.Also, I think that there is a bug here. I know that you didn't introduce it, but it should be fixed. If we are checking the next packet's sync byte after we find the first sync byte, the reader is left skipping the first valid packet. I would argue that this func should only consume those bytes before the first sync byte. A reader is tricky because once we find a sync byte, we have consumed it. I am not sure the best way to solve this, but I would consider using a
[]byte]
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After checking #18 We could likely solve this with
bufio.Reader
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I noticed this and actually that is one reason I changed the name (I missed the comment). In the
parsefile
example it does the seek to the position returned so the change of the reader position caused here doesn't affect it, but it would be better to have this actually bring the reader to the right point instead.Out of curiosity, do you know the reason it also checks the 2nd sync after the first found?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure why the next sync byte is checked. It could be just to make sure it's actually a packet start and not an arbitrary
0x47
. Any additional sync byte verification should be done explicitly by the user of the library though.