File:  [DVB] / libsoftmpeg / demo / dfb_ts.c
Revision 1.12: download - view: text, annotated - select for diffs
Wed Mar 17 15:15:25 2004 UTC (20 years, 2 months ago) by hunold
Branches: MAIN
CVS tags: HEAD
- if init->layer_id == -1, use DLID_PRIMARY

/*
   (c) Copyright 2004  convergence GmbH

   All rights reserved.

   Written by Michael Hunold <hunold@convergence.de>,
              Andreas Hundt <andi@fischlustig.de> and
              Denis Oliver Kropp <dok@directfb.org>

   This file is subject to the terms and conditions of the MIT License:

   Permission is hereby granted, free of charge, to any person
   obtaining a copy of this software and associated documentation
   files (the "Software"), to deal in the Software without restriction,
   including without limitation the rights to use, copy, modify, merge,
   publish, distribute, sublicense, and/or sell copies of the Software,
   and to permit persons to whom the Software is furnished to do so,
   subject to the following conditions:

   The above copyright notice and this permission notice shall be
   included in all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
   IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
   CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
   TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
   SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <time.h>

#include <directfb.h>
#include "softmpeg.h"

static IDirectFB             *dfb          = NULL;
static IDirectFBDisplayLayer *primarylayer = NULL;
static IDirectFBWindow       *window       = NULL;
static IDirectFBEventBuffer  *keybuffer    = NULL;

static DFBWindowID       windowid = -1;
static DFBDisplayLayerID layerid  = -1;

static enum softmpeg_mode mode = SOFTMPEG_PLAY;

#define DFBCHECK(x...)   \
do {		     \
    DFBResult err = x;   \
			 \
	if (err != DFB_OK) { \
		fprintf( stderr, "%s <%d>:\n\t", __FILE__, __LINE__ );   \
		DirectFBErrorFatal( #x, err );			   \
	}							    \
} while (0)

static DFBEnumerationResult
enum_layers_callback(unsigned int id, DFBDisplayLayerDescription desc, void *data)
{
     printf("\nLayer %d:\n", id);

     if (desc.caps & DLCAPS_SURFACE)
          printf("  - Has a surface.\n");

     if (desc.caps & DLCAPS_ALPHACHANNEL)
          printf("  - Supports blending based on alpha channel.\n");

     if (desc.caps & DLCAPS_SRC_COLORKEY)
          printf("  - Supports source color keying.\n");

     if (desc.caps & DLCAPS_DST_COLORKEY)
          printf("  - Supports destination color keying.\n");

     if (desc.caps & DLCAPS_FLICKER_FILTERING)
          printf("  - Supports flicker filtering.\n");

     if (desc.caps & DLCAPS_DEINTERLACING)
          printf("  - Can deinterlace interlaced video for progressive display.  \n");

     if (desc.caps & DLCAPS_OPACITY)
          printf("  - Supports blending based on global alpha factor.\n");

     if (desc.caps & DLCAPS_SCREEN_LOCATION)
          printf("  - Can be positioned on the screen.\n");

     if (desc.caps & DLCAPS_BRIGHTNESS)
          printf("  - Brightness can be adjusted.\n");

     if (desc.caps & DLCAPS_CONTRAST)
          printf("  - Contrast can be adjusted.\n");

     if (desc.caps & DLCAPS_HUE)
          printf("  - Hue can be adjusted.\n");

     if (desc.caps & DLCAPS_SATURATION)
          printf("  - Saturation can be adjusted.\n");

     printf("\n");

     /* We take the first video layer not being the primary */
     if (id != DLID_PRIMARY && desc.caps & DLCAPS_SURFACE && desc.type & DLTF_VIDEO) {
          layerid = id;
          return DFENUM_CANCEL;
     }

     return DFENUM_OK;
}

static int
dfb_init(int *argc, char ***argv)
{
     DFBCHECK(DirectFBInit(argc, argv));
     DFBCHECK(DirectFBSetOption("no-cursor", NULL));
     DFBCHECK(DirectFBCreate(&dfb));
     DFBCHECK(dfb->GetDisplayLayer(dfb, DLID_PRIMARY, &primarylayer));
     DFBCHECK(dfb->EnumDisplayLayers(dfb, enum_layers_callback, NULL));

     if (layerid == -1) {
          DFBWindowDescription windowdesc;

          printf("could not find suitable videolayer - using window on primary layer\n");

          windowdesc.flags  = DWDESC_HEIGHT | DWDESC_WIDTH;
          windowdesc.height = 720;
          windowdesc.width  = 576;

          DFBCHECK(primarylayer->CreateWindow(primarylayer, &windowdesc, &window));
          window->GetID(window, &windowid);

          window->SetOpacity(window, 0xff);
     }

     /* create an input buffer for key events */
     DFBCHECK(dfb->CreateInputEventBuffer(dfb, DICAPS_KEYS, DFB_TRUE, &keybuffer));

     return 0;
}

static void
dfb_destroy(void)
{
     if (window)
          window->Release(window);

     if (keybuffer)
          keybuffer->Release(keybuffer);

     if (primarylayer)
          primarylayer->Release(primarylayer);

     if (dfb)
          dfb->Release(dfb);
}

int
main(int argc, char **argv)
{
     struct softmpeg_directfb_initdata init;
     
     unsigned int apid, vpid, pcrpid;
     struct softmpeg_decoder *d;
     int dmx_fd;
     int quit = 0;

     if (argc != 4 && argc != 5) {
          fprintf(stderr, "\n\tusage: %s <ts_source> <vpid> <apid> [pcr_pid]\n\n", argv[0]);
          return -1;
     }

     dfb_init(&argc, &argv);

     init.window_id = windowid;
     init.layer_id = layerid;
     init.width = 720;
     init.height = 576;

     vpid = strtol(argv[2], NULL, 0);
     apid = strtol(argv[3], NULL, 0);

     if (argc == 5)
          pcrpid = strtol(argv[4], NULL, 0);
     else {
          pcrpid = vpid;
          fprintf(stderr, "warning: no PCR PID given, assuming PCR PID == vpid == 0x%04x.\n", pcrpid);
     }

     dmx_fd = open(argv[1], O_RDONLY | O_NONBLOCK);
     if (dmx_fd < 0) {
          fprintf(stderr, "cannot open source file\n");
          return -1;
     }

     d = softmpeg_decoder_create(NULL, NULL, SOFTMPEG_DIRECTFB, &init, SOFTMPEG_FUSIONSOUND, NULL);
     if (NULL == d) {
          fprintf(stderr, "creating softmpeg instance failed\n");
          return -1;
     }

     softmpeg_decoder_set_pids(d, vpid,apid,pcrpid,-1);

     if (0 != softmpeg_decoder_polling_thread_create(d, dmx_fd)) {
          fprintf(stderr, "creating softmpeg polling thread failed\n");
          return -1;
     }

     while (!quit) {
          DFBInputEvent evt;

          keybuffer->WaitForEvent(keybuffer);

          while (keybuffer->GetEvent(keybuffer, DFB_EVENT(&evt)) == DFB_OK) {
               if (evt.type == DIET_KEYPRESS) {
                    switch (DFB_LOWER_CASE(evt.key_symbol)) {
                         case DIKS_SMALL_P: {
			 	if (mode == SOFTMPEG_PLAY)
					mode = softmpeg_decoder_set_mode (d, SOFTMPEG_PAUSE, 0);
				else 
					mode = softmpeg_decoder_set_mode (d, SOFTMPEG_PLAY, 0);
				break;
			}
			case DIKS_CURSOR_RIGHT: {
				mode = softmpeg_decoder_set_mode (d, SOFTMPEG_SKIP, 10);
				break;
			}
			case DIKS_CURSOR_LEFT: {
				mode = softmpeg_decoder_set_mode (d, SOFTMPEG_SKIP, -10);
				break;
			}
                         case DIKS_ESCAPE:
                         case DIKS_SMALL_Q:
                         case DIKS_BACK:
                         case DIKS_STOP:
                              /* quit main loop */
                              quit = 1;
                              break;
                    }
               }
          }
     }

     close(dmx_fd);
     softmpeg_decoder_destroy(d);
     dfb_destroy();

     return 0;
}

LinuxTV legacy CVS <linuxtv.org/cvs>