Managing video windows

You can render video to a screen using the Screen and Windowing API, libscreen.

The following example shows how to give mm-renderer a window group and window ID to use in creating a window on the application's behalf, configure mm-renderer for audio and video output, and get a handle to the window and use libscreen to manipulate it.

This example omits many steps for the sake of simplicity. For the full example, see the VideoWindow sample.

To begin, we define the libscreen window group and window ID to use for the output. In this example, the same string is used for both: "videosamplewindowgroup_" with a random number appended to ensure uniqueness. The window group and window ID are the same in this example, but they don't have to be. We use these two properties to set the output URL, video_device_url.

app_id = rand();

// Create the video URL for mm-renderer.
// (PATH_MAX is the maximum length of a filepath on the device.)

static char video_device_url[PATH_MAX];
rc = snprintf(video_device_url, PATH_MAX, 
       "screen:?winid=videosamplewindowgroup_%d
        &wingrp=videosamplewindowgroup_%d", app_id, app_id);
if (rc < 0) {
    fprintf(stderr, "Error building video device URL string\n");
    return EXIT_FAILURE;
}
else if (rc >= PATH_MAX) {
    fprintf(stderr, "Video device URL too long\n");
    return EXIT_FAILURE;
}

// Create the video context name for mm-renderer
static char video_context_name[PATH_MAX];
...

Using the same random number as before, put the window group name in a variable that we can pass to the screen_create_window_group() function. Once the window group is created, we connect to mm-renderer and create a context. Finally, we attach the output to the context by calling mmr_output_attach(), specifying the URL variable we set up earlier, video_device_url. We use the same function to attach the audio output.

// Window group name, with the same random number appended.
static char window_group_name[PATH_MAX];
rc = snprintf(window_group_name, PATH_MAX, 
              "videosamplewindowgroup_%d", app_id);
if (rc < 0) {
    fprintf(stderr, "Error building window group name string: \
                    %s\n", strerror(errno));
    return EXIT_FAILURE;
}
else if (rc >= PATH_MAX) {
    fprintf(stderr, "Video context name too long\n");
    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' 
 * its CHILD_WINDOW, which contains the video.
 */
if (screen_create_window_group(
        g_screen_win, window_group_name) != 0) {
    fprintf(stderr, "Error creating window group: %s\n", 
            strerror(errno));
    return EXIT_FAILURE;
}

/*
 * Configure mm-renderer.
 */
mmr_connection = mmr_connect(NULL);
if (mmr_connection == NULL) {
    fprintf(stderr, "Error connecting to renderer service: %s\n", 
            strerror(errno));
    return EXIT_FAILURE;
}

mmr_context = mmr_context_create( mmr_connection, 
                                  video_context_name, 
                                  0, 
                                  S_IRWXU|S_IRWXG|S_IRWXO );
if (mmr_context == NULL) {
    fprintf(stderr, "Error creating renderer context: %s\n", 
            strerror(errno));
    return EXIT_FAILURE;
}

/*
 * Configure video and audio output.
 */
const mmr_error_info_t* errorInfo;
 
video_device_output_id = mmr_output_attach( mmr_context, 
                                            video_device_url,
                                            "video" );
if (video_device_output_id == -1) {
    errorInfo = mmr_error_info(mmr_context);
    fprintf(stderr, "Attaching video output produced error code \
            %d\n", errorInfo->error_code); 
    return EXIT_FAILURE;
}

audio_device_output_id = mmr_output_attach( mmr_context, 
                                            audio_device_url, 
                                            "audio" );
if (audio_device_output_id == -1) {
    errorInfo = mmr_error_info(mmr_context);
    fprintf(stderr, "Attaching audio output produced error code \
            %d\n", errorInfo->error_code); 
    return EXIT_FAILURE;
}

Next, we retrieve the window handle of the video window from the screen event received when the window is created. Note that we ensure that the ID of the window matches our video window. For more complicated applications, this is important so that we can distinguish between the video window and another child window belonging to the same window group. Note that the function screen_event_get_event() is from the BlackBerry Platform Services (BPS) library, libbps.

screen_event_t screen_event = screen_event_get_event(event);
int event_type;
screen_get_event_property_iv( screen_event, 
                              SCREEN_PROPERTY_TYPE, 
                              &event_type );

// Check if it's a creation event and the video output window 
// has not yet been initialized
if ((event_type == SCREEN_EVENT_CREATE) && 
        (video_window == (screen_window_t)NULL)) {
    char id[256];

    rc = screen_get_event_property_pv( screen_event, 
                                       SCREEN_PROPERTY_WINDOW,
                                       (void**)&video_window );
    if (rc != 0) {
        fprintf(stderr, "Error reading event window: %s\n", 
                strerror(errno));
        return EXIT_FAILURE;
    }

    rc = screen_get_window_property_cv( video_window, 
                                        SCREEN_PROPERTY_ID_STRING, 
                                        256, 
                                        id );
    if (rc != 0) {
        fprintf(stderr, "Error reading window ID: %s\n", 
            strerror(errno));
        return EXIT_FAILURE;
    }

    if (strncmp(
            id, window_group_name, strlen(window_group_name)) != 0)
        fprintf(stderr, "Mismatch in window group names\n");
        return EXIT_FAILURE;
}

...

Once we have this handle we can manipulate the video window directly with libscreen function calls.

// Set the z-order of the video window to put it above or below 
// the main window. Alternate between +1 and -1 to implement
// double-buffering to avoid flickering of output.
app_window_above = !app_window_above;
if (app_window_above) {
    screen_val = 1;
}
else {
    screen_val = -1;
}

if (screen_set_window_property_iv( video_window, 
                                   SCREEN_PROPERTY_ZORDER, 
                                   &screen_val ) != 0) {
    fprintf(stderr, "Error setting z-order of video window: %s\n", 
            strerror(errno));
    return EXIT_FAILURE;
}

// Set the video window to be visible.
screen_val = 1;
if (screen_set_window_property_iv( video_window, 
                                   SCREEN_PROPERTY_VISIBLE, 
                                   &screen_val) != 0 ) {
    fprintf(stderr, "Error making window visible: %s\n", 
            strerror(errno));
    return EXIT_FAILURE;
}

...