[vlc-commits] chromecast: refactor

Thomas Guillem git at videolan.org
Tue Mar 27 11:38:53 CEST 2018


vlc | branch: master | Thomas Guillem <thomas at gllm.fr> | Tue Mar 27 08:58:48 2018 +0200| [b6484305815cb39606f1b47f92d5f5b2e6d1d387] | committer: Thomas Guillem

chromecast: refactor

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=b6484305815cb39606f1b47f92d5f5b2e6d1d387
---

 modules/stream_out/chromecast/cast.cpp | 229 ++++++++++++++++++++-------------
 1 file changed, 141 insertions(+), 88 deletions(-)

diff --git a/modules/stream_out/chromecast/cast.cpp b/modules/stream_out/chromecast/cast.cpp
index 22532e7e7c..78533c2a10 100644
--- a/modules/stream_out/chromecast/cast.cpp
+++ b/modules/stream_out/chromecast/cast.cpp
@@ -152,6 +152,8 @@ struct sout_stream_sys_t
     unsigned int                       spu_streams_count;
 
 private:
+    std::string GetAcodecOption( sout_stream_t *, vlc_fourcc_t *, const audio_format_t *, int );
+    std::string GetVcodecOption( sout_stream_t *, vlc_fourcc_t *, const video_format_t *, int );
     bool UpdateOutput( sout_stream_t * );
 };
 
@@ -164,7 +166,6 @@ struct sout_stream_id_sys_t
 
 #define SOUT_CFG_PREFIX "sout-chromecast-"
 
-static const vlc_fourcc_t DEFAULT_TRANSCODE_VIDEO = VLC_CODEC_H264;
 static const char DEFAULT_MUXER[] = "avformat{mux=matroska,options={live=1}}";
 static const char DEFAULT_MUXER_WEBM[] = "avformat{mux=webm,options={live=1}}";
 
@@ -917,6 +918,141 @@ bool sout_stream_sys_t::transcodingCanFallback() const
     return transcoding_state != (TRANSCODING_VIDEO|TRANSCODING_AUDIO);
 }
 
+static std::string GetVencX264Option( sout_stream_t * /* p_stream */,
+                                      const video_format_t *p_vid,
+                                      int i_quality )
+{
+    std::stringstream ssout;
+    static const char video_x264_preset_veryfast[] = "veryfast";
+    static const char video_x264_preset_ultrafast[] = "ultrafast";
+    const char *psz_video_x264_preset;
+    unsigned i_video_x264_crf_hd, i_video_x264_crf_720p;
+
+    switch ( i_quality )
+    {
+        case CONVERSION_QUALITY_HIGH:
+            i_video_x264_crf_hd = i_video_x264_crf_720p = 21;
+            psz_video_x264_preset = video_x264_preset_veryfast;
+            break;
+        case CONVERSION_QUALITY_MEDIUM:
+            i_video_x264_crf_hd = 23;
+            i_video_x264_crf_720p = 21;
+            psz_video_x264_preset = video_x264_preset_veryfast;
+            break;
+        case CONVERSION_QUALITY_LOW:
+            i_video_x264_crf_hd = i_video_x264_crf_720p = 23;
+            psz_video_x264_preset = video_x264_preset_veryfast;
+            break;
+        default:
+        case CONVERSION_QUALITY_LOWCPU:
+            i_video_x264_crf_hd = i_video_x264_crf_720p = 23;
+            psz_video_x264_preset = video_x264_preset_ultrafast;
+            break;
+    }
+
+    const bool b_hdres = p_vid == NULL || p_vid->i_height == 0 || p_vid->i_height >= 800;
+    unsigned i_video_x264_crf = b_hdres ? i_video_x264_crf_hd : i_video_x264_crf_720p;
+
+    ssout << "venc=x264{preset=" << psz_video_x264_preset
+          << ",crf=" << i_video_x264_crf << "}";
+    return ssout.str();
+}
+
+std::string
+sout_stream_sys_t::GetVcodecOption( sout_stream_t *p_stream, vlc_fourcc_t *p_codec_video,
+                                    const video_format_t *p_vid, int i_quality )
+{
+    std::stringstream ssout;
+    static const char video_maxres_hd[] = "maxwidth=1920,maxheight=1080";
+    static const char video_maxres_720p[] = "maxwidth=1280,maxheight=720";
+    const char *psz_video_maxres;
+
+    switch ( i_quality )
+    {
+        case CONVERSION_QUALITY_HIGH:
+        case CONVERSION_QUALITY_MEDIUM:
+            psz_video_maxres = video_maxres_hd;
+            break;
+        default:
+            psz_video_maxres = video_maxres_720p;
+    }
+
+    *p_codec_video = VLC_CODEC_H264;
+
+    std::string venc_option;
+    if( module_exists("x264") )
+        venc_option = GetVencX264Option( p_stream, p_vid, i_quality );
+
+    msg_Dbg( p_stream, "Converting video to %.4s", (const char*)p_codec_video );
+
+    char fourcc[5];
+    ssout << "vcodec=";
+    vlc_fourcc_to_char( *p_codec_video, fourcc );
+    fourcc[4] = '\0';
+    ssout << fourcc << ',' << psz_video_maxres << ',';
+
+    if( p_vid == NULL
+     || p_vid->i_frame_rate == 0 || p_vid->i_frame_rate_base == 0
+     || ( p_vid->i_frame_rate / p_vid->i_frame_rate_base ) > 30 )
+    {
+        /* Even force 24fps if the frame rate is unknown */
+        msg_Warn( p_stream, "lowering frame rate to 24fps" );
+        ssout << "fps=24,";
+    }
+
+    if( !venc_option.empty() )
+        ssout << venc_option << ",";
+
+    return ssout.str();
+}
+
+std::string
+sout_stream_sys_t::GetAcodecOption( sout_stream_t *p_stream, vlc_fourcc_t *p_codec_audio,
+                                    const audio_format_t *p_aud, int i_quality )
+{
+    std::stringstream ssout;
+
+    bool b_audio_mp3;
+
+    /* If we were already transcoding: force mp3 because maybe the CC may
+     * have failed because of vorbis. */
+    if( transcoding_state & TRANSCODING_AUDIO )
+        b_audio_mp3 = true;
+    else
+    {
+        switch ( i_quality )
+        {
+            case CONVERSION_QUALITY_HIGH:
+            case CONVERSION_QUALITY_MEDIUM:
+                b_audio_mp3 = false;
+                break;
+            default:
+                b_audio_mp3 = true;
+                break;
+        }
+    }
+
+    if ( !b_audio_mp3
+      && p_aud->i_channels > 2 && module_exists( "vorbis" ) )
+        *p_codec_audio = VLC_CODEC_VORBIS;
+    else
+        *p_codec_audio = VLC_CODEC_MP3;
+
+    msg_Dbg( p_stream, "Converting audio to %.4s", (const char*)p_codec_audio );
+
+    ssout << "acodec=";
+    char fourcc[5];
+    vlc_fourcc_to_char( *p_codec_audio, fourcc );
+    fourcc[4] = '\0';
+    ssout << fourcc << ',';
+
+    /* XXX: higher vorbis qualities can cause glitches on some CC
+     * devices (Chromecast 1 & 2) */
+    if( *p_codec_audio == VLC_CODEC_VORBIS )
+        ssout << "aenc=vorbis{quality=4},";
+    return ssout.str();
+}
+
 bool sout_stream_sys_t::UpdateOutput( sout_stream_t *p_stream )
 {
     assert( p_stream->p_sys == this );
@@ -1025,103 +1161,20 @@ bool sout_stream_sys_t::UpdateOutput( sout_stream_t *p_stream )
                 config_PutInt(SOUT_CFG_PREFIX "show-perf-warning", 0 );
         }
 
-        static const char video_maxres_hd[] = "maxwidth=1920,maxheight=1080";
-        static const char video_maxres_720p[] = "maxwidth=1280,maxheight=720";
-        static const char video_x264_preset_veryfast[] = "veryfast";
-        static const char video_x264_preset_ultrafast[] = "ultrafast";
-
         const int i_quality = var_InheritInteger( p_stream, SOUT_CFG_PREFIX "conversion-quality" );
-        const char *psz_video_maxres;
-        const char *psz_video_x264_preset;
-        unsigned i_video_x264_crf_hd, i_video_x264_crf_720p;
-        bool b_audio_mp3;
-
-        switch ( i_quality )
-        {
-            case CONVERSION_QUALITY_HIGH:
-                psz_video_maxres = video_maxres_hd;
-                i_video_x264_crf_hd = i_video_x264_crf_720p = 21;
-                psz_video_x264_preset = video_x264_preset_veryfast;
-                b_audio_mp3 = false;
-                break;
-            case CONVERSION_QUALITY_MEDIUM:
-                psz_video_maxres = video_maxres_hd;
-                i_video_x264_crf_hd = 23;
-                i_video_x264_crf_720p = 21;
-                psz_video_x264_preset = video_x264_preset_veryfast;
-                b_audio_mp3 = false;
-                break;
-            case CONVERSION_QUALITY_LOW:
-                psz_video_maxres = video_maxres_720p;
-                i_video_x264_crf_hd = i_video_x264_crf_720p = 23;
-                psz_video_x264_preset = video_x264_preset_veryfast;
-                b_audio_mp3 = true;
-                break;
-            default:
-            case CONVERSION_QUALITY_LOWCPU:
-                psz_video_maxres = video_maxres_720p;
-                i_video_x264_crf_hd = i_video_x264_crf_720p = 23;
-                psz_video_x264_preset = video_x264_preset_ultrafast;
-                b_audio_mp3 = true;
-                break;
-        }
-
-        /* If we were already transcoding: force mp3 because maybe the CC may
-         * have failed because of vorbis. */
-        if (transcoding_state & TRANSCODING_AUDIO)
-            b_audio_mp3 = true;
 
         /* TODO: provide audio samplerate and channels */
         ssout << "transcode{";
-        char s_fourcc[5];
         if ( i_codec_audio == 0 && p_original_audio )
         {
-            if ( !b_audio_mp3
-              && p_original_audio->audio.i_channels > 2 && module_exists( "vorbis" ) )
-                i_codec_audio = VLC_CODEC_VORBIS;
-            else
-                i_codec_audio = VLC_CODEC_MP3;
-
-            msg_Dbg( p_stream, "Converting audio to %.4s", (const char*)&i_codec_audio );
-            ssout << "acodec=";
-            vlc_fourcc_to_char( i_codec_audio, s_fourcc );
-            s_fourcc[4] = '\0';
-            ssout << s_fourcc << ',';
-
-            /* XXX: higher vorbis qualities can cause glitches on some CC
-             * devices (Chromecast 1 & 2) */
-            if( i_codec_audio == VLC_CODEC_VORBIS )
-                ssout << "aenc=vorbis{quality=4},";
+            ssout << GetAcodecOption( p_stream, &i_codec_audio,
+                                      &p_original_audio->audio, i_quality );
             new_transcoding_state |= TRANSCODING_AUDIO;
         }
         if ( i_codec_video == 0 && p_original_video )
         {
-            i_codec_video = DEFAULT_TRANSCODE_VIDEO;
-            msg_Dbg( p_stream, "Converting video to %.4s", (const char*)&i_codec_video );
-            ssout << "vcodec=";
-            vlc_fourcc_to_char( i_codec_video, s_fourcc );
-            s_fourcc[4] = '\0';
-            ssout << s_fourcc << ',' << psz_video_maxres << ',';
-
-            const video_format_t *p_vid = &p_original_video->video;
-            const bool b_hdres = p_vid == NULL || p_vid->i_height == 0 || p_vid->i_height >= 800;
-            unsigned i_video_x264_crf = b_hdres ? i_video_x264_crf_hd : i_video_x264_crf_720p;
-
-            if( p_vid == NULL
-             || p_vid->i_frame_rate == 0 || p_vid->i_frame_rate_base == 0
-             || ( p_vid->i_frame_rate / p_vid->i_frame_rate_base ) > 30 )
-            {
-                /* Even force 24fps if the frame rate is unknown */
-                msg_Warn( p_stream, "lowering frame rate to 24fps" );
-                ssout << "fps=24,";
-            }
-
-            if( i_codec_video == VLC_CODEC_H264 )
-            {
-                if ( module_exists("x264") )
-                    ssout << "venc=x264{preset=" << psz_video_x264_preset
-                          << ",crf=" << i_video_x264_crf << "},";
-            }
+            ssout << GetVcodecOption( p_stream, &i_codec_video,
+                                      &p_original_video->video, i_quality );
             new_transcoding_state |= TRANSCODING_VIDEO;
         }
         if ( p_original_spu )



More information about the vlc-commits mailing list