tv-service  0.1.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
tv_service_live_overlay_vol_test.c

This is an example of how to use the TV Service's audio volume related APIs.

Author
Jae-Il Jung ji013.nosp@m.0.ju.nosp@m.ng@sa.nosp@m.msun.nosp@m.g.com
/* Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* @author Jae-Il Jung <ji0130.jung@samsung.com>
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <time.h>
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include "tv_service_logger.h"
#define VOL_MIN 0
#define VOL_MAX 10
static GMainLoop *loop = NULL;
static TvServiceLive live_handle = NULL;
guintptr window;
Display *dpy = NULL;
guint dst_x = 0;
guint dst_y = 0;
guint dst_width = 1920;
guint dst_height = 1080;
gdouble volume = 0.0;
void
print_usage (char * argv)
{
printf ("Volume API test \n");
printf ("%s {frequency} {modulation mode} {APID} {VPID} {PPID} {vol_up_num} {vol_down_num} \n", argv);
printf ("i.e) %s 485000000 7 20 17 16 5 10\n", argv);
return;
}
gboolean
exit_loop (gpointer user_data)
{
gint rc = tv_service_live_set_volume (live_handle, volume);
if (rc != TVS_ERROR_OK) {
printf ("failed to set volume");
}
g_main_loop_quit (loop);
return FALSE;
}
static XImage *
make_transparent_image (Display * dpy, int width, int height)
{
XImage *xim = NULL;
xim = XCreateImage (dpy, DefaultVisualOfScreen (DefaultScreenOfDisplay (dpy)),
24, ZPixmap, 0, NULL, width, height, 32, 0);
xim->data = (char*) calloc(1, xim->bytes_per_line * xim->height);
return xim;
}
gboolean
vol_down (gpointer user_data)
{
gint res;
gdouble vol;
res = tv_service_live_get_volume (live_handle, &vol);
if (vol < VOL_MIN || vol > VOL_MAX) {
printf ("failed to get volume");
return FALSE;
}
vol -= 1.0;
if (vol < VOL_MIN) {
return FALSE;
}
printf ("volume down %.0f\n", vol);
res = tv_service_live_set_volume (live_handle, vol);
if (res < 0) {
printf ("failed to set volume");
return FALSE;
}
return FALSE;
}
gboolean
vol_up (gpointer user_data)
{
gint res;
gdouble vol;
res = tv_service_live_get_volume (live_handle, &vol);
if (vol < VOL_MIN || vol > VOL_MAX) {
printf ("failed to get volume");
return FALSE;
}
vol += 1.0;
if (vol > VOL_MAX) {
return FALSE;
}
printf ("volume up %.0f\n", vol);
res = tv_service_live_set_volume (live_handle, vol);
if (res < 0) {
printf ("failed to set volume");
return FALSE;
}
return FALSE;
}
gboolean
audio_mute (gpointer user_data)
{
gint res;
printf ("audio mute\n");
res = tv_service_live_set_audio_mute (live_handle, TRUE);
if (res < 0) {
printf ("failed to mute");
return FALSE;
}
return TRUE;
}
gboolean
audio_unmute (gpointer user_data)
{
gint res;
printf ("audio unmute\n");
res = tv_service_live_set_audio_mute (live_handle, FALSE);
if (res < 0) {
printf ("failed to unmute");
return FALSE;
}
return TRUE;
}
static Window
createWindow (Display * dpy)
{
Window window_id;
GC gc;
XEvent event;
XSizeHints my_hints = {0};
XSetWindowAttributes attr;
attr.override_redirect = TRUE;
window_id = XCreateWindow (dpy, RootWindow(dpy, 0), dst_x, dst_y, dst_width, dst_height, 0,
CopyFromParent, InputOutput, CopyFromParent, CWOverrideRedirect, &attr);
printf ("create_window - XCreateWindow, window id[%ld]", window_id);
my_hints.flags = PPosition | PSize;
my_hints.x = dst_x;
my_hints.y = dst_y;
my_hints.width = dst_width;
my_hints.height = dst_height;
XSetNormalHints (dpy, window_id, &my_hints);
XSelectInput (dpy, window_id, ExposureMask);
printf ("create_window - XSelectInput");
XMapWindow (dpy, window_id);
printf ("create_window - XMapWindow");
gc = XCreateGC (dpy, window_id, 0, 0);
printf ("create_window - XCreateGC");
XSetForeground (dpy, gc, 0xffffff);
printf ("create_window - XSetForeground");
XNextEvent (dpy, &event);
if (event.type == Expose) {
XFillRectangle (dpy, window_id, gc, dst_x, dst_y, dst_width, dst_height);
}
XFlush (dpy);
XSync (dpy, 0);
XFreeGC (dpy, gc);
printf ("create_window - XSync");
return window_id;
}
void
signal_handle (int signum)
{
printf ("In signal handle!\n");
if (live_handle) {
tv_service_live_destroy (live_handle);
}
g_main_loop_quit (loop);
XDestroyWindow (dpy, window);
XCloseDisplay (dpy);
exit (0);
return;
}
static void
app_callback (TvServiceLiveEvent event, gpointer user_data, const gpointer data)
{
switch (event) {
{
gboolean *lock_status = (gboolean *)data;
printf ("tv_service_callback: get tuner locked callback:%d\n", *lock_status);
}
break;
printf ("event channel lock\n");
break;
printf ("event channel unlock\n");
break;
printf ("event auto destroy\n");
live_handle = NULL;
exit_loop (NULL);
break;
default:
printf ("get callback_type: %d\n", event);
}
return;
}
int
main (int argc, char ** argv)
{
gint i;
gint rc = TVS_ERROR_OK;
guint freq;
guint mod;
guint apid;
guint vpid;
guint ppid;
guint vol_up_num;
guint vol_down_num;
guint total_time = 5;
GC gc;
XImage *xim = NULL;
if (argc == 8) {
freq = atoi (argv[1]);
mod = atoi (argv[2]);
apid = atoi (argv[3]);
vpid = atoi (argv[4]);
ppid = atoi (argv[5]);
vol_up_num = atoi (argv[6]);
vol_down_num = atoi (argv[7]);
} else {
print_usage (argv[0]);
return -1;
}
dpy = XOpenDisplay (NULL);
window = createWindow (dpy);
gc = XCreateGC (dpy, window, 0, 0);
xim = make_transparent_image (dpy, dst_width, dst_height);
XPutImage (dpy, window, gc, xim, 0, 0, dst_x, dst_y, dst_width, dst_height);
XSync (dpy, False);
loop = g_main_loop_new (NULL, FALSE);
rc = tv_service_live_create (&live_handle);
if (rc != TVS_ERROR_OK) {
return rc;
}
signal (SIGINT,signal_handle);
rc = tv_service_live_get_volume (live_handle, &volume);
if (rc != TVS_ERROR_OK) {
printf ("failed to get volume");
printf ("volume = %f\n",volume);
return rc;
}
rc = tv_service_live_set_window_overlay (live_handle, (gpointer)window);
if (rc != TVS_ERROR_OK) {
return rc;
}
for (i = 1; i <= vol_up_num; i++) {
total_time += i;
g_timeout_add_seconds (total_time, vol_up, live_handle);
}
total_time += 2;
g_timeout_add_seconds (total_time, audio_mute, live_handle);
total_time += 2;
g_timeout_add_seconds (total_time, audio_unmute, live_handle);
for (i = 1; i <= vol_down_num; i++) {
total_time += i;
g_timeout_add_seconds (total_time, vol_down, live_handle);
}
tv_service_live_register_callback (live_handle, app_callback, NULL);
tv_service_live_tune_full (live_handle, freq, mod, apid, vpid, ppid);
g_timeout_add_seconds (total_time + 10, exit_loop, NULL);
g_main_loop_run (loop);
if (live_handle) {
tv_service_live_destroy (live_handle);
}
XFreeGC (dpy, gc);
XDestroyWindow (dpy, window);
XCloseDisplay (dpy);
return 0;
}