Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: implement set bitrate of tracks #45

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions Sonar.Player/Sonar.Player.Api/Controllers/SetBitrateController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Sonar.Player.Api.Controllers.Dto;
using Sonar.Player.Application.Properties.Bitrate.Commands;
using Sonar.Player.Application.Queue.Commands;
using Sonar.Player.Application.Queue.Queries;
using Sonar.Player.Application.Services;
using Sonar.Player.Application.Tools;

namespace Sonar.Player.Api.Controllers;

[ApiController]
[Route("[controller]")]
public class SetBitrateController : Controller
{
private readonly IMediator _mediator;
private readonly IUserService _userService;

public SetBitrateController(IMediator mediator, IUserService userService)
{
_mediator = mediator;
_userService = userService;
}

[HttpPatch("lowBitrate")]
public async Task<ActionResult<SetBitrate.Response>> SetLowAsync(
[FromHeader(Name = "Token")] string token,
[FromQuery(Name = "id")] Guid trackId,
[FromForm] TrackFormDto form,
CancellationToken cancellationToken)
{
await using var fileStream = form.File.OpenReadStream();
return Ok(
await _mediator.Send(
new SetBitrate.Command(
token,
trackId,
form.Name,
new SetBitrate.Command.TrackFile(
form.File.FileName,
fileStream),
(int) BitrateEnum.LowBitrate
), cancellationToken));
}

[HttpPatch("mediumBitrate")]
public async Task<ActionResult<SetBitrate.Response>> SetMediumAsync(
[FromHeader(Name = "Token")] string token,
[FromQuery(Name = "id")] Guid trackId,
[FromForm] TrackFormDto form,
CancellationToken cancellationToken)
{
await using var fileStream = form.File.OpenReadStream();
return Ok(
await _mediator.Send(
new SetBitrate.Command(
token,
trackId,
form.Name,
new SetBitrate.Command.TrackFile(
form.File.FileName,
fileStream),
(int) BitrateEnum.MediumBitrate
), cancellationToken));
Patisson4 marked this conversation as resolved.
Show resolved Hide resolved
}

[HttpPatch("highBitrate")]
public async Task<ActionResult<SetBitrate.Response>> SetHighAsync(
[FromHeader(Name = "Token")] string token,
[FromQuery(Name = "id")] Guid trackId,
[FromForm] TrackFormDto form,
CancellationToken cancellationToken)
{
await using var fileStream = form.File.OpenReadStream();
return Ok(
await _mediator.Send(
new SetBitrate.Command(
token,
trackId,
form.Name,
new SetBitrate.Command.TrackFile(
form.File.FileName,
fileStream),
(int) BitrateEnum.HighBitrate
), cancellationToken));
}
}
2 changes: 2 additions & 0 deletions Sonar.Player/Sonar.Player.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Serilog;
using Sonar.Player.Api.Bootstraps;
using Sonar.Player.Api.Filters;
using Sonar.Player.Application.Properties;
using Sonar.Player.Application.Services;
using Sonar.Player.Application.Services.TracksStorage;
using Sonar.Player.Application.Tools;
Expand All @@ -13,6 +14,7 @@

var builder = WebApplication.CreateBuilder(args);

var settings = Settings.GetInstance();
builder.Host.UseSerilog((ctx, lc) => lc.WriteTo.Console());

builder.Services.AddControllers(options => options.Filters.Add<ExceptionFilter>());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System.Linq;
using MediatR;
using Sonar.Player.Application.Services.TracksStorage;
using Sonar.Player.Application.Tools;
using Sonar.Player.Application.Tools.Exceptions;
using Sonar.Player.Data;
using Sonar.Player.Domain.Entities;
using Sonar.Player.Domain.Enumerations;
using Sonar.Player.Domain.Models;
using Sonar.UserTracksManagement.ApiClient;

namespace Sonar.Player.Application.Properties.Bitrate.Commands;

public static class SetBitrate
{
public record Command(string Token, Guid TrackId, string Name, Command.TrackFile File, int Bitrate) : IRequest<Response>
{
public record TrackFile(string Name, Stream Content);
};

public record Response(Guid TrackId);

public class CommandHandler : IRequestHandler<Command, Response>
{
private readonly PlayerDbContext _dbContext;
private readonly IUserTracksApiClient _userTracksApiClient;
private readonly ITrackStorage _trackStorage;

public CommandHandler(PlayerDbContext dbContext, IUserTracksApiClient userTracksApiClient, ITrackStorage trackStorage)
{
_dbContext = dbContext;
_userTracksApiClient = userTracksApiClient;
_trackStorage = trackStorage;
}
public async Task<Response> Handle(Command request, CancellationToken cancellationToken)
{
bool isEnoughAccess = await _userTracksApiClient.IsEnoughAccessAsync(
request.Token,
request.TrackId,
cancellationToken);

if (!isEnoughAccess)
{
throw new NotEnoughAccessException($"Not enough access to track {request.TrackId}");
}

await _trackStorage.DeleteTrack(request.TrackId);

Settings.Bitrate = request.Bitrate;
var track = await _trackStorage.SaveTrack(request.TrackId, AudioFormat.FromFileName(request.File.Name), request.File.Content);

_dbContext.Tracks.Update(track);

return new Response(request.TrackId);
}
}
}
16 changes: 16 additions & 0 deletions Sonar.Player/Sonar.Player.Application/Properties/Settings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Sonar.Player.Application.Tools;

namespace Sonar.Player.Application.Properties;

public class Settings
{
private static Settings? _instance = null;
public static int Bitrate = (int) BitrateEnum.MediumBitrate;

private Settings() { }

public static Settings GetInstance()
{
return _instance ??= new Settings();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0_0

}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics;
using Sonar.Player.Application.Properties;
using Sonar.Player.Application.Tools;
using Sonar.Player.Domain.Entities;
using Sonar.Player.Domain.Enumerations;
Expand Down Expand Up @@ -32,7 +33,7 @@ public async Task<Track> SaveTrack(Guid id, MediaFormat format, Stream content)

var arguments = FfmpegArgumentsBuilder.CreateBuilder()
.GetSource(trackPath)
.SetBitrate(324)
.SetBitrate(Settings.Bitrate)
.SetSegmentFilename(segmentPath)
.WriteTo(infoFilePath)
.Build();
Expand Down
8 changes: 8 additions & 0 deletions Sonar.Player/Sonar.Player.Application/Tools/BitrateEnum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Sonar.Player.Application.Tools;

public enum BitrateEnum : int
{
LowBitrate = 192,
MediumBitrate = 256,
HighBitrate = 320
}