diff --git a/README.md b/README.md index c33b077..c44a554 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ Currently implemented Features: * resume the call (`call-resume`) * play dtmf tones (`dtmf`) * get call status (`call-status`) +* get call stats (`call-stats`) Features supported by the unix socket (linphone deamon): diff --git a/pylinphone.py b/pylinphone.py index 45d883d..d74a57a 100644 --- a/pylinphone.py +++ b/pylinphone.py @@ -109,6 +109,42 @@ class LinphoneCommunicationSocket(): else: raise RuntimeError(answer["error"]) + def call_stats(self, call_id): + self.socket.send("call-stats {call_id}".format(call_id=call_id).encode("ascii")) + answer = self._await_answer() + + if answer["status"]: + stats_offsets = [i for i, x in enumerate(answer["data"]) if "Id:" in x] + stats = { + "audio": {}, + "video": {} + } + stat_type = answer["data"][stats_offsets[0]+1].split(":", 1)[1].strip().lower() + for stat in answer["data"][:stats_offsets[1]]: + k, v = stat.split(":", 1) + key = k.strip().lower().replace(" ", "_") + value = v.strip().lower().replace(" ", "_") + try: + value = int(value) + except Exception: + pass + stats[stat_type].update({key: value}) + + stat_type = answer["data"][stats_offsets[1]+1].split(":", 1)[1].strip().lower() + for stat in answer["data"][stats_offsets[1]:]: + k, v = stat.split(":", 1) + key = k.strip().lower().replace(" ", "_") + value = v.strip().lower().replace(" ", "_") + try: + value = int(value) + except Exception: + pass + stats[stat_type].update({key: value}) + + return status + else: + raise RuntimeError(answer["error"]) + def call_resume(self, call_id): self.socket.send("call-resume {call_id}".format(call_id=call_id).encode("ascii")) answer = self._await_answer()