Skip to content

Commit

Permalink
refactor: use new transcode configs everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbenincasa committed Dec 20, 2024
1 parent 893e31d commit 6660b48
Show file tree
Hide file tree
Showing 43 changed files with 947 additions and 561 deletions.
6 changes: 6 additions & 0 deletions server/src/api/debug/debugFfmpegApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,14 @@ export const debugFfmpegApiRouter: RouterPluginAsyncCallback = async (
const channel = await req.serverCtx.channelDB.getChannel(
req.query.channel,
);

if (!channel) {
return res.status(404).send();
}

const transcodeConfig =
await req.serverCtx.transcodeConfigDB.getChannelConfig(channel.uuid);

const details = new LocalFileStreamDetails(req.query.path);
const streamDetails = await details.getStream();

Expand All @@ -50,6 +55,7 @@ export const debugFfmpegApiRouter: RouterPluginAsyncCallback = async (

const ffmpeg = new FfmpegStreamFactory(
req.serverCtx.settings.ffmpegSettings(),
transcodeConfig,
channel,
);

Expand Down
95 changes: 85 additions & 10 deletions server/src/api/debug/debugStreamApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { getDatabase } from '@/db/DBAccess.ts';
import { createOfflineStreamLineupItem } from '@/db/derived_types/StreamLineup.ts';
import { AllChannelTableKeys, Channel } from '@/db/schema/Channel.ts';
import { ProgramDao, ProgramType } from '@/db/schema/Program.ts';
import {
AllTranscodeConfigColumns,
TranscodeConfig,
} from '@/db/schema/TranscodeConfig.ts';
import { MpegTsOutputFormat } from '@/ffmpeg/builder/constants.ts';
import { serverContext } from '@/serverContext.ts';
import { OfflineProgramStream } from '@/stream/OfflinePlayer.ts';
Expand All @@ -12,7 +16,7 @@ import { PlexProgramStream } from '@/stream/plex/PlexProgramStream.ts';
import { TruthyQueryParam } from '@/types/schemas.ts';
import { RouterPluginAsyncCallback } from '@/types/serverType.ts';
import { jsonObjectFrom } from 'kysely/helpers/sqlite';
import { first, isNumber, isUndefined, nth, random } from 'lodash-es';
import { isNumber, isUndefined, nth, random } from 'lodash-es';
import { PassThrough } from 'stream';
import { z } from 'zod';

Expand All @@ -34,6 +38,19 @@ export const debugStreamApiRouter: RouterPluginAsyncCallback = async (
const channel = await getDatabase()
.selectFrom('channel')
.selectAll()
.select((eb) =>
jsonObjectFrom(
eb
.selectFrom('transcodeConfig')
.whereRef(
'transcodeConfig.uuid',
'=',
'channel.transcodeConfigId',
)
.select(AllTranscodeConfigColumns),
).as('transcodeConfig'),
)
.$narrowType<{ transcodeConfig: TranscodeConfig }>()
.executeTakeFirstOrThrow();

const stream = new OfflineProgramStream(
Expand All @@ -47,7 +64,8 @@ export const debugStreamApiRouter: RouterPluginAsyncCallback = async (
false,
false,
true,
req.query.useNewPipeline,
req.query.useNewPipeline ?? false,
channel.transcodeConfig,
),
MpegTsOutputFormat,
);
Expand All @@ -72,15 +90,30 @@ export const debugStreamApiRouter: RouterPluginAsyncCallback = async (
const channel = await getDatabase()
.selectFrom('channel')
.selectAll()
.select((eb) =>
jsonObjectFrom(
eb
.selectFrom('transcodeConfig')
.whereRef(
'transcodeConfig.uuid',
'=',
'channel.transcodeConfigId',
)
.select(AllTranscodeConfigColumns),
).as('transcodeConfig'),
)
.$narrowType<{ transcodeConfig: TranscodeConfig }>()
.executeTakeFirstOrThrow();

const stream = new OfflineProgramStream(
true,
PlayerContext.error(
30_000,
'',
channel,
true,
req.query.useNewPipeline,
req.query.useNewPipeline ?? false,
channel.transcodeConfig,
),
MpegTsOutputFormat,
);
Expand Down Expand Up @@ -109,6 +142,18 @@ export const debugStreamApiRouter: RouterPluginAsyncCallback = async (
eb
.selectFrom('channel')
.whereRef('channel.uuid', '=', 'channelPrograms.channelUuid')
.select((eb) =>
jsonObjectFrom(
eb
.selectFrom('transcodeConfig')
.whereRef(
'transcodeConfig.uuid',
'=',
'channel.transcodeConfigId',
)
.select(AllTranscodeConfigColumns),
).as('transcodeConfig'),
)
.select(AllChannelTableKeys),
).as('channel'),
)
Expand All @@ -120,7 +165,11 @@ export const debugStreamApiRouter: RouterPluginAsyncCallback = async (
return res.status(404);
}

const out = await initStream(program, firstChannel);
const out = await initStream(
program,
firstChannel,
firstChannel.transcodeConfig!,
);
return res.header('Content-Type', 'video/mp2t').send(out);
});

Expand Down Expand Up @@ -161,6 +210,18 @@ export const debugStreamApiRouter: RouterPluginAsyncCallback = async (
eb
.selectFrom('channel')
.whereRef('channel.uuid', '=', 'channelPrograms.channelUuid')
.select((eb) =>
jsonObjectFrom(
eb
.selectFrom('transcodeConfig')
.whereRef(
'transcodeConfig.uuid',
'=',
'channel.transcodeConfigId',
)
.select(AllTranscodeConfigColumns),
).as('transcodeConfig'),
)
.select(AllChannelTableKeys),
).as('channel'),
)
Expand All @@ -169,17 +230,29 @@ export const debugStreamApiRouter: RouterPluginAsyncCallback = async (
let firstChannel = nth(channels, 0)?.channel;

if (!firstChannel) {
firstChannel = await req.serverCtx.channelDB
.getAllChannels()
.then((channels) => first(channels) ?? null);
if (!firstChannel) {
return res.status(404);
}
firstChannel = await getDatabase()
.selectFrom('channel')
.selectAll()
.select((eb) =>
jsonObjectFrom(
eb
.selectFrom('transcodeConfig')
.whereRef(
'transcodeConfig.uuid',
'=',
'channel.transcodeConfigId',
)
.select(AllTranscodeConfigColumns),
).as('transcodeConfig'),
)
.$narrowType<{ transcodeConfig: TranscodeConfig }>()
.executeTakeFirstOrThrow();
}

const outStream = await initStream(
program,
firstChannel,
firstChannel.transcodeConfig!,
startTime * 1000,
req.query.useNewPipeline,
);
Expand All @@ -190,6 +263,7 @@ export const debugStreamApiRouter: RouterPluginAsyncCallback = async (
async function initStream(
program: ProgramDao,
channel: Channel,
transcodeConfig: TranscodeConfig,
startTime: number = 0,
useNewPipeline: boolean = false,
) {
Expand All @@ -204,6 +278,7 @@ export const debugStreamApiRouter: RouterPluginAsyncCallback = async (
false,
true,
useNewPipeline,
transcodeConfig,
);

let stream: ProgramStream;
Expand Down
2 changes: 1 addition & 1 deletion server/src/api/ffmpegSettingsApi.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TrannscodeConfig as TrannscodeConfigDao } from '@/db/schema/TranscodeConfig.ts';
import { TranscodeConfig as TrannscodeConfigDao } from '@/db/schema/TranscodeConfig.ts';
import { serverOptions } from '@/globals.js';
import { RouterPluginCallback } from '@/types/serverType.js';
import { firstDefined } from '@/util/index.js';
Expand Down
6 changes: 3 additions & 3 deletions server/src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MediaSourceType } from '@/db/schema/MediaSource.ts';
import { MediaSourceApiFactory } from '@/external/MediaSourceApiFactory.js';
import { FFMPEGInfo } from '@/ffmpeg/ffmpegInfo.js';
import { FfmpegInfo } from '@/ffmpeg/ffmpegInfo.js';
import { serverOptions } from '@/globals.js';
import { GlobalScheduler } from '@/services/Scheduler.ts';
import { UpdateXmlTvTask } from '@/tasks/UpdateXmlTvTask.js';
Expand Down Expand Up @@ -81,7 +81,7 @@ export const apiRouter: RouterPluginAsyncCallback = async (fastify) => {
async (req, res) => {
try {
const ffmpegSettings = req.serverCtx.settings.ffmpegSettings();
const v = await new FFMPEGInfo(ffmpegSettings).getVersion();
const v = await new FfmpegInfo(ffmpegSettings).getVersion();
let tunarrVersion: string = getTunarrVersion();
if (!isProduction) {
tunarrVersion += `-dev`;
Expand All @@ -101,7 +101,7 @@ export const apiRouter: RouterPluginAsyncCallback = async (fastify) => {
);

fastify.get('/ffmpeg-info', async (req, res) => {
const info = new FFMPEGInfo(req.serverCtx.settings.ffmpegSettings());
const info = new FfmpegInfo(req.serverCtx.settings.ffmpegSettings());
const [audioEncoders, videoEncoders] = await Promise.all([
run(async () => {
const res = await info.getAvailableAudioEncoders();
Expand Down
45 changes: 18 additions & 27 deletions server/src/db/ChannelDB.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ChannelQueryBuilder } from '@/db/ChannelQueryBuilder.ts';
import { globalOptions } from '@/globals.ts';
import { serverContext } from '@/serverContext.ts';
import { ChannelNotFoundError } from '@/types/errors.ts';
Expand Down Expand Up @@ -97,7 +98,10 @@ import {
} from './schema/Channel.ts';
import { programExternalIdString } from './schema/Program.ts';
import { ChannelTranscodingSettings } from './schema/base.ts';
import { ChannelWithPrograms as RawChannelWithPrograms } from './schema/derivedTypes.js';
import {
ChannelWithRelations,
ChannelWithPrograms as RawChannelWithPrograms,
} from './schema/derivedTypes.js';

// We use this to chunk super huge channel / program relation updates because
// of the way that mikro-orm generates these (e.g. "delete from XYZ where () or () ...").
Expand Down Expand Up @@ -158,31 +162,6 @@ function updateRequestToChannel(updateReq: SaveChannelRequest): ChannelUpdate {
guideFlexTitle: updateReq.guideFlexTitle,
transcodeConfigId: updateReq.transcodeConfigId,
} satisfies ChannelUpdate;
// return omitBy<ChannelUpdate>(
// {
// number: updateReq.number,
// watermark: sanitizeChannelWatermark(updateReq.watermark),
// icon: updateReq.icon,
// guideMinimumDuration: updateReq.guideMinimumDuration,
// groupTitle: updateReq.groupTitle,
// disableFillerOverlay: updateReq.disableFillerOverlay,
// startTime: updateReq.startTime,
// offline: updateReq.offline,
// name: updateReq.name,
// transcoding: isEmpty(transcoding)
// ? undefined
// : {
// targetResolution: transcoding?.targetResolution,
// videoBitrate: transcoding?.videoBitrate,
// videoBufferSize: transcoding?.videoBufferSize,
// },
// duration: updateReq.duration,
// stealth: updateReq.stealth,
// fillerRepeatCooldown: updateReq.fillerRepeatCooldown,
// guideFlexTitle: updateReq.guideFlexTitle,
// },
// isNil,
// );
}

function createRequestToChannel(saveReq: SaveChannelRequest): NewChannel {
Expand Down Expand Up @@ -260,7 +239,15 @@ export class ChannelDB {
return !isNil(channel);
}

getChannel(id: string | number, includeFiller: boolean = false) {
getChannel(id: string | number): Promise<Maybe<ChannelWithRelations>>;
getChannel(
id: string | number,
includeFiller: true,
): Promise<Maybe<MarkRequired<ChannelWithRelations, 'fillerShows'>>>;
getChannel(
id: string | number,
includeFiller: boolean = false,
): Promise<Maybe<ChannelWithRelations>> {
return getDatabase()
.selectFrom('channel')
.$if(isString(id), (eb) => eb.where('channel.uuid', '=', id as string))
Expand All @@ -284,6 +271,10 @@ export class ChannelDB {
.executeTakeFirst();
}

getChannelBuilder(id: string | number) {
return ChannelQueryBuilder.createForIdOrNumber(getDatabase(), id);
}

getChannelAndPrograms(
uuid: string,
): Promise<RawChannelWithPrograms | undefined> {
Expand Down
Loading

0 comments on commit 6660b48

Please sign in to comment.