config.ts 3.4 KB
import { PublicConfigClass } from '@/packages/public'
import { CreateComponentType } from '@/packages/index.d'
import { SingleCameraConfig } from './index'
import cloneDeep from 'lodash/cloneDeep'
import { StreamType } from "@/components/Video/src/types";
import { useFingerprint } from "@/utils/external/useFingerprint";
import { getOpenFlvPlayUrl, getVideoControlStart } from "@/api/external/flvPlay";
import { getVideoUrl } from "@/api/external/common";
import { CameraRecord } from "@/api/external/common/model";
import { useGlobSetting } from '@/hooks/external/setting';

export enum sourceTypeEnum {
  CUSTOM = 'custom',
  PLATFORM = 'platform'
}

export enum sourceTypeNameEnum {
  CUSTOM = '自定义',
  PLATFORM = '平台获取'
}

export enum FluoriteMideaProtocolEnum {
  HLS = 2,
  FLV = 4,
}

export enum VideoPlayerTypeEnum {
  m3u8 = 'application/x-mpegURL',
  mp4 = 'video/mp4',
  webm = 'video/webm',
  flv = 'video/x-flv'
}

export interface videoListInterface {
  name: string
  accessMode: number
  id: string
  videoUrl: string
  label: string
  value: string
  sn: string
  channelId: string
  deviceId: string
  customUrl: string
  params: GBT28181Params
  playProtocol?: number
}

export interface GBT28181Params {
  channelNo: string
  deviceId: string
  deviceName: string
}

export enum AccessMode {
  ManuallyEnter = 0, //手动输入
  Streaming = 1, //流媒体
  GBT28181 = 2 //GBT28181
}

export interface Dataset {
  accessMode: AccessMode
  channelId?: string
  customUrl?: string
  deviceId?: string
  id?: string
  value?: string
  playProtocol?: number
}

export const option = {
  dataset: {} as Dataset,
  customVideoUrl: '', //用于手动输入视频流地址
  autoplay: true,
  poster: '',
  sourceType: sourceTypeEnum.CUSTOM,
  organization: '',
  videoId: null
}

export default class Config extends PublicConfigClass implements CreateComponentType {
  public key = SingleCameraConfig.key
  public chartConfig = cloneDeep(SingleCameraConfig)
  public option = cloneDeep(option)
}

export const isRtspProtocol = (url: string) => {
  const reg = /^rtsp:\/\//g;
  return reg.test(url);
};

export async function getPlayUrl(
  params: CameraRecord
): Promise<{ url: string; type: StreamType } | undefined> {
  const { accessMode } = params;
  if (accessMode === AccessMode.ManuallyEnter) {
    const { videoUrl } = params;
    if (params.videoUrl) {
      const isRTSPPlay = isRtspProtocol(videoUrl);

      if (isRTSPPlay) {
        const { getResult } = useFingerprint();
        const fingerprint = await getResult();
        return { url: getOpenFlvPlayUrl(videoUrl, fingerprint.visitorId), type: 'flv' };
      } else {
        return { url: videoUrl, type: 'auto' };
      }
    }
  } else if (accessMode === AccessMode.GBT28181) {
    const { streamMediaContentSecurityProtocol } = useGlobSetting()
    console.log(useGlobSetting())
    const { deviceId, channelNo } = params?.params || {};
    const result = await getVideoControlStart({ channelId: channelNo!, deviceId: deviceId! });
    return { url: streamMediaContentSecurityProtocol ? result.data.https_flv : result.data.flv, type: 'flv' };
  } else {
    const { id, playProtocol } = params;
    const result = await getVideoUrl(id);
    const type: StreamType =
      playProtocol === FluoriteMideaProtocolEnum.FLV
        ? 'flv'
        : playProtocol === FluoriteMideaProtocolEnum.HLS
          ? 'hls'
          : 'auto';
    return { url: result.data.url, type };
  }
}