From: Dario Faggioli Date: Thu, 14 May 2009 06:52:16 +0000 (+0200) Subject: Added new version, thread-based of the video playing demo. X-Git-Url: http://rtime.felk.cvut.cz/gitweb/frescor/frsh.git/commitdiff_plain/938423a6d6f1222d2abede351bdf927e439d89f4 Added new version, thread-based of the video playing demo. --- diff --git a/demo/video/Makefile.omk b/demo/video/Makefile.omk index 404f40a..02ede68 100644 --- a/demo/video/Makefile.omk +++ b/demo/video/Makefile.omk @@ -4,3 +4,6 @@ lib_LOADLIBES+= m pthread rt $(FRSH_LIBS) bin_PROGRAMS = fplayer fplayer_SOURCES = play.c play.h +bin_PROGRAMS = fplayer_th +fplayer_th_SOURCES = play_th.c play_th.h + diff --git a/demo/video/play_th.c b/demo/video/play_th.c new file mode 100644 index 0000000..82df285 --- /dev/null +++ b/demo/video/play_th.c @@ -0,0 +1,107 @@ +#include "play_th.h" + +static void player_cleanup(void *arg) +{ + frsh_vres_id_t vres; + + frsh_thread_get_vres_id(fosa_thread_self(), &vres); + frsh_contract_cancel(vres); +} + +void *player(void *thread_args) +{ + struct player_args args = *((struct player_args*) thread_args); + + frsh_rel_time_t player_budget, player_period; + frsh_contract_t player_contract; + frsh_vres_id_t player_vres; + + int terror, status; + char cmd[255]; + + fprintf(stdout, "== Waiting for being bound...\n"); + + fprintf(stdout, + "== Starting playing %s, with %d/%d CPU bandwidth\n", + args.file, args.budget, args.period); + + player_budget = fosa_usec_to_rel_time(args.budget); + player_period = fosa_usec_to_rel_time(args.period); + PXW(frsh_contract_init(&player_contract)); + PXW(frsh_contract_set_basic_params(&player_contract, + &player_budget, + &player_period, + FRSH_WT_INDETERMINATE, + FRSH_CT_REGULAR)); + PXW(frsh_contract_set_resource_and_label(&player_contract, + FRSH_RT_PROCESSOR, + 0, + "PLAYER_CONTRACT")); + fprintf(stdout, "== Player contract initialized\n"); + + PXW(frsh_contract_negotiate(&player_contract, &player_vres)); + fprintf(stdout, "== Player contract negotiated\n"); + + pthread_cleanup_push(player_cleanup, NULL); + + PXW(frsh_thread_bind(player_vres, fosa_thread_self())); + fprintf(stdout, "== Player thread bound to its contract\n"); + + snprintf(cmd, 255, "mplayer -fs -zoom %s", args.file); + + fprintf(stdout, "== Issuing command:\n %s\n", cmd); + + status = system(cmd); + + if (WIFEXITED(status)) + fprintf(stdout, "== Playback finished normally\n"); + else if (WIFSIGNALED(status)) + fprintf(stdout, "== Playback aborted\n"); + + pthread_cleanup_pop(1); + + pthread_exit(EXIT_SUCCESS); +} + +int main(int argc, char *argv[]) +{ + pthread_t player_thread; + pthread_attr_t player_attr; + struct player_args args; + int opt, terror; + + if (argc < 7) { +err_usage: + fprintf(stderr, "USAGE:\n" + "%s -b budget_usec -p period_usec -f file\n\n", + argv[0]); + error(EXIT_FAILURE, EINVAL, "Wrong argument count"); + } + + while ((opt = getopt(argc, argv, "b:p:f:")) != -1) { + switch (opt) { + case 'b': + args.budget = atoi(optarg); + break; + case 'p': + args.period = atoi(optarg); + break; + case 'f': + strncpy(args.file, optarg, sizeof(args.file)); + break; + default: + goto err_usage; + } + } + + PXW(frsh_init()); + + terror = pthread_create(&player_thread, &player_attr, player, NULL); + if (terror) assert_perror(errno); + + terror = pthread_join(player_thread, NULL); + if (terror) assert_perror(errno); + + exit(EXIT_SUCCESS); +} + diff --git a/demo/video/play_th.h b/demo/video/play_th.h new file mode 100644 index 0000000..ed4ef47 --- /dev/null +++ b/demo/video/play_th.h @@ -0,0 +1,25 @@ +#ifndef _PLAY_H_ +#define _PLAY_H_ + +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif + +#include +#include +#include +#include +#include +#include + +#include + +struct player_args { + int budget, period; + char file[128]; +}; + +void *player(void *args); + +#endif /* _PLAY_H_ */ +