Commit 86cf665a authored by Thomas Perl's avatar Thomas Perl

[audioresource] Initial commit

parents
cmake_minimum_required(VERSION 2.8)
project(libaudioresource)
# Shared library version
set(MAJOR_VERSION "1")
set(MINOR_VERSION "0")
set(PATCH_VERSION "0")
set(VERSION "${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_VERSION}")
set(VERSION_SONAME "${MAJOR_VERSION}")
# Implementation backend dependencies: libresource
find_package(PkgConfig)
pkg_check_modules(LIBRESOURCE_GLIB REQUIRED libresource-glib)
include_directories(${LIBRESOURCE_GLIB_INCLUDE_DIRS})
# Multiarch support
if(NOT LIB_DEST)
if(CMAKE_LIBRARY_ARCHITECTURE)
set(LIB_DEST "lib/${CMAKE_LIBRARY_ARCHITECTURE}")
else()
if (${CMAKE_C_SIZEOF_DATA_PTR} EQUAL 8)
set(LIB_DEST "lib64")
else()
set(LIB_DEST "lib")
endif()
endif()
endif()
# pkg-config support
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/pkgconfig/audioresource.pc.in
${CMAKE_CURRENT_BINARY_DIR}/audioresource.pc
@ONLY)
# Shared library
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
add_library(audioresource SHARED src/audioresource.c)
target_link_libraries(audioresource ${LIBRESOURCE_GLIB_LIBRARIES})
set_target_properties(audioresource PROPERTIES VERSION ${VERSION} SOVERSION ${VERSION_SONAME})
# Installation
install(TARGETS audioresource LIBRARY DESTINATION ${LIB_DEST})
install(FILES include/audioresource.h DESTINATION include/audioresource)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/audioresource.pc DESTINATION ${LIB_DEST}/pkgconfig)
This diff is collapsed.
Nemo Mobile Audio Resource API
==============================
This library provides a way to acquire audio resources for playback on Nemo
Mobile and Sailfish, as well as a way to get notified when audio resources
have been released, in which case audio playback must be stopped.
Audio resources for your application might be released in case of an incoming
call, alarm sound, other applications playing music, etc.. - by only playing
back audio when the resource is acquired, an application developer can make
sure that the application is well-behaving, and also react to these events in
the user interface (e.g. by simulating a "pause" press in a player UI).
For actually playing back audio, use libpulse or libpulse-simple.
Third party applications that expect a stable API/ABI for resource management
should use this library - lower-level libraries such as libresource (which this
library currently uses as its backend) are considered an implementation detail
and could be replaced or removed in the future.
Example Usage
-------------
#include <audioresource.h>
void on_acquired(audioresource_t *audio_resource, bool acquired, void *user_data)
{
if (acquired) {
// start playback here
} else {
// stop playback here
}
}
int main(int argc, char *argv[])
{
audioresource_t *resource;
// You can pass in a application-specific pointer that will be
// provided as "user_data" to the on_acquired() callback above
void *user_data = NULL;
// Initialize the resource for a game
resource = audioresource_init(AUDIO_RESOURCE_GAME, on_acquired, user_data);
// When you want to start playback
audioresource_acquire(resource);
while (true) {
// game loop, etc..
}
// When you want to stop playback
audioresource_release(resource);
// When you close your application
audioresource_free(resource);
}
/**
*
* Copyright (C) 2013 Jolla Ltd.
* Contact: Thomas Perl <thomas.perl@jolla.com>
* All rights reserved.
*
* This file is part of libaudioresource
*
* You may use this file under the terms of the GNU Lesser General
* Public License version 2.1 as published by the Free Software Foundation
* and appearing in the file COPYING.LGPL included in the packaging
* of this file.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* and appearing in the file license.lgpl included in the packaging
* of this file.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
**/
#ifndef LIBAUDIORESOURCE_AUDIORESOURCE_H
#define LIBAUDIORESOURCE_AUDIORESOURCE_H
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
// Opaque pointer to our audio resource
typedef struct audioresource_t audioresource_t;
/**
* Callback that needs to be passed to audioresource_init.
*
* This callback will be called every time the audio resource
* is acquired or released (both can happen due to API requests
* or because of system-initiated policy changes).
*
* The application can start playing audio as soon as this callback
* is called with acquired=true, and must stop playing audio as soon
* as this callback is called with acquired=false.
*
* The user_data parameter is the one passed to audioresource_init().
**/
typedef void (*audioresource_acquired_callback_t)(audioresource_t *audio_resource,
bool acquired, void *user_data);
/**
* Type of audio resource - for games, use AUDIO_RESOURCE_GAME, for music
* and audio players, use AUDIO_RESOURCE_MEDIA.
**/
enum audioresource_type_t {
AUDIO_RESOURCE_INVALID = 0,
AUDIO_RESOURCE_GAME = 1,
AUDIO_RESOURCE_MEDIA = 2,
};
/**
* Initialize the audio resource library, registering a callback that
* will be called every time the acquired state changes. The user_data
* parameter can be NULL. It will be passed to the callback on ever call.
**/
audioresource_t *audioresource_init(enum audioresource_type_t type,
audioresource_acquired_callback_t acquired_cb,
void *user_data);
/**
* Request to acquire the audio resource. If the resource was successfully
* acquired, the callback passed to audioresource_init() will be called.
*
* If the request was not successful, or if the resource was already
* acquired, the call will do nothing.
**/
void audioresource_acquire(audioresource_t *audio_resource);
/**
* Request to release the audio resource. This function should be called
* whenever the application stops playing back audio (e.g. when it is put
* into the background).
*
* If the request was not successful, or if the resource was not acquired,
* the call will do nothing.
**/
void audioresource_release(audioresource_t *audio_resource);
/**
* Close the audio resource library and release resources / free memory.
**/
void audioresource_free(audioresource_t *audio_resource);
#ifdef __cplusplus
};
#endif
#endif /* LIBAUDIORESOURCE_AUDIORESOURCE_H */
prefix=@CMAKE_INSTALL_PREFIX@
exec_prefix=${prefix}
libdir=${prefix}/@LIB_DEST@
includedir=${prefix}/include/audioresource
Name: audioresource
Description: Nemo Mobile Audio Resource API
URL: http://github.com/nemomobile/
Version: @VERSION@
Libs: -L${libdir} -laudioresource
Cflags: -I${includedir}
Name: libaudioresource
Version: 1.0.0
Release: 1
Summary: Nemo Mobile Audio Resource API
Group: Development/Libraries
License: LGPL
URL: http://github.com/nemomobile
Source: %{name}-%{version}.tar.bz2
BuildRequires: pkgconfig(libresource-glib)
Requires(post): /sbin/ldconfig
Requires(postun): /sbin/ldconfig
%description
This library provides a way to acquire audio resources for playback on Nemo
Mobile and Sailfish, as well as a way to get notified when audio resources
have been released, in which case audio playback must be stopped.
%package devel
Summary: Development library for %{name}
Requires: %{name} = %{version}
%description devel
This package contains the development library for %{name}.
%prep
%setup -q
%build
%cmake
make
%install
%make_install
%post -p /sbin/ldconfig
%postun -p /sbin/ldconfig
%files
%defattr(-,root,root,-)
%{_libdir}/%{name}.so.*
%files devel
%defattr(-,root,root,-)
%doc README COPYING.LGPL
%{_libdir}/%{name}.so
%{_libdir}/pkgconfig/audioresource.pc
%{_includedir}/audioresource/audioresource.h
/**
*
* Copyright (C) 2013 Jolla Ltd.
* Contact: Thomas Perl <thomas.perl@jolla.com>
* All rights reserved.
*
* This file is part of libaudioresource
*
* You may use this file under the terms of the GNU Lesser General
* Public License version 2.1 as published by the Free Software Foundation
* and appearing in the file COPYING.LGPL included in the packaging
* of this file.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* and appearing in the file license.lgpl included in the packaging
* of this file.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
**/
#include "audioresource.h"
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <resource.h>
struct audioresource_t {
enum audioresource_type_t type;
audioresource_acquired_callback_t acquired_callback;
void *user_data;
bool acquired;
resource_set_t *resource_set;
};
static void grant_callback(resource_set_t *resource_set, uint32_t resources, void *userdata) {
audioresource_t *audio_resource = (audioresource_t *)userdata;
bool acquired = ((resources & RESOURCE_AUDIO_PLAYBACK) != 0);
if (audio_resource->acquired != acquired) {
audio_resource->acquired_callback(audio_resource, acquired, audio_resource->user_data);
audio_resource->acquired = acquired;
}
}
static void advice_callback(resource_set_t *resource_set, uint32_t resources, void *userdata) {
// TODO
}
audioresource_t *audioresource_init(enum audioresource_type_t type,
audioresource_acquired_callback_t acquired_cb,
void *user_data)
{
const char *type_str;
switch (type) {
case AUDIO_RESOURCE_GAME:
type_str = "game";
break;
case AUDIO_RESOURCE_MEDIA:
type_str = "media";
break;
default:
/* Invalid audio resource type */
return NULL;
}
audioresource_t *audio_resource =
(audioresource_t *)malloc(sizeof(audioresource_t));
audio_resource->type = type;
audio_resource->acquired_callback = acquired_cb;
audio_resource->user_data = user_data;
audio_resource->acquired = false;
audio_resource->resource_set = resource_set_create(type_str,
RESOURCE_AUDIO_PLAYBACK, 0, 0, grant_callback,
audio_resource);
resource_set_configure_advice_callback(audio_resource->resource_set,
advice_callback, audio_resource);
resource_set_configure_audio(audio_resource->resource_set, type_str,
getpid(), "*");
return audio_resource;
}
void audioresource_acquire(audioresource_t *audio_resource)
{
resource_set_acquire(audio_resource->resource_set);
}
void audioresource_release(audioresource_t *audio_resource)
{
resource_set_release(audio_resource->resource_set);
}
void audioresource_free(audioresource_t *audio_resource)
{
resource_set_destroy(audio_resource->resource_set);
free(audio_resource);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment