test_realtime_meeting.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import time
  2. import threading
  3. import sys
  4. import nls
  5. class TestRealtimeMeeting:
  6. def __init__(self, tid, test_file, url):
  7. self.__th = threading.Thread(target=self.__test_run)
  8. self.__id = tid
  9. self.__test_file = test_file
  10. self.__url = url
  11. def loadfile(self, filename):
  12. with open(filename, "rb") as f:
  13. self.__data = f.read()
  14. def start(self):
  15. self.loadfile(self.__test_file)
  16. self.__th.start()
  17. def test_on_sentence_begin(self, message, *args):
  18. print("test_on_sentence_begin:{}".format(message))
  19. def test_on_sentence_end(self, message, *args):
  20. print("test_on_sentence_end:{}".format(message))
  21. def test_on_start(self, message, *args):
  22. print("test_on_start:{}".format(message))
  23. def test_on_error(self, message, *args):
  24. print("on_error message=>{} args=>{}".format(message, args))
  25. def test_on_close(self, *args):
  26. print("on_close: args=>{}".format(args))
  27. def test_on_result_chg(self, message, *args):
  28. print("test_on_chg:{}".format(message))
  29. def test_on_result_translated(self, message, *args):
  30. print("test_on_translated:{}".format(message))
  31. def test_on_completed(self, message, *args):
  32. print("on_completed:args=>{} message=>{}".format(args, message))
  33. def __test_run(self):
  34. print("thread:{} start..".format(self.__id))
  35. rm = nls.NlsRealtimeMeeting(
  36. url=self.__url,
  37. on_sentence_begin=self.test_on_sentence_begin,
  38. on_sentence_end=self.test_on_sentence_end,
  39. on_start=self.test_on_start,
  40. on_result_changed=self.test_on_result_chg,
  41. on_result_translated=self.test_on_result_translated,
  42. on_completed=self.test_on_completed,
  43. on_error=self.test_on_error,
  44. on_close=self.test_on_close,
  45. callback_args=[self.__id]
  46. )
  47. print("{}: session start".format(self.__id))
  48. r = rm.start()
  49. self.__slices = zip(*(iter(self.__data),) * 640)
  50. for i in self.__slices:
  51. rm.send_audio(bytes(i))
  52. time.sleep(0.01)
  53. time.sleep(1)
  54. r = rm.stop()
  55. print("{}: rm stopped:{}".format(self.__id, r))
  56. time.sleep(5)
  57. def multiruntest(num=1):
  58. for i in range(0, num):
  59. name = "thread" + str(i)
  60. t = TestRealtimeMeeting(name, "tests/test1.pcm", "wss://tingwu-realtime-cn-hangzhou-pre.aliyuncs.com/api/ws/v1?")
  61. t.start()
  62. nls.enableTrace(True)
  63. multiruntest(1)