Skip to content

Commit

Permalink
Make the code a bit more efficient and look better
Browse files Browse the repository at this point in the history
For efficiency, we try to minimize the amount of code that is needlessly
initialized over and over again.

To make it look better, we made some whitespace changes and used clearer
variable names.
  • Loading branch information
Anthony Hu committed Jul 26, 2012
1 parent 6f99874 commit 2c9f006
Showing 1 changed file with 77 additions and 68 deletions.
145 changes: 77 additions & 68 deletions VideoWindow/main.c
Expand Up @@ -55,24 +55,29 @@

#include <sys/strm.h>

static const int ctrl_h = 100;
static const int ctrl_w = 100;

static EGLDisplay g_egl_disp = EGL_NO_DISPLAY;
static EGLSurface g_egl_surf = EGL_NO_SURFACE;

static EGLConfig g_egl_conf = ((EGLConfig)0);
static EGLContext g_egl_ctx = EGL_NO_CONTEXT;

static EGLint g_surface_width;
static EGLint g_surface_height;

static screen_context_t g_screen_ctx;
static screen_context_t g_screen_ctx;
static screen_window_t g_screen_win;
static screen_display_t g_screen_disp;

static int app_id = 0;

GLfloat g_triangle_vertices[6];
GLfloat g_square_vertices[10];


void
terminate_egl_window() {
//Typical EGL cleanup

// Typical EGL cleanup
if (g_egl_disp != EGL_NO_DISPLAY) {
eglMakeCurrent(g_egl_disp, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
if (g_egl_surf != EGL_NO_SURFACE) {
Expand Down Expand Up @@ -122,7 +127,9 @@ initialize_egl_window(screen_context_t ctx, char * window_group_name) {
int format = SCREEN_FORMAT_RGBA8888;
int sensitivity = SCREEN_SENSITIVITY_ALWAYS;
int rc, num_configs;
const int num_window_buffers = 2; //must be 2 for RGBA8888

// must be 2 for RGBA8888
const int num_window_buffers = 2;

EGLint attrib_list[]= { EGL_RED_SIZE, 8,
EGL_GREEN_SIZE, 8,
Expand All @@ -132,7 +139,7 @@ initialize_egl_window(screen_context_t ctx, char * window_group_name) {
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES_BIT,
EGL_NONE};

//Simple egl initialization
// Simple egl initialization
g_screen_ctx = ctx;

g_egl_disp = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Expand Down Expand Up @@ -177,8 +184,9 @@ initialize_egl_window(screen_context_t ctx, char * window_group_name) {
return EXIT_FAILURE;
}

/* Create the window group for our window, this is important, as we pass the group name
* to MMR which uses it to 'parent' it's CHILD_WINDOW which contains the video
/* Create the window group for our window, this is important, as we pass
* the group name to MMR which uses it to 'parent' it's CHILD_WINDOW
* which contains the video.
*/
if (screen_create_window_group(g_screen_win, window_group_name) != 0) {
return EXIT_FAILURE;
Expand Down Expand Up @@ -285,47 +293,32 @@ initialize_egl_window(screen_context_t ctx, char * window_group_name) {
return EXIT_FAILURE;
}

// Set the clear color to be transparent
glClearColor(0.0f, 0.25f, 0.0f, 0.0f);

return EXIT_SUCCESS;
}


void render(bool paused)
{
/*
* Clear the window to be fully transparent, this is important because by default the MM renderer
* window will be below our window in Z order, so we will only see the video if our window is transparent
/* Clear the window to be fully transparent, this is important because by
* default the MM renderer window will be below our window in Z order, so
* we will only see the video if our window is transparent. Note that
* transparency was set using glClearColor().
*/
glClearColor(0.0f, 0.25f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);

static const int ctrl_h = 100;
static const int ctrl_w = 100;

int orig_x = (float) g_surface_width / 2 - ctrl_w / 2;
int orig_y = (float)g_surface_height / 2 - ctrl_h / 2;

GLfloat triangle_vertices[] = { orig_x, orig_y,
orig_x, orig_y + ctrl_h,
orig_x+ctrl_w, orig_y + ctrl_h/2
};

GLfloat square_vertices[] = { orig_x, orig_y,
orig_x, orig_y + ctrl_h,
orig_x+ctrl_w,orig_y+ctrl_h,
orig_x+ctrl_w,orig_y,
orig_x,orig_y
};

glEnableClientState(GL_VERTEX_ARRAY);

if (!paused)
{
glVertexPointer(2, GL_FLOAT, 0, square_vertices);
glVertexPointer(2, GL_FLOAT, 0, g_square_vertices);
glColor4f(0.123f, 0.18f, .9f, 1.0f);
glDrawArrays(GL_TRIANGLE_STRIP, 0 , 5);
}
else {
glVertexPointer(2, GL_FLOAT, 0, triangle_vertices);
glVertexPointer(2, GL_FLOAT, 0, g_triangle_vertices);
glColor4f(0.123f, 0.78f, 0.0, 1.0f);
glDrawArrays(GL_TRIANGLE_STRIP, 0 , 3);
}
Expand Down Expand Up @@ -354,7 +347,7 @@ strm_dict_t* calculate_rect(int width, int height) {
return NULL;
}

//fullscreen is the default.
// fullscreen is the default.
dict = strm_dict_set(dict, "video_dest_x", "0");
if (NULL == dict)
goto fail;
Expand Down Expand Up @@ -425,6 +418,13 @@ int main(int argc, char *argv[])
int video_device_output_id = -1;
int audio_device_output_id = -1;

// Position of the play and stop button.
static int ctrl_x = 0;
static int ctrl_y = 0;

EGLint surface_width;
EGLint surface_height;

srand(time(0));
app_id = rand();

Expand Down Expand Up @@ -456,29 +456,27 @@ int main(int argc, char *argv[])

bps_initialize();

/*
* Create the Screen Context.
*/
// Create the Screen Context.
if (screen_create_context(&g_screen_ctx, SCREEN_APPLICATION_CONTEXT) != 0) {
return EXIT_FAILURE;
}

/* Create the window and initialize EGL for GL_ES_1 rendering*/
// Create the window and initialize EGL for GL_ES_1 rendering
rc = initialize_egl_window(g_screen_ctx, window_group_name);
if (rc != EXIT_SUCCESS)
return EXIT_FAILURE;

//Query width and height of the window surface created by utility code
eglQuerySurface(g_egl_disp, g_egl_surf, EGL_WIDTH, &g_surface_width);
eglQuerySurface(g_egl_disp, g_egl_surf, EGL_HEIGHT, &g_surface_height);
// Query width and height of the window surface created by utility code
eglQuerySurface(g_egl_disp, g_egl_surf, EGL_WIDTH, &surface_width);
eglQuerySurface(g_egl_disp, g_egl_surf, EGL_HEIGHT, &surface_height);
EGLint err = eglGetError();
if (err != EGL_SUCCESS) {
fprintf(stderr, "Unable to query EGL surface dimensions\n");
return EXIT_FAILURE;
}

//Initialize GL for 2D rendering
glViewport(0, 0, (int)g_surface_width, (int) g_surface_height);
// Initialize GL for 2D rendering
glViewport(0, 0, (int)surface_width, (int) surface_height);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
Expand All @@ -488,13 +486,34 @@ int main(int argc, char *argv[])
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

//Set world coordinates to coincide with screen pixels
glScalef(1.0f / (float)g_surface_width, 1.0f / (float)g_surface_height, 1.0f);
// Set world coordinates to coincide with screen pixels
glScalef(1.0f / (float)surface_width, 1.0f / (float)surface_height, 1.0f);

// We can calculate location and verticies of the controls
ctrl_x = (float)surface_width / 2 - ctrl_w / 2;
ctrl_y = (float)surface_height / 2 - ctrl_h / 2;

/*
* Configure mm-renderer.
*/
g_triangle_vertices[0] = ctrl_x;
g_triangle_vertices[1] = ctrl_y;

g_triangle_vertices[2] = ctrl_x;
g_triangle_vertices[3] = ctrl_y + ctrl_h;

g_triangle_vertices[4] = ctrl_x + ctrl_w;
g_triangle_vertices[5] = ctrl_y + ctrl_h / 2;

g_square_vertices[0] = ctrl_x;
g_square_vertices[1] = ctrl_y;
g_square_vertices[2] = ctrl_x;
g_square_vertices[3] = ctrl_y + ctrl_h;
g_square_vertices[4] = ctrl_x + ctrl_w;
g_square_vertices[5] = ctrl_y + ctrl_h;
g_square_vertices[6] = ctrl_x + ctrl_w;
g_square_vertices[7] = ctrl_y;
g_square_vertices[8] = ctrl_x;
g_square_vertices[9] = ctrl_y;

// Configure mm-renderer.
mmr_connection = mmr_connect(NULL);
if (mmr_connection == NULL) {
return EXIT_FAILURE;
Expand All @@ -505,9 +524,7 @@ int main(int argc, char *argv[])
return EXIT_FAILURE;
}

/*
* Configure video and audio output.
*/
// Configure video and audio output.
video_device_output_id = mmr_output_attach(mmr_context, video_device_url, "video");
if (video_device_output_id == -1) {
return EXIT_FAILURE;
Expand All @@ -532,32 +549,27 @@ int main(int argc, char *argv[])
return EXIT_FAILURE;
}

/*
* Attach the input media.
*/
// Attach the input media.
if (mmr_input_attach(mmr_context, media_file, "track") != 0) {
return EXIT_FAILURE;
}


int video_speed = 0;
/*
* Set the speed to 0 to pause the video initially
*/

// Set the speed to 0 to pause the video initially
if (mmr_speed_set(mmr_context, video_speed) != 0) {
return EXIT_FAILURE;
}

/*
* Change to the play state, although speed is zero
*/
// Change to the play state, although speed is zero
if (mmr_play(mmr_context) != 0) {
return EXIT_FAILURE;
}

/* Do some work to make the aspect ratio correct.
*/
dict = calculate_rect(g_surface_width, g_surface_height);
dict = calculate_rect(surface_width, surface_height);
if (NULL == dict) {
return EXIT_FAILURE;
}
Expand All @@ -566,9 +578,9 @@ int main(int argc, char *argv[])
return EXIT_FAILURE;
}

/* Note that we allocated memory for the dictionary, but the call to
* mmr_output_parameters() deallocates that memory even on failure.
*/
/* Note that we allocated memory for the dictionary, but the call to
* mmr_output_parameters() deallocates that memory even on failure.
*/
dict = NULL;

screen_request_events(g_screen_ctx);
Expand All @@ -579,15 +591,12 @@ int main(int argc, char *argv[])
int screen_val;


/*
* Handle keyboard events and stop playback upon user request.
*/
// Handle keyboard events and stop playback upon user request.
for (;;) {
bps_event_t *event = NULL;
if (bps_get_event(&event, 0) != BPS_SUCCESS) {
return EXIT_FAILURE;
}
//rc = eglSwapBuffers(g_egl_disp, g_egl_surf);
if (event) {

if (bps_event_get_domain(event) == navigator_get_domain())
Expand Down

0 comments on commit 2c9f006

Please sign in to comment.