1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "framegrabber.hxx"
23 #include <tools/stream.hxx>
24 #include <vcl/graph.hxx>
25 #include <vcl/cvtgrf.hxx>
26 #include <unotools/localfilehelper.hxx>
28 using namespace ::com::sun::star;
30 namespace avmedia { namespace macavf {
36 FrameGrabber::FrameGrabber( const uno::Reference< lang::XMultiServiceFactory >& /*rxMgr*/ )
40 // ------------------------------------------------------------------------------
42 FrameGrabber::~FrameGrabber()
45 CFRelease( mpImageGen );
48 // ------------------------------------------------------------------------------
50 bool FrameGrabber::create( const ::rtl::OUString& rURL )
52 NSString* pNSStr = [NSString stringWithCharacters:rURL.getStr() length:rURL.getLength()];
53 NSURL* pNSURL = [NSURL URLWithString: [pNSStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
54 AVAsset* pMovie = [AVURLAsset URLAssetWithURL:pNSURL options:nil];
57 OSL_TRACE( "AVGrabber::create() cannot load url=\"%s\"", [pNSStr UTF8String] );
61 return create( pMovie );
64 // ------------------------------------------------------------------------------
66 bool FrameGrabber::create( AVAsset* pMovie )
68 if( [[pMovie tracksWithMediaType:AVMediaTypeVideo] count] == 0)
70 OSL_TRACE( "AVGrabber::create() found no video content!" );
74 mpImageGen = [AVAssetImageGenerator assetImageGeneratorWithAsset:pMovie];
75 CFRetain( mpImageGen );
79 // ------------------------------------------------------------------------------
81 uno::Reference< graphic::XGraphic > SAL_CALL FrameGrabber::grabFrame( double fMediaTime )
82 throw (uno::RuntimeException)
84 uno::Reference< graphic::XGraphic > xRet;
87 OSL_TRACE( "AVPlayer::grabFrame( %.3fsec)", fMediaTime );
89 // get the requested image from the movie
90 CGImage* pCGImage = [mpImageGen copyCGImageAtTime:CMTimeMakeWithSeconds(fMediaTime,1000) actualTime:NULL error:NULL];
92 // convert the image to a TIFF-formatted byte-array
93 CFMutableDataRef pCFData = CFDataCreateMutable( kCFAllocatorDefault, 0 );
94 CGImageDestination* pCGImgDest = CGImageDestinationCreateWithData( pCFData, kUTTypeTIFF, 1, 0 );
95 CGImageDestinationAddImage( pCGImgDest, pCGImage, NULL );
96 CGImageDestinationFinalize( pCGImgDest );
97 CFRelease( pCGImgDest );
98 const long nBitmapLen = CFDataGetLength( pCFData );
99 void* pBitmapBytes = (void*)CFDataGetBytePtr( pCFData );
101 // convert the image into the return-value type which is a graphic::XGraphic
102 SvMemoryStream aMemStm( pBitmapBytes, nBitmapLen, StreamMode::READ | StreamMode::WRITE );
104 if( GraphicConverter::Import( aMemStm, aGraphic, CVT_TIF ) == ERRCODE_NONE )
105 xRet = aGraphic.GetXGraphic();
107 // clean up resources
108 CFRelease( pCFData );
112 // ------------------------------------------------------------------------------
114 ::rtl::OUString SAL_CALL FrameGrabber::getImplementationName( )
115 throw (uno::RuntimeException)
117 return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( AVMEDIA_MACAVF_FRAMEGRABBER_IMPLEMENTATIONNAME ) );
120 // ------------------------------------------------------------------------------
122 sal_Bool SAL_CALL FrameGrabber::supportsService( const ::rtl::OUString& ServiceName )
123 throw (uno::RuntimeException)
125 return ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( AVMEDIA_MACAVF_FRAMEGRABBER_SERVICENAME ) );
128 // ------------------------------------------------------------------------------
130 uno::Sequence< ::rtl::OUString > SAL_CALL FrameGrabber::getSupportedServiceNames( )
131 throw (uno::RuntimeException)
133 uno::Sequence< ::rtl::OUString > aRet(1);
134 aRet[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( AVMEDIA_MACAVF_FRAMEGRABBER_SERVICENAME ) );
139 } // namespace macavf
140 } // namespace avmedia
142 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */