]> rtime.felk.cvut.cz Git - frescor/frsh.git/blob - demo/video/play_th.c
Bugfixes in the video demo.
[frescor/frsh.git] / demo / video / play_th.c
1 #include "play_th.h"
2
3 static void player_cleanup(void *arg)
4 {
5         frsh_vres_id_t vres;
6
7         frsh_thread_get_vres_id(fosa_thread_self(), &vres);
8         frsh_contract_cancel(vres);
9 }
10
11 void *player(void *thread_args)
12 {
13         struct player_args *args = (struct player_args*) thread_args;
14
15         frsh_rel_time_t player_budget, player_period;
16         frsh_contract_t player_contract;
17         frsh_vres_id_t player_vres;
18
19         int terror, status;
20         char cmd[255];
21
22         fprintf(stdout, "== Waiting for being bound...\n");
23
24         fprintf(stdout,
25                 "== Starting playing %s, with %d/%d CPU bandwidth\n",
26                 args->file, args->budget, args->period);
27
28         player_budget = fosa_usec_to_rel_time(args->budget);
29         player_period = fosa_usec_to_rel_time(args->period);
30         PXW(frsh_contract_init(&player_contract));
31         PXW(frsh_contract_set_basic_params(&player_contract,
32                                            &player_budget,
33                                            &player_period,
34                                            FRSH_WT_INDETERMINATE,
35                                            FRSH_CT_REGULAR));
36         PXW(frsh_contract_set_resource_and_label(&player_contract,
37                                                  FRSH_RT_PROCESSOR,
38                                                  0,
39                                                  "PLAYER_CONTRACT"));
40         fprintf(stdout, "== Player contract initialized\n");
41
42         PXW(frsh_contract_negotiate(&player_contract, &player_vres));
43         fprintf(stdout, "== Player contract negotiated\n");
44
45         pthread_cleanup_push(player_cleanup, NULL);
46
47         PXW(frsh_thread_bind(player_vres, fosa_thread_self()));
48         fprintf(stdout, "== Player thread bound to its contract\n");
49
50         snprintf(cmd, 255, PLAYER_BINARY PLAYER_ARGS " %s", args->file);
51
52         fprintf(stdout, "== Issuing command:\n  %s\n", cmd);
53
54         status = system(cmd);
55
56         if (WIFEXITED(status))
57                 fprintf(stdout, "== Playback finished normally\n");
58         else if (WIFSIGNALED(status))
59                 fprintf(stdout, "== Playback aborted\n");
60
61         pthread_cleanup_pop(1);
62
63         pthread_exit(EXIT_SUCCESS);
64 }
65
66 int main(int argc, char *argv[])
67 {
68         pthread_t player_thread;
69         pthread_attr_t player_attr;
70         struct player_args args;
71         int opt, terror;
72
73         if (argc < 7) {
74 err_usage:
75                 fprintf(stderr, "USAGE:\n"
76                         "%s -b budget_usec -p period_usec -f file\n\n",
77                         argv[0]);
78                 error(EXIT_FAILURE, EINVAL, "Wrong argument count");
79         }
80
81         while ((opt = getopt(argc, argv, "b:p:f:")) != -1) {
82                 switch (opt) {
83                         case 'b':
84                                 args.budget = atoi(optarg);
85                                 break;
86                         case 'p':
87                                 args.period = atoi(optarg);
88                                 break;
89                         case 'f':
90                                 strncpy(args.file, optarg, sizeof(args.file));
91                                 break;
92                         default:
93                                 goto err_usage;
94                 }
95         }
96
97         PXW(frsh_init());
98         fprintf(stdout, "FRSH initialized\n");
99
100         terror = pthread_attr_init(&player_attr);
101         if (terror) assert_perror(errno);
102
103         terror = pthread_create(&player_thread, &player_attr, player, (void*) &args);
104         if (terror) assert_perror(errno);
105         fprintf(stdout, "FRSH thread created, waiting for its termination...\n");
106
107         terror = pthread_join(player_thread, NULL);
108         if (terror) assert_perror(errno);
109         fprintf(stdout, "FRSH thread ended. Exiting\n\n");
110
111         exit(EXIT_SUCCESS);
112 }
113