# coding:utf-8
import threading
import time

import cv2
from lib.service.StreamH264 import StreamH264

'''
使用H264视频文件 和aac音频文件作为推流的压力测试
'''
class H264PresureTest():

    def __init__(self):
        self.mobileStart = 100000000000                           # 开始手机号
        self.channel = 1                                          # 频道号
        self.terNum = 2                                           # 要压力测试的线程数
        self.videoPath = "../../h264/bbb3.h264"                   # 要推流的视频路劲
        self.audioPath = "../../h264/bbb3.aac"                    # 要推流的音频路劲
        self.host = "10.100.12.3"
        self.port = 1078
        self.sendDur = 0.001
        self.isShowFrame = 0                                      # 拉流是否展示画面  0：不展示  1：展示

        self.threadInfo = {}                                      # 存放线程集
        self.threadInfo["threadObj"] = {}
        self.threadInfo["sucessT"] = {}
        self.threadInfo["failT"] = {}

        self.threadInfoPull = {}                                  # 存放线程集（拉流线程）
        self.threadInfoPull["threadObj"] = {}
        self.threadInfoPull["sucessT"] = {}
        self.threadInfoPull["failT"] = {}

        self.isOpenPullStream = 1                                 # 是否开启视频拉流  0：不  1：是
        self.pullUrl = "http://10.100.11.125:8085/live?port=1985&app=vandyo&stream="

    def setTerNum(self,data):
        self.terNum = data
    def setMobileStart(self,data="10000000000"):
        self.mobileStart = data
    def setChannel(self,data):
        self.channel = data
    def setVideoPath(self,data):
        self.videoPath = data
    def setAudioPath(self,data):
        self.audioPath = data
    def setHost(self,data):
        self.host = data
    def setPort(self,data):
        self.port = data
    def setIsOpenPullStream(self,data):
        self.isOpenPullStream = data
    def setSendDur(self,data):
        self.sendDur = data
    def setIsShowFrame(self,data):
        self.isShowFrame = data


    def testServicePush(self,cloudMirror):
        cloudMirror.sendAVStream()

    def testServicePull(self,mobile):
        pullUrl = self.pullUrl + mobile +"-" + str(self.channel)
        cap = cv2.VideoCapture(pullUrl)
        while (cap.isOpened()):
            ret, frame = cap.read()
            if ret == False:
                break
            if self.isShowFrame == 1:
                cv2.imshow("video", frame)
            if cv2.waitKey(1)&0xFF==ord('q'):
                break
        print("拉流结束")
        cap.release()
        cv2.destroyAllWindows()

    def runPush(self,mobile,threadId):
        cloudMirror = StreamH264()
        cloudMirror.setHost(self.host)
        cloudMirror.setPort(self.port)
        cloudMirror.setSendDur(self.sendDur)
        cloudMirror.connectServer()
        cloudMirror.setFPSVideo(30)
        cloudMirror.setFPSAudio(47)
        cloudMirror.setVideoPath(self.videoPath)
        cloudMirror.setAudioPath(self.audioPath)
        cloudMirror.setMobile(mobile)
        cloudMirror.setChannel(self.channel)

        conThread = threading.Thread(target=self.testServicePush,args=(cloudMirror,))
        self.threadInfo["threadObj"][threadId] = conThread
        conThread.start()
        print(threadId + "启动了：" + mobile + "-" + str(self.channel))

    def runPull(self,mobile,threadId):
        pullThread = threading.Thread(target=self.testServicePull, args=(mobile,))
        self.threadInfoPull["threadObj"][threadId] = pullThread
        pullThread.start()
        print(threadId + "启动了：" + mobile + "-" + str(self.channel))

    def start(self):
        for i in range(0,self.terNum):
            mobile = self.mobileStart + i
            mobile = "0" + str(mobile)
            threadId = "threadId__" + str(i)
            self.runPush(mobile,threadId)
            time.sleep(0.01)

        if self.isOpenPullStream == 1:
            for j in range(0,self.terNum):
                mobile = self.mobileStart + j
                mobile = "0" + str(mobile)
                threadPullId = "threadPullId__" + str(j)
                self.runPull(mobile,threadPullId)
                time.sleep(0.01)


if __name__ == "__main__":
    test = H264PresureTest()
    test.setHost("10.100.12.3")
    test.setPort(1078)
    test.setMobileStart(10000000000)
    test.setChannel(1)
    test.setTerNum(10)
    test.setSendDur(0.001)
    test.setIsOpenPullStream(0)                           # 设置是否开启拉流
    test.setIsShowFrame(0)
    test.setVideoPath("../../h264/bbb3.h264")
    test.setAudioPath("../../aac/bbb3.aac")
    test.start()