Переглянути джерело

+ added placeholder code for audio playback inside videos (disabled for now)
~ fixed video looping not working properly (fixes #102)

Signed-off-by: Alexis Maiquez Murcia <almamu@almamu.com>

Alexis Maiquez Murcia 4 роки тому
батько
коміт
a12d05a61d

+ 35 - 10
src/WallpaperEngine/Render/CVideo.cpp

@@ -47,6 +47,23 @@ CVideo::CVideo (Core::CVideo* video, CContainer* container, CContext* context) :
     if (avcodec_open2 (m_codecCtx, codec, NULL) < 0)
         throw std::runtime_error ("Failed to open codec");
 
+    // initialize audio if there's any audio stream
+    if (m_audioStream != -1)
+    {
+        const AVCodec* audioCodec = avcodec_find_decoder (m_formatCtx->streams [m_audioStream]->codecpar->codec_id);
+        if (audioCodec == nullptr)
+            throw std::runtime_error ("Failed to find codec");
+
+        AVCodecContext* audioContext = avcodec_alloc_context3 (audioCodec);
+        if (avcodec_parameters_to_context (audioContext, m_formatCtx->streams [m_audioStream]->codecpar))
+            throw std::runtime_error ("Failed to copy codec parameters");
+
+        if (avcodec_open2 (audioContext, audioCodec, NULL) < 0)
+            throw std::runtime_error ("Failed to open codec");
+
+        this->m_audio = new Audio::CAudioStream (audioContext);
+    }
+
     m_videoFrame = av_frame_alloc ();
     m_videoFrameRGB = av_frame_alloc ();
     if (m_videoFrameRGB == nullptr)
@@ -175,18 +192,26 @@ void CVideo::getNextFrame ()
             throw std::runtime_error (av_make_error_string (err, AV_ERROR_MAX_STRING_SIZE, readError));
         }
         
-    } while (packet.stream_index != m_videoStream);
+    } while (packet.stream_index != m_videoStream /*&& packet.stream_index != m_audioStream*/);
 
-    // Send video stream packet to codec
-    if (avcodec_send_packet (m_codecCtx, &packet) < 0)
-        return;
-    
-    // Receive frame from codec
-    if (avcodec_receive_frame (m_codecCtx, m_videoFrame) < 0)
-        return;
+    if (!eof && packet.stream_index == m_videoStream)
+    {
+        // Send video stream packet to codec
+        if (avcodec_send_packet (m_codecCtx, &packet) < 0)
+            return;
+
+        // Receive frame from codec
+        if (avcodec_receive_frame (m_codecCtx, m_videoFrame) < 0)
+            return;
 
-    sws_scale (m_swsCtx, (uint8_t const* const*) m_videoFrame->data, m_videoFrame->linesize,
-               0, m_codecCtx->height, m_videoFrameRGB->data, m_videoFrameRGB->linesize);
+        sws_scale (m_swsCtx, (uint8_t const* const*) m_videoFrame->data, m_videoFrame->linesize,
+                   0, m_codecCtx->height, m_videoFrameRGB->data, m_videoFrameRGB->linesize);
+
+    }
+    /*else if (packet.stream_index == m_audioStream)
+    {
+        this->m_audio->queuePacket (&packet);
+    }*/
 
     av_packet_unref (&packet);
 

+ 3 - 0
src/WallpaperEngine/Render/CVideo.h

@@ -2,6 +2,7 @@
 
 #include "WallpaperEngine/Core/CVideo.h"
 
+#include "WallpaperEngine/Audio/CAudioStream.h"
 #include "WallpaperEngine/Render/CWallpaper.h"
 
 extern "C"
@@ -46,6 +47,8 @@ namespace WallpaperEngine::Render
         uint8_t* m_buffer = nullptr;
         int m_videoStream = -1, m_audioStream = -1;
 
+        Audio::CAudioStream* m_audio = nullptr;
+
         /**
          * The texture used for the video output
          */