aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Api/Playback/Hls/VideoHlsService.cs
blob: 7fcd4357be28c7399aaa18b88242bcc1326ead73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using MediaBrowser.Common.IO;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Library;
using System;

namespace MediaBrowser.Api.Playback.Hls
{
    public class VideoHlsService : BaseHlsService
    {
        public VideoHlsService(IServerApplicationPaths appPaths, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager)
            : base(appPaths, userManager, libraryManager, isoManager)
        {
        }

        /// <summary>
        /// Gets the audio arguments.
        /// </summary>
        /// <param name="state">The state.</param>
        /// <returns>System.String.</returns>
        protected override string GetAudioArguments(StreamState state)
        {
            if (!state.Request.AudioCodec.HasValue)
            {
                return "-codec:a:0 copy";
            }

            var codec = GetAudioCodec(state.Request);

            var args = "-codec:a:0 " + codec;

            if (state.AudioStream != null)
            {
                var channels = GetNumAudioChannelsParam(state.Request, state.AudioStream);

                if (channels.HasValue)
                {
                    args += " -ac " + channels.Value;
                }

                if (state.Request.AudioSampleRate.HasValue)
                {
                    args += " -ar " + state.Request.AudioSampleRate.Value;
                }

                if (state.Request.AudioBitRate.HasValue)
                {
                    args += " -ab " + state.Request.AudioBitRate.Value;
                }

                return args;
            }

            return args;
        }

        /// <summary>
        /// Gets the video arguments.
        /// </summary>
        /// <param name="state">The state.</param>
        /// <returns>System.String.</returns>
        protected override string GetVideoArguments(StreamState state)
        {
            var codec = GetVideoCodec(state.Request);

            // Right now all we support is either h264 or copy
            if (!codec.Equals("copy", StringComparison.OrdinalIgnoreCase) && !codec.Equals("libx264", StringComparison.OrdinalIgnoreCase))
            {
                codec = "libx264";
            }

            // See if we can save come cpu cycles by avoiding encoding
            if (codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
            {
                return IsH264(state.VideoStream) ? "-codec:v:0 copy -bsf h264_mp4toannexb" : "-codec:v:0 copy";
            }

            var args = "-codec:v:0 " + codec + " -preset superfast";

            if (state.Request.VideoBitRate.HasValue)
            {
                args += string.Format(" -b:v {0}", state.Request.VideoBitRate.Value);
            }

            // Add resolution params, if specified
            if (state.Request.Width.HasValue || state.Request.Height.HasValue || state.Request.MaxHeight.HasValue || state.Request.MaxWidth.HasValue)
            {
                args += GetOutputSizeParam(state, codec);
            }

            // Get the output framerate based on the FrameRate param
            double framerate = state.Request.Framerate ?? 0;

            // We have to supply a framerate for hls, so if it's null, account for that here
            if (framerate.Equals(0))
            {
                framerate = state.VideoStream.AverageFrameRate ?? 0;
            }
            if (framerate.Equals(0))
            {
                framerate = state.VideoStream.RealFrameRate ?? 0;
            }
            if (framerate.Equals(0))
            {
                framerate = 23.976;
            }

            args += string.Format(" -r {0}", framerate);

            // Needed to ensure segments stay under 10 seconds
            args += string.Format(" -g {0}", framerate);

            return args;
        }

        /// <summary>
        /// Gets the segment file extension.
        /// </summary>
        /// <param name="state">The state.</param>
        /// <returns>System.String.</returns>
        protected override string GetSegmentFileExtension(StreamState state)
        {
            return ".ts";
        }
    }
}