--- /dev/null
+#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);
+}
+