116 lines
3.9 KiB
TypeScript
116 lines
3.9 KiB
TypeScript
import { EntityDict as BaseEntityDict } from 'oak-domain/lib/types/Entity';
|
|
import { assert } from 'oak-domain/lib/utils/assert';
|
|
import { EntityDict } from '../oak-app-domain';
|
|
import { Schema as Livestream } from '../oak-app-domain/Livestream/Schema';
|
|
import { AccountOrigin, QiniuLiveConfig } from '../types/Config';
|
|
import { getConfig } from './getContextConfig';
|
|
import { QiniuCloudInstance } from 'oak-external-sdk/es/service/qiniu/QiniuCloud';
|
|
import { BackendRuntimeContext } from '../context/BackendRuntimeContext';
|
|
import { cloneDeep } from 'oak-domain/lib/utils/lodash';
|
|
|
|
/**
|
|
* 创建直播流并生成推拉流地址
|
|
* @param streamTitle 直播流名称
|
|
* @param expireAt 推流过期时间
|
|
* @param context context
|
|
* @returns Livestream 对象
|
|
*/
|
|
export async function getLivestream<ED extends EntityDict & BaseEntityDict, Cxt extends BackendRuntimeContext<ED>>(
|
|
params: {
|
|
origin: AccountOrigin;
|
|
streamTitle: string,
|
|
expireAt: number,
|
|
},
|
|
context: Cxt
|
|
): Promise<Pick<Livestream,
|
|
| 'streamTitle'
|
|
| 'hub'
|
|
| 'rtmpPushUrl'
|
|
| 'rtmpPlayUrl'
|
|
| 'pcPushUrl'
|
|
| 'streamKey'
|
|
| 'expireAt'>
|
|
> {
|
|
const { streamTitle, expireAt, origin } = params;
|
|
// 获取七牛直播云信息
|
|
const {
|
|
instance,
|
|
config,
|
|
} = getConfig(context.getApplication()!.system!.config!, 'Live', origin);
|
|
assert(origin === 'qiniu');
|
|
const { hub, liveHost, publishDomain, playDomainType, playDomain, playKey, publishKey, publishSecurity } = config as QiniuLiveConfig;
|
|
const r = await (instance as QiniuCloudInstance).createLiveStream(hub, streamTitle, liveHost, publishDomain, playDomainType!, playDomain, expireAt, publishSecurity!, publishKey, playKey);
|
|
return {
|
|
streamTitle,
|
|
hub,
|
|
rtmpPushUrl: r.rtmpPushUrl,
|
|
rtmpPlayUrl: r.playUrl,
|
|
pcPushUrl: r.pcPushUrl,
|
|
streamKey: r.streamKey,
|
|
expireAt,
|
|
};
|
|
}
|
|
|
|
// 获取推拉流地址
|
|
/**
|
|
* 直播流已存在的情况下,获取推拉流地址
|
|
* @param streamTitle 直播流名称
|
|
* @param expireAt 推流过期时间
|
|
* @param context context
|
|
* @returns livestream对象
|
|
*/
|
|
export async function getStreamObj<ED extends EntityDict & BaseEntityDict, Cxt extends BackendRuntimeContext<ED>>(
|
|
params: {
|
|
origin: AccountOrigin;
|
|
streamTitle: string;
|
|
expireAt: number;
|
|
},
|
|
context: Cxt
|
|
): Promise<Pick<Livestream,
|
|
| 'streamTitle'
|
|
| 'hub'
|
|
| 'rtmpPushUrl'
|
|
| 'rtmpPlayUrl'
|
|
| 'pcPushUrl'
|
|
| 'streamKey'
|
|
| 'expireAt'>
|
|
> {
|
|
const { streamTitle, expireAt, origin } = params;
|
|
const {
|
|
instance,
|
|
config,
|
|
} = getConfig(context.getApplication()!.system!.config!, 'Live', origin);
|
|
|
|
assert(origin === 'qiniu');
|
|
const { playDomainType, publishDomain, publishSecurity, publishKey, playDomain, playKey, hub } = config as QiniuLiveConfig;
|
|
const r = (instance as QiniuCloudInstance).getStreamObj(hub, streamTitle, expireAt, publishDomain, playDomainType!, playDomain, publishSecurity!, publishKey, playKey);
|
|
return {
|
|
streamTitle,
|
|
hub,
|
|
rtmpPushUrl: r.rtmpPushUrl,
|
|
rtmpPlayUrl: r.playUrl,
|
|
pcPushUrl: r.pcPushUrl,
|
|
streamKey: r.streamKey,
|
|
expireAt,
|
|
};
|
|
}
|
|
|
|
// 生成直播回放
|
|
export async function getPlayBackUrl<ED extends EntityDict & BaseEntityDict, Cxt extends BackendRuntimeContext<ED>>(
|
|
params: {
|
|
origin: AccountOrigin;
|
|
streamTitle: string;
|
|
start: number;
|
|
end: number;
|
|
},
|
|
context: Cxt
|
|
) {
|
|
const { streamTitle, start, end, origin } = params;
|
|
// 获取七牛直播云信息
|
|
const {
|
|
config,
|
|
instance
|
|
} = getConfig(context.getApplication()!.system!.config!, 'Live', origin);
|
|
const { hub, playBackDomain, liveHost } = config as QiniuLiveConfig;
|
|
return (instance as QiniuCloudInstance).getPlayBackUrl(hub, playBackDomain, streamTitle, start, end, 'POST', liveHost);
|
|
} |