Skip to content

Commit

Permalink
Code cleanup; renmaing class files, removing dead code, refactor loca…
Browse files Browse the repository at this point in the history
…l URL building to helper
  • Loading branch information
chrisbenincasa committed May 30, 2024
1 parent 55c3155 commit 6900483
Show file tree
Hide file tree
Showing 19 changed files with 65 additions and 51 deletions.
4 changes: 2 additions & 2 deletions server/src/api/debugApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import {
} from '../dao/derived_types/StreamLineup.js';
import { Channel } from '../dao/entities/Channel.js';
import { PlexPlayer } from '../stream/plex/PlexPlayer.js';
import { PlexTranscoder } from '../stream/plex/plexTranscoder.js';
import { PlexTranscoder } from '../stream/plex/PlexTranscoder.js';
import { FillerPicker } from '../services/FillerPicker.js';
import { StreamContextChannel } from '../stream/types.js';
import { PlayerContext } from '../stream/player.js';
import { PlayerContext } from '../stream/Player.js';
import { Maybe } from '../types/util.js';
import { RouterPluginAsyncCallback } from '../types/serverType.js';
import { mapAsyncSeq } from '../util/index.js';
Expand Down
2 changes: 1 addition & 1 deletion server/src/api/hlsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { RouterPluginAsyncCallback } from '../types/serverType.js';
import fs from 'node:fs/promises';
import { join } from 'node:path';
import { isNil, isUndefined, map } from 'lodash-es';
import { sessionManager } from '../stream/sessionManager.js';
import { sessionManager } from '../stream/SessionManager.js';
import { z } from 'zod';
import { TruthyQueryParam } from '../types/schemas.js';
import { v4 } from 'uuid';
Expand Down
20 changes: 11 additions & 9 deletions server/src/api/videoApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import * as fsSync from 'node:fs';
import { Readable } from 'stream';
import { z } from 'zod';
import { FfmpegText } from '../ffmpeg/ffmpegText.js';
import { serverOptions } from '../globals.js';
import { ConcatStream } from '../stream/ConcatStream.js';
import { VideoStream } from '../stream/VideoStream.js';
import { StreamQueryStringSchema, TruthyQueryParam } from '../types/schemas.js';
import { RouterPluginAsyncCallback } from '../types/serverType.js';
import { LoggerFactory } from '../util/logging/LoggerFactory.js';
import { sessionManager } from '../stream/sessionManager.js';
import { sessionManager } from '../stream/SessionManager.js';
import { v4 } from 'uuid';
import { run } from '../util/index.js';
import { PassThrough } from 'node:stream';
import { makeLocalUrl } from '../util/serverUtil.js';

let StreamCount = 0;

Expand Down Expand Up @@ -344,13 +344,15 @@ export const videoRouter: RouterPluginAsyncCallback = async (fastify) => {
// We only need 2 entries + stream_loop on the concat command for an infinite
// stream. See https://trac.ffmpeg.org/wiki/Concatenate#Changingplaylistfilesonthefly
for (let i = 0; i < 2; i++) {
data += `file 'http://localhost:${
serverOptions().port
}/stream?channel=${
req.query.channel
}&session=${sessionId}&audioOnly=${audioOnly}&hls=${
req.query.hls
}&index=${i}'\n`;
const url = makeLocalUrl('/stream', {
channel: req.query.channel,
session: sessionId,
audioOnly,
hls: req.query.hls,
index: i,
});

data += `file '${url}'\n`;
}

return res.type('text').send(data);
Expand Down
2 changes: 1 addition & 1 deletion server/src/dao/fillerDb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
UpdateFillerListRequest,
} from '@tunarr/types/api';
import { filter, isNil, isString, map } from 'lodash-es';
import { ChannelCache } from '../stream/channelCache.js';
import { ChannelCache } from '../stream/ChannelCache.js';
import { mapAsyncSeq } from '../util/index.js';
import { ProgramConverter } from './converters/programConverters.js';
import { getEm } from './dataSource.js';
Expand Down
13 changes: 6 additions & 7 deletions server/src/ffmpeg/ffmpeg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import { isEmpty, isNil, isString, isUndefined, merge, round } from 'lodash-es';
import path from 'path';
import { DeepReadonly, DeepRequired } from 'ts-essentials';
import { serverOptions } from '../globals.js';
import { StreamDetails } from '../stream/plex/plexTranscoder.js';
import { StreamDetails } from '../stream/plex/PlexTranscoder.js';
import { StreamContextChannel } from '../stream/types.js';
import { Maybe } from '../types/util.js';
import { TypedEventEmitter } from '../types/eventEmitter.js';
import stream from 'stream';
import { Logger, LoggerFactory } from '../util/logging/LoggerFactory.js';
import { isNonEmptyString } from '../util/index.js';
import { makeLocalUrl } from '../util/serverUtil.js';

const spawn = child_process.spawn;

Expand Down Expand Up @@ -106,9 +107,7 @@ export class FFMPEG extends (events.EventEmitter as new () => TypedEventEmitter<
channel: channel.uuid,
});
this.opts = opts;
this.errorPicturePath = `http://localhost:${
serverOptions().port
}/images/generic-error-screen.png`;
this.errorPicturePath = makeLocalUrl('/images/generic-error-screen.png');
this.ffmpegName = 'unnamed ffmpeg';
this.channel = channel;
this.ffmpegPath = opts.ffmpegExecutablePath;
Expand Down Expand Up @@ -475,9 +474,9 @@ export class FFMPEG extends (events.EventEmitter as new () => TypedEventEmitter<
pic = streamStats.placeholderImage;
} else if (!isString(streamUrl) && streamUrl.errorTitle == 'offline') {
// TODO fix me
const defaultOfflinePic = `http://localhost:${
serverOptions().port
}/images/generic-offline-screen.png`;
const defaultOfflinePic = makeLocalUrl(
'/images/generic-offline-screen.png',
);
pic = this.channel.offlinePicture ?? defaultOfflinePic;
} else if (this.opts.errorScreen == 'pic') {
pic = this.errorPicturePath;
Expand Down
2 changes: 1 addition & 1 deletion server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { getSettings } from './dao/settings.js';
import { ServerOptions, serverOptions } from './globals.js';
import { ServerRequestContext, serverContext } from './serverContext.js';
import { GlobalScheduler, scheduleJobs } from './services/scheduler.js';
import { initPersistentStreamCache } from './stream/channelCache.js';
import { initPersistentStreamCache } from './stream/ChannelCache.js';
import { runFixers } from './tasks/fixers/index.js';
import { UpdateXmlTvTask } from './tasks/UpdateXmlTvTask.js';
import { filename, isProduction, run } from './util/index.js';
Expand Down
2 changes: 1 addition & 1 deletion server/src/serverContext.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AsyncLocalStorage } from 'async_hooks';
import { isUndefined, once } from 'lodash-es';
import path from 'path';
import { ChannelCache } from './stream/channelCache.js';
import { ChannelCache } from './stream/ChannelCache.js';
import { ChannelDB } from './dao/channelDb.js';
import { CustomShowDB } from './dao/customShowDb.js';
import { FillerDB } from './dao/fillerDb.js';
Expand Down
2 changes: 1 addition & 1 deletion server/src/services/FillerPicker.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { EntityDTO, Loaded } from '@mikro-orm/core';
import constants from '@tunarr/shared/constants';
import { isEmpty, isNil, isUndefined } from 'lodash-es';
import { ChannelCache } from '../stream/channelCache';
import { ChannelCache } from '../stream/ChannelCache';
import { Channel } from '../dao/entities/Channel';
import { ChannelFillerShow } from '../dao/entities/ChannelFillerShow';
import { Program } from '../dao/entities/Program';
Expand Down
2 changes: 1 addition & 1 deletion server/src/stream/ConcatSession.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { isUndefined } from 'lodash-es';
import { Maybe } from '../types/util';
import { ConcatStream, VideoStreamResult } from './ConcatStream';
import { SessionOptions, StreamSession } from './session';
import { SessionOptions, StreamSession } from './Session';
import { Channel } from '../dao/entities/Channel';

export class ConcatSession extends StreamSession {
Expand Down
15 changes: 7 additions & 8 deletions server/src/stream/ConcatStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { isNil, isUndefined, once, round } from 'lodash-es';
import { PassThrough, Readable } from 'node:stream';
import { v4 } from 'uuid';
import { ConcatOptions, FFMPEG } from '../ffmpeg/ffmpeg';
import { serverOptions } from '../globals';
import { getServerContext } from '../serverContext';
import { fileExists } from '../util/fsUtil';
import { LoggerFactory } from '../util/logging/LoggerFactory';
import { makeLocalUrl } from '../util/serverUtil.js';

export type VideoStreamSuccessResult = {
type: 'success';
Expand Down Expand Up @@ -89,15 +89,14 @@ export class ConcatStream {
outStream.push(null);
});

const concatUrl = new URL(
`http://localhost:${serverOptions().port}/playlist`,
);
concatUrl.searchParams.set('channel', channel.number.toString());
// TODO: Do we know this is true? We probably need to push a param through
concatUrl.searchParams.set('audioOnly', 'false');
concatUrl.searchParams.set('hls', `${!!concatOptions?.enableHls}`);
const concatUrl = makeLocalUrl('/playlist', {
channel: channel.number,
audioOnly: false,
hls: !!concatOptions?.enableHls,
});

const ff = ffmpeg.spawnConcat(concatUrl.toString(), concatOptions);
const ff = ffmpeg.spawnConcat(concatUrl, concatOptions);

if (isUndefined(ff)) {
return {
Expand Down
2 changes: 1 addition & 1 deletion server/src/stream/HlsSession.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from 'node:fs/promises';
import retry from 'async-retry';
import { SessionOptions, StreamReadyResult, StreamSession } from './session';
import { SessionOptions, StreamReadyResult, StreamSession } from './Session';
import { isNodeError } from '../util';
import { isError, isString } from 'lodash-es';
import { ConcatStream } from './ConcatStream';
Expand Down
4 changes: 2 additions & 2 deletions server/src/stream/VideoStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import {
generateChannelContext,
} from './StreamProgramCalculator';
import { wereThereTooManyAttempts } from './StreamThrottler';
import { PlayerContext } from './player';
import { ProgramPlayer } from './programPlayer';
import { PlayerContext } from './Player';
import { ProgramPlayer } from './ProgramPlayer';
import { StreamContextChannel } from './types';

type VideoStreamSuccessResult = {
Expand Down
8 changes: 3 additions & 5 deletions server/src/stream/offlinePlayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import EventEmitter from 'events';
import { isError } from 'lodash-es';
import { Readable, Writable } from 'stream';
import { FFMPEG, FfmpegEvents } from '../ffmpeg/ffmpeg.js';
import { serverOptions } from '../globals.js';
import { TypedEventEmitter } from '../types/eventEmitter.js';
import { Maybe } from '../types/util.js';
import { LoggerFactory } from '../util/logging/LoggerFactory.js';
import { Player, PlayerContext } from './player.js';
import { Player, PlayerContext } from './Player.js';
import { makeLocalUrl } from '../util/serverUtil.js';

export class OfflinePlayer extends Player {
private logger = LoggerFactory.child({ caller: import.meta });
Expand All @@ -29,9 +29,7 @@ export class OfflinePlayer extends Player {
if (context.isLoading === true) {
context.channel = {
...context.channel,
offlinePicture: `http://localhost:${
serverOptions().port
}/images/loading-screen.png`,
offlinePicture: makeLocalUrl('/images/loading-screen.png'),
offlineSoundtrack: undefined,
};
}
Expand Down
2 changes: 1 addition & 1 deletion server/src/stream/plex/PlexPlayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { TypedEventEmitter } from '../../types/eventEmitter.js';
import { Maybe, Nullable } from '../../types/util.js';
import { ifDefined } from '../../util/index.js';
import { LoggerFactory } from '../../util/logging/LoggerFactory.js';
import { Player, PlayerContext } from '../player.js';
import { Player, PlayerContext } from '../Player.js';
import { PlexStreamDetails } from './PlexStreamDetails.js';

const USED_CLIENTS: Record<string, boolean> = {};
Expand Down
10 changes: 5 additions & 5 deletions server/src/stream/plex/PlexStreamDetails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ import { PlexServerSettings } from '../../dao/entities/PlexServerSettings';
import { Plex, PlexApiFactory } from '../../external/plex';
import { Nullable } from '../../types/util';
import { Logger, LoggerFactory } from '../../util/logging/LoggerFactory';
import { PlexStream, StreamDetails } from './plexTranscoder';
import { PlexStream, StreamDetails } from './PlexTranscoder';
import { isNonEmptyString } from '../../util';
import { serverOptions } from '../../globals';
import { ContentBackedStreamLineupItem } from '../../dao/derived_types/StreamLineup.js';
import { SettingsDB } from '../../dao/settings.js';
import { makeLocalUrl } from '../../util/serverUtil.js';

// The minimum fields we need to get stream details about an item
type PlexItemStreamDetailsQuery = Pick<
Expand Down Expand Up @@ -168,9 +168,9 @@ export class PlexStreamDetails {
});

if (!isNonEmptyString(streamDetails.placeholderImage)) {
streamDetails.placeholderImage = `http://localhost:${
serverOptions().port
}/images/generic-music-screen.png`;
streamDetails.placeholderImage = makeLocalUrl(
'/images/generic-music-screen.png',
);
}
}

Expand Down
6 changes: 3 additions & 3 deletions server/src/stream/programPlayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import { FfmpegEvents } from '../ffmpeg/ffmpeg.js';
import { TypedEventEmitter } from '../types/eventEmitter.js';
import { Maybe } from '../types/util.js';
import { isNonEmptyString } from '../util/index.js';
import { OfflinePlayer } from './offlinePlayer.js';
import { Player, PlayerContext } from './player.js';
import { OfflinePlayer } from './OfflinePlayer.js';
import { Player, PlayerContext } from './Player.js';
import { PlexPlayer } from './plex/PlexPlayer.js';
import { StreamContextChannel } from './types.js';
import { LoggerFactory } from '../util/logging/LoggerFactory.js';
Expand Down Expand Up @@ -167,7 +167,7 @@ export class ProgramPlayer extends Player {
disableFillerOverlay = true;
}

if (type == 'commercial' && disableFillerOverlay) {
if (type === 'commercial' && disableFillerOverlay) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion server/src/stream/sessionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
SessionType,
StreamConnectionDetails,
StreamSession,
} from './session.js';
} from './Session.js';
import { isNil, isNull } from 'lodash-es';
import { Mutex } from 'async-mutex';
import { getEm } from '../dao/dataSource.js';
Expand Down
2 changes: 1 addition & 1 deletion server/src/tasks/CleanupSessionsTask.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ld, { filter, forEach, isEmpty, keys } from 'lodash-es';
import { sessionManager } from '../stream/sessionManager.js';
import { sessionManager } from '../stream/SessionManager.js';
import { Maybe } from '../types/util.js';
import { Task, TaskId } from './Task.js';

Expand Down
16 changes: 16 additions & 0 deletions server/src/util/serverUtil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import querystring, { ParsedUrlQueryInput } from 'node:querystring';
import { serverOptions } from '../globals.js';
import { isEmpty } from 'lodash-es';

export function makeLocalUrl(
path: string,
query: ParsedUrlQueryInput = {},
): string {
const stringifiedQuery = querystring.stringify(query);
const urlBase = `http://localhost:${serverOptions().port}${path}`;
if (!isEmpty(stringifiedQuery)) {
return `${urlBase}?${stringifiedQuery}`;
}

return urlBase;
}

0 comments on commit 6900483

Please sign in to comment.