Incorporating video into your BlackBerry 10 OpenGL applications

Game Development

Guest post from Dale D. – Ed.

So, you want to add video to your native OpenGL application on BlackBerry 10? Below I’ll go through the steps that you need to complete in order to get it up and running quickly.

The first thing you’ll want to do is create a window group in your screen creation code. The window group will be used to attach the video window to. The code is as follows:

       // Window group name
static const char *window_group_name = "videowindowgroup";

       if (screen_create_window_group(screen_win, window_group_name)) 
       {
              fprintf(stderr,"screen_create_window_group failed");
       }

The next thing you’ll want to do is handle the initialization of the video screen. There is a screen event called SCREEN_EVENT_CREATE that you need to listen to and initialize the video screen as you see fit. For example, if I wanted my screen to take up an arbitrary width and height I could do the following:

       case SCREEN_EVENT_CREATE:
{
              screen_window_t video_window;
              screen_get_event_property_pv(screen_event, SCREEN_PROPERTY_WINDOW, (void **)&video_window);

              int size[2];
              size[0] = video_width;
              size[1] = video_height;

              screen_set_window_property_iv(video_window, SCREEN_PROPERTY_SIZE, size);
              screen_set_window_property_iv(video_window, SCREEN_PROPERTY_BUFFER_SIZE, size);

              int order = 1;
              screen_set_window_property_iv(video_window, SCREEN_PROPERTY_ZORDER, &order);
              break;
}

It’s important to set the zorder on the video window as it’s behind your main window by default, so make sure the order is set to a positive number.

Once initialization is complete, you can create a method to play your video. You can just use mmrenderer in order to do the heavy lifting. We just need to point it to use the window group that we’ve already defined called “videowindowgroup”. An example might look like this:

// I/O devices
static const char *video_device_url    = "screen:?winid=videowindowgroup&wingrp=videowindowgroup";
static const char *audio_device_url    = "audio:default";

// Name of video context
static const char *video_context_name = "videocontextname";

// I/O variables
int                video_device_output_id = -1;
int                audio_device_output_id = -1;

// Renderer variables
static mmr_connection_t*     mmr_connection = 0;
static mmr_context_t*        mmr_context = 0;

int playVideo()
{
       int rc;

       /*
       * Configure mm-renderer.
       */
       mmr_connection = mmr_connect(NULL);
       if (mmr_connection == NULL) {
              return EXIT_FAILURE;
       }

       mmr_context = mmr_context_create(mmr_connection, video_context_name, 0, S_IRWXU|S_IRWXG|S_IRWXO);
       if (mmr_context == NULL) {
              return EXIT_FAILURE;
       }

       /*
       * 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;
       }

       audio_device_output_id = mmr_output_attach(mmr_context, audio_device_url, "audio");
       if (audio_device_output_id == -1) {
              return EXIT_FAILURE;
       }

       /*
       * Build up the path to where the video is.
       */
       char cwd[PATH_MAX];
       char media_file[PATH_MAX];
       getcwd(cwd, PATH_MAX);

       rc = snprintf(media_file, PATH_MAX, "file://%s/app/native/myVideo.m4v", cwd);
       if ((rc == -1) || (rc >= PATH_MAX)) {
              return EXIT_FAILURE;
       }

       /*
       * Start the playback.
       */
       if (mmr_input_attach(mmr_context, media_file, "track") != 0) {
              fprintf(stderr, "failed to attach video\n");
              return EXIT_FAILURE;
       }

       if (mmr_play(mmr_context) != 0) {
              return EXIT_FAILURE;
       }

       return EXIT_SUCCESS;
}

Finally when we’re done, we’ll want to clean up the window and the mmrenderer contexts:

void clearVideoScreen(screen_event_t screen_event)
{
       mmr_stop(mmr_context);
       mmr_output_detach(mmr_context, audio_device_output_id);
       mmr_output_detach(mmr_context, video_device_output_id);
       mmr_context_destroy(mmr_context);

       mmr_context = 0;
       video_device_output_id = -1;
       audio_device_output_id = -1;

       mmr_disconnect(mmr_connection);
       mmr_connection = 0;

       int order = -1;
       screen_set_window_property_iv(video_window, SCREEN_PROPERTY_ZORDER, &order);

       int visible = 0;
       screen_set_window_property_iv(video_window, SCREEN_PROPERTY_VISIBLE, &visible);

       screen_destroy_window(video_window);
}

Hopefully this gives you a good start into incorporating video into your BlackBerry 10 applications. For some other related samples of using video, you can check out the VideoPlayerSample in the Cascades-Community-Samples and also the Video Overlay sample in the native samples.

Join the conversation

Show comments Hide comments
+ -
blog comments powered by Disqus