# -*- coding: UTF-8 -*-
import sys
import os
from os import path, access, R_OK
import subprocess
import thread

from ssat_sdk.device_manage.dektec_manager import *


'''
DTV播放器,汇总各种设备进行DTV信号输出
'''
class StreamCard_SCBC():

    def __init__(self):
        self.dektecMgr = DektecManager()
        self.cmdline = self.dektecMgr.cmdline

    def getCmdLine(self):
        return self.dektecMgr.cmdline

    def playStream(self, streamFile, times, type, channelPoint):
        self.dektecMgr.playStream(streamFile, times, type, channelPoint)

    def playStreamAtThread(self, streamFile,  times,  type,  channelPoint ):
        thread.start_new_thread(self.dektecMgr.playStreamAtThread,(streamFile,times,  type,  channelPoint))
        # self.dektecMgr.playStreamAtThread(streamFile,  times,  type,  channelPoint)

    def isPlaying(self):
        return self.dektecMgr.isPlaying()

    def interrupt(self):
        self.dektecMgr.interrupt()

    def taskkill(self):
        self.dektecMgr.taskkill()

    def set_cmd_clear(self):
        self.dektecMgr.set_cmd_clear()

    def set_playfile(self, playfile):
        # 码流文件
        self.dektecMgr.set_playfile(playfile)

    def set_Device_type(self, type):
        # 设备选择
        self.dektecMgr.set_Device_type(type)

    def set_Modulation_carrier_frequency(self, frequency):
        # 频点
        self.dektecMgr.set_Modulation_carrier_frequency(frequency)

    '''
    type:
        ATSC      ATSC modulation
        CMMB      CMMB modulation
        DAB       DAB modulation
        DTMB      DTMB modulation
        DVBS      DVB-S QPSK modulation
        DVBS2_16APSK DVB-S.2 16APSK modulation
        DVBS2_32APSK 32APSK modulation
        DVBS2_8PSK DVB-S.2 8PSK modulation
        DVBS2_QPSK DVB-S.2 QPSK modulation
        DVBS2_L3  DVB-S.2 L3 modulation
        DVBT      DVB-T/H modulation
        ISDBS     ISDB-S modulation
        ISDBT     ISDB-T modulation
        IQ        IQ direct
        QAM4      QAM-4 modulation
        QAM16     QAM-16 modulation
        QAM32     QAM-32 modulation
        QAM64     QAM-64 modulation
        QAM128    QAM-128 modulation
        QAM256    QAM-256 modulation
        T2MI      T2MI modulation
    '''
    def set_Modulation_type(self, Modulation_type):
        # 制式选择
        self.dektecMgr.set_Modulation_type(Modulation_type)

    def set_Modulation_bandwidth(self, Modulation_bandwidth):
        # 带宽
        self.dektecMgr.set_Modulation_bandwidth(Modulation_bandwidth)

    def set_Modulation_transmission_mode(self, transmission_mode):
        # 载波数
        self.dektecMgr.set_Modulation_transmission_mode(transmission_mode)

    def set_Modulation_constellation (self, constellation ):
        # 调制方式
        self.dektecMgr.set_Modulation_constellation(constellation)

    def set_Modulation_guard_interval(self, guard_interval):
        # 保护间隔
        self.dektecMgr.set_Modulation_guard_interval(guard_interval)

    def set_Modulation_Convolutional_rate(self, Convolutional_rate):
        # 信号纠错码
        self.dektecMgr.set_Modulation_Convolutional_rate(Convolutional_rate)

    def set_Modulation_Output_level(self, Output_level):
        # 信号强度
        self.dektecMgr.set_Modulation_Output_level(Output_level)

    def set_loop_times(self, loop_times):
        # 播放次数
        self.dektecMgr.set_loop_times(loop_times)

    def set_rate(self, rate):
        # 输出码率
        self.dektecMgr.set_rate(rate)

    def play_out(self):
        thread.start_new_thread(self.dektecMgr.play_out,())
        # self.dektecMgr.play_out()

    def play_out_wait(self):
        self.dektecMgr.play_out_wait()

    def DtPlay_exe_CMD(self, str):
        self.dektecMgr.DtPlay_exe_CMD(str)

    def DtPlay_exe_CMD_wait(self, str):
        self.dektecMgr.DtPlay_exe_CMD_wait(str)


    def help(self):
        self.dektecMgr.help()

    def __del__(self):
        self.dektecMgr.__del__()


# ------------------------------------------------------------------------------
if __name__ == "__main__":
    scscbc = StreamCard_SCBC()
    streamFile = r"D:\test\482_1218_105425.ts"
    times = 1
    type = "DTMB"
    channelPoint = "52.5MHz"
    # scscbc.playStream( streamFile,  times,  type,  channelPoint)
    # scscbc.help()
    # scscbc.DtPlay_exe_CMD( streamFile + " -mt DTMB -mf 52.5MHz")

    # 按照 SteamXpress 界面的设置
    scscbc.set_playfile(streamFile)
    scscbc.set_Device_type("215")
    scscbc.set_Modulation_carrier_frequency("578MHz")
    scscbc.set_Modulation_type("DVBT")
    scscbc.set_Modulation_bandwidth("8")
    scscbc.set_Modulation_transmission_mode("8k")
    # scscbc.set_Modulation_constellation("QAM64")
    scscbc.set_Modulation_constellation("QPSK")
    scscbc.set_Modulation_guard_interval("1/32")
    scscbc.set_Modulation_Convolutional_rate("7/8")
    scscbc.set_Modulation_Output_level("-90dBm")#单位可以不写,前面的负号"-"一定要写
    scscbc.set_loop_times("0")

    scscbc.set_rate("18661765")

    print scscbc.dektecMgr.cmdline
    # 频繁操作注意延迟3秒,画面才播放出来。
    scscbc.play_out()