Index: tools/py_event_log_analyzer/pb_parse.py |
diff --git a/tools/py_event_log_analyzer/pb_parse.py b/tools/py_event_log_analyzer/pb_parse.py |
new file mode 100644 |
index 0000000000000000000000000000000000000000..d14e7bc9a983f092af1b991cce1dbec15f92a390 |
--- /dev/null |
+++ b/tools/py_event_log_analyzer/pb_parse.py |
@@ -0,0 +1,83 @@ |
+# Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
+# |
+# Use of this source code is governed by a BSD-style license |
+# that can be found in the LICENSE file in the root of the source |
+# tree. An additional intellectual property rights grant can be found |
+# in the file PATENTS. All contributing project authors may |
+# be found in the AUTHORS file in the root of the source tree. |
+ |
+"""Parses protobuf RTC dumps. |
+""" |
+ |
+from __future__ import division |
+ |
+import os |
+import struct |
+import sys |
+import enum |
+ |
+ |
+# makes sure to import correct protobuf |
+webrtc_root = "../../" |
+sys.path.append(os.path.join(webrtc_root, "out/Debug/pyproto/webrtc/call/")) |
+sys.path.append(os.path.join(webrtc_root, "out/Release/pyproto/webrtc/call/")) |
+sys.path.append(os.path.join(webrtc_root, "third_party/protobuf/python/")) |
kwiberg-webrtc
2016/05/24 12:09:51
sys.path += [os.path.join(webrtc_root, path) for p
aleloi
2016/05/24 15:54:41
The point is to make it import the correct generat
kwiberg-webrtc
2016/05/25 12:42:41
Sorry, I didn't mean to ask why you did this; I ju
aleloi2
2016/05/30 14:57:55
Thank you! This is better. Ignore the latest comme
|
+import rtc_event_log_pb2 as rtc_pb |
+ |
+ |
+DIR = enum.Enum("Direction", "incoming outgoing") |
ivoc
2016/05/24 11:50:18
I would prefer a less fancy solution that does not
kwiberg-webrtc
2016/05/24 12:09:51
Why is this in all caps?
aleloi
2016/05/24 15:54:40
Got convinced by Ivo and removed. Directions are n
|
+ |
+ |
+class DataPoint(object): |
+ """Simple container class for RTP events. |
+ """ |
+ |
+ def __init__(self, rtp_header_str, packet_size, |
+ arrival_timestamp_us, direction): |
+ """Builds a data point by parsing an RTP header. |
+ |
+ As defined in RFC 3550 section 5.1. |
+ """ |
+ self.size = packet_size |
+ self.arrival_timestamp_ms = arrival_timestamp_us / 1000 |
+ unpacked = struct.unpack_from("!HHII", rtp_header_str, 0) |
+ first2header_bytes, self.seq_no, self.timestamp, self.ssrc = unpacked |
ivoc
2016/05/24 11:50:18
The temporary 'unpacked' variable can be removed h
kwiberg-webrtc
2016/05/24 12:09:51
I find it's good for readability to surround tuple
aleloi
2016/05/24 15:54:40
Acknowledged.
|
+ |
+ def bit_unpack(bit_offsets, number): |
+ """unpacks a big-endian number into blocks of bits.""" |
kwiberg-webrtc
2016/05/24 12:09:51
This doesn't sound right. Python integers are alwa
aleloi
2016/05/24 15:54:40
Yes, thank you. Anyway, I removed it completely by
|
+ res = [] |
+ for offset in reversed(bit_offsets): |
+ mask = ((1<<offset) - 1) |
+ res.append(number & mask) |
+ number >>= offset |
+ return reversed(res) |
+ |
+ # fields V, P, X, CC, and M of the header are ignored |
+ _, _, _, _, _, self.pt = bit_unpack([2, 1, 1, 4, 1, 7], first2header_bytes) |
ivoc
2016/05/24 11:50:18
I think something like this would be simpler (I di
kwiberg-webrtc
2016/05/24 12:09:51
It's probably much easier to just shift and mask m
aleloi
2016/05/24 15:54:40
Thank you! Changed to "& 0b01111111". I think it's
|
+ self.direction = direction |
ivoc
2016/05/24 11:50:18
Move to line 42 please.
aleloi
2016/05/24 15:54:40
completely removed because not used.
|
+ |
+ |
+def parse_protobuf(file_path): |
+ """Parses rtc event log from protobuf file. |
+ |
+ Args: |
+ file_path: path to protobuf file of RTC event stream |
+ |
+ Returns: |
+ all RtpPacket events from the event stream as a list of DataPoint:s |
ivoc
2016/05/24 11:50:18
Remove the : please.
aleloi
2016/05/24 15:54:40
Done.
|
+ """ |
+ event_stream = rtc_pb.EventStream() |
+ with open(file_path, "rb") as f: |
+ event_stream.ParseFromString(f.read()) |
+ result = [] |
+ |
+ for event in event_stream.stream: |
+ if event.HasField("rtp_packet"): |
+ rtp_packet = event.rtp_packet |
+ header = rtp_packet.header |
+ direction = DIR.incoming if rtp_packet.incoming else DIR.outgoing |
+ result.append(DataPoint(header, rtp_packet.packet_length, |
+ event.timestamp_us, |
+ direction)) |
kwiberg-webrtc
2016/05/24 12:09:51
Consider using a comprehension:
return [DataPoi
aleloi
2016/05/24 15:54:40
Thank you, that's nicer!
|
+ |
+ return result |