Design a quick thumbnail service for Ubuntu

Source: Internet
Author: User
Tags ubuntu touch gstreamer
Recently, JamesHenstridge, XaviGarciaMena, and MichiHenning have implemented a fast and scalable thumbnail service for Ubuntu and Ubuntu touch. Introduction The thumbnails service is required for mobile phones and desktop applications in many scenarios. At the same time, there are many media types that need to generate thumbnails, such as slices, music, and videos. Designing independent APIs for each media type increases development costs, and generating these thumbnails also consumes a lot of CPU

Recently, James Henstridge, Xavi Garcia Mena, and Michi Henning have implemented a fast and scalable thumbnail service for Ubuntu and Ubuntu Touch.

Introduction

Thumbnails are required for mobile phones and desktop applications in many scenarios. At the same time, there are many media types that need to generate thumbnails, such as slices, music, and videos. Designing independent APIs for each media type increases development costs, and generating these thumbnails also consumes a lot of CPU resources, and network transmission thumbnails consume a lot of bandwidth. This article mainly introduces a common thumbnail service, which shields developers from these complex content and greatly improves the performance of the thumbnail service through caching and other measures.

System Architecture

External API

The thumbnail Service provides three APIs.

  • Qml api: By registering as a provider of QQuickAsyncImageProvider, the caller can pass in a specific URI and parameter to obtain local or remote thumbnail files of a specific size.
  • Qt API: provides three functions to obtain thumbnails of specific types.

  • QSharedPointer getThumbnail (QString const & filePath, QSize const & requestedSize );
  • QSharedPointer getAlbumArt (QString const & artist, QString const & album, QSize const & requestedSize );
  • QSharedPointer getArtistArt (QString const & artist, QString const & album, QSize const & requestedSize );

The getThumbnail function extracts thumbnails from the local media file. The getAlbumArt and getArtistArt functions are obtained from the remote image server. The Request objects returned by these functions provide downloadFinished signals, which can be connected by callers to obtain thumbnail data asynchronously.

  • Using API: the thumbnail service registers two interfaces through callback: com. canonical. ThumbnailerAdmin and com. canonical. Thumbnailer. The former provides cache operations and cache status queries, and the latter provides GetAlbumArt, GetArtistArt, and GetThumbnail functions, corresponding to the three functions in Qt API, to obtain different types of thumbnails.

The thumbnails Service also provides command line tools.Thumbnailer-adminYes, you must be able to operate the above two interfaces through the command line.

To save resources, the ingress interface is started by the ingress service and closed after 30 seconds.

Image extraction module

The image extraction module includes the audio, video extract, download, and image extract in the image.

The audio and video extractors use GStreamer to decode audio and video files. Due to the stability of GStreamer, some decoder may cause the program to suspend and lose the response. Therefore, the interaction part with GStreamer is encapsulated into an independent executable program.Vs-thumb. The main service interacts with it in the form of a pipeline, which effectively avoids stability problems caused by decoder crash.

The downloader provides two asynchronous functions: download_album and download_artist. The QNetworkAccessManager component of Qt downloads images from dash.ubuntu.com.

The image Extraction Tool uses the image conversion module to extract thumbnail images from local images.

Image Scaling and conversion module

The image scaling and conversion module converts and scales an image to a final image file in JPEG format. This module uses the Gdk-Pixbuf library for conversion.

For JPEG images, the image scaling module tries to read the image's EXIF information through the libexif library. If the EXIF information of an image contains a thumbnail and the size of the thumbnail is not smaller than the target, the image scaling module scales the image using the thumbnail in EXIF to improve performance.

Disk cache Module

The disk cache includes three parts: Full-size image cache, thumbnail cache, and failure cache.

  • Full-size cache: stores images obtained from remote image servers and images extracted from audio and video files. Because the acquisition cost of images from these sources is relatively high, these images are saved in the original size. By default, the cache size is 50 MB, which is replaced by the latest minimum usage. You can modify data/com. canonical. unity. thumbnailer. gschema. the full-size-cache-size node data in the xml file to reset the cache size.
  • Thumbnail cache: Save As a thumbnail of the specified size generated by the caller. The default cache size is 100 MB. You can modify the data of the thumbnail-cache-size node to reset the cache size of the thumbnail.
  • Failed cache: stores items that fail to be extracted due to exceptions. Remote files may be obtained because they do not exist or fail to be authorized. Local files may be damaged or audio files do not contain illustrations. The failed cache is mainly used to reduce repeated attempts to known errors. For the content in the cache, the latest minimum usage and the specified expiration time are used to control the cache failure.

Because three caches are used, the process of searching for a thumbnail of the specified size is roughly as follows:

  1. Check whether an image of the specified size already exists in the thumbnail cache. If yes, return directly.
  2. Check whether the full-size copy of the image exists in the full-size cache. If yes, use a full-size image for scaling. After adding the scaled thumbnail to the thumbnail cache, return.
  3. Check whether there are corresponding items in the failed cache. If yes, an error is returned directly.
  4. Try to remotely download or extract thumbnails from the original file. If it fails, it is added to the failed cache and an error is returned.
  5. If the source file is downloaded remotely or extracted from audio or video streams, add the source file to the full-size cache.
  6. Scales the image file to a specified size, adds it to the thumbnail cache, and returns the result.

Performance Improvement

The thumbnail service is mainly used for network-based download and CPU-based image extraction. Therefore, the performance improvement mainly involves network I/O and CPU utilization. To prevent these two time-consuming operations from blocking other requests, especially requests that can be quickly responded from the cache module, downloads and image extraction are placed in an independent event loop, uses the Qt signal and slot mechanism to asynchronously process time-consuming requests.

For cache testing, with machines configured with an Intel Ivy Bridge i7-3770k 3.5 GHz processor and 256 gb ssd, the test data uses a 60-byte string as the key, use random binary data with an average size of 20 KB as the value, and the cache size is 100 MB.

The cache write time is about 2.8 seconds, and the test scenario is 80% cache hit rate. When the cache is not hit, new data is inserted into the cache, trigger the cache to replace data according to the latest minimum usage mode. In 0.1 million cycles, the cache returns about 4800 "snapshots" per second, and the aggregate read/write throughput is 93 MB per second. If the cache hit rate is increased to 90%, the number of records returned per second is nearly doubled, reaching 7100 records per second.

Summary

Each module in the thumbnail service has a clear interface definition. It is very easy to support new media types or extend the remote image service without affecting the existing code.

To make full use of hardware resources, the thumbnails service uses multithreading for long-time asynchronous interfaces to improve the system I/O utilization.

The thumbnail Service is currently used on the Ubuntu Touch system to provide the thumbnail service for image libraries, cameras, music, and other applications that use media thumbnails.

For more information about Ubuntu, see Ubuntu special page http://www.linuxidc.com/topicnews.aspx? Tid = 2

This article permanently updates the link address: Http://www.linuxidc.com/Linux/2015-08/122041.htm

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.