1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include "gstframegrabber.hxx"
21 #include "gstplayer.hxx"
22 
23 #include <cppuhelper/supportsservice.hxx>
24 
25 #include <gst/gstbuffer.h>
26 #include <gst/video/video.h>
27 #include <gst/video/gstvideosink.h>
28 
29 #include <vcl/graph.hxx>
30 #include <vcl/BitmapTools.hxx>
31 
32 #include <string>
33 
34 #define AVMEDIA_GST_FRAMEGRABBER_IMPLEMENTATIONNAME "com.sun.star.comp.avmedia.FrameGrabber_GStreamer"
35 #define AVMEDIA_GST_FRAMEGRABBER_SERVICENAME "com.sun.star.media.FrameGrabber_GStreamer"
36 
37 using namespace ::com::sun::star;
38 
39 namespace avmedia { namespace gstreamer {
40 
disposePipeline()41 void FrameGrabber::disposePipeline()
42 {
43     if( mpPipeline != nullptr )
44     {
45         gst_element_set_state( mpPipeline, GST_STATE_NULL );
46         g_object_unref( G_OBJECT( mpPipeline ) );
47         mpPipeline = nullptr;
48     }
49 }
50 
FrameGrabber(const OUString & rURL)51 FrameGrabber::FrameGrabber( const OUString &rURL ) :
52     FrameGrabber_BASE()
53 {
54     gchar *pPipelineStr;
55     pPipelineStr = g_strdup_printf(
56         "uridecodebin uri=%s ! videoconvert ! videoscale ! appsink "
57         "name=sink caps=\"video/x-raw,format=RGB,pixel-aspect-ratio=1/1\"",
58         OUStringToOString( rURL, RTL_TEXTENCODING_UTF8 ).getStr() );
59 
60     GError *pError = nullptr;
61     mpPipeline = gst_parse_launch( pPipelineStr, &pError );
62     if( pError != nullptr) {
63         g_warning( "Failed to construct frame-grabber pipeline '%s'\n", pError->message );
64         g_error_free( pError );
65         disposePipeline();
66     }
67 
68     if( mpPipeline ) {
69         // pre-roll
70         switch( gst_element_set_state( mpPipeline, GST_STATE_PAUSED ) ) {
71         case GST_STATE_CHANGE_FAILURE:
72         case GST_STATE_CHANGE_NO_PREROLL:
73             g_warning( "failure pre-rolling media" );
74             disposePipeline();
75             break;
76         default:
77             break;
78         }
79     }
80     if( mpPipeline &&
81         gst_element_get_state( mpPipeline, nullptr, nullptr, 5 * GST_SECOND ) == GST_STATE_CHANGE_FAILURE )
82         disposePipeline();
83 }
84 
~FrameGrabber()85 FrameGrabber::~FrameGrabber()
86 {
87     disposePipeline();
88 }
89 
create(const OUString & rURL)90 FrameGrabber* FrameGrabber::create( const OUString &rURL )
91 {
92     return new FrameGrabber( rURL );
93 }
94 
grabFrame(double fMediaTime)95 uno::Reference< graphic::XGraphic > SAL_CALL FrameGrabber::grabFrame( double fMediaTime )
96 {
97     uno::Reference< graphic::XGraphic > xRet;
98 
99     if( !mpPipeline )
100         return xRet;
101 
102     gint64 gst_position = llround( fMediaTime * GST_SECOND );
103     gst_element_seek_simple(
104         mpPipeline, GST_FORMAT_TIME,
105         GstSeekFlags(GST_SEEK_FLAG_KEY_UNIT | GST_SEEK_FLAG_FLUSH),
106         gst_position );
107 
108     GstElement *pSink = gst_bin_get_by_name( GST_BIN( mpPipeline ), "sink" );
109     if( !pSink )
110         return xRet;
111 
112     GstBuffer *pBuf = nullptr;
113     GstCaps *pCaps = nullptr;
114 
115     // synchronously fetch the frame
116     GstSample *pSample = nullptr;
117     g_signal_emit_by_name( pSink, "pull-preroll", &pSample, nullptr );
118 
119     if( pSample )
120     {
121         pBuf = gst_sample_get_buffer( pSample );
122         pCaps = gst_sample_get_caps( pSample );
123     }
124 
125     // get geometry
126     int nWidth = 0, nHeight = 0;
127     if( !pCaps )
128         g_warning( "could not get snapshot format\n" );
129     else
130     {
131         GstStructure *pStruct = gst_caps_get_structure( pCaps, 0 );
132 
133         /* we need to get the final caps on the buffer to get the size */
134         if( !gst_structure_get_int( pStruct, "width", &nWidth ) ||
135             !gst_structure_get_int( pStruct, "height", &nHeight ) )
136             nWidth = nHeight = 0;
137     }
138 
139     if( pBuf && nWidth > 0 && nHeight > 0 &&
140         // sanity check the size
141         gst_buffer_get_size( pBuf ) >= static_cast<unsigned>( nWidth * nHeight * 3 )
142         )
143     {
144         sal_uInt8 *pData = nullptr;
145         GstMapInfo aMapInfo;
146         gst_buffer_map( pBuf, &aMapInfo, GST_MAP_READ );
147         pData = aMapInfo.data;
148 
149         int nStride = GST_ROUND_UP_4( nWidth * 3 );
150         BitmapEx aBmp = vcl::bitmap::CreateFromData(pData, nWidth, nHeight, nStride, 24 );
151 
152         gst_buffer_unmap( pBuf, &aMapInfo );
153         xRet = Graphic( aBmp ).GetXGraphic();
154     }
155 
156     return xRet;
157 }
158 
getImplementationName()159 OUString SAL_CALL FrameGrabber::getImplementationName(  )
160 {
161     return AVMEDIA_GST_FRAMEGRABBER_IMPLEMENTATIONNAME;
162 }
163 
supportsService(const OUString & ServiceName)164 sal_Bool SAL_CALL FrameGrabber::supportsService( const OUString& ServiceName )
165 {
166     return cppu::supportsService(this, ServiceName);
167 }
168 
getSupportedServiceNames()169 uno::Sequence< OUString > SAL_CALL FrameGrabber::getSupportedServiceNames()
170 {
171     return { AVMEDIA_GST_FRAMEGRABBER_SERVICENAME };
172 }
173 
174 } // namespace gstreamer
175 } // namespace avmedia
176 
177 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
178