source: trunk/video/RpMediaPlayerTclInterface.cc @ 3177

Last change on this file since 3177 was 3177, checked in by mmc, 12 years ago

Updated all of the copyright notices to reference the transfer to
the new HUBzero Foundation, LLC.

File size: 5.8 KB
Line 
1/*
2 * ----------------------------------------------------------------------
3 *  Rappture::MediaPlayer
4 *
5 *  This is an interface to the rappture movieplayer module.
6 *  It allows you to grab image frames from mpeg movies using ffmpeg.
7 * ======================================================================
8 *  AUTHOR:  Derrick Kearney, Purdue University
9 *  Copyright (c) 2004-2012  HUBzero Foundation, LLC
10 *
11 *  See the file "license.terms" for information on usage and
12 *  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13 * ======================================================================
14 */
15#include <tcl.h>
16#include "RpMediaPlayer.h"
17
18extern "C" Tcl_AppInitProc RpMediaPlayer_Init;
19
20#include "RpOp.h"
21
22static Tcl_ObjCmdProc MediaPlayerCmd;
23static Tcl_ObjCmdProc MediaPlayerCallCmd;
24static Tcl_ObjCmdProc InitOp;
25static Tcl_ObjCmdProc LoadOp;
26static Tcl_ObjCmdProc ReadOp;
27static Tcl_ObjCmdProc ReleaseOp;
28
29static Rp_OpSpec rpMediaPlayerOps[] = {
30    {"init",  1, (void *)InitOp, 3, 3, "<path>",},
31    {"load",  1, (void *)LoadOp, 3, 3, "<path>",},
32    {"read",  1, (void *)ReadOp, 2, 2, "",},
33    {"release", 1, (void *)ReleaseOp, 2, 2, "",},
34};
35
36static int nRpMediaPlayerOps = sizeof(rpMediaPlayerOps) / sizeof(Rp_OpSpec);
37
38/*
39 * ------------------------------------------------------------------------
40 *  RpMediaPlayer_Init()
41 *
42 *  Called in Rappture_Init() to initialize the commands defined
43 *  in this file.
44 * ------------------------------------------------------------------------
45 */
46int
47RpMediaPlayer_Init(Tcl_Interp *interp)
48{
49
50    Tcl_CreateObjCommand(interp, "::Rappture::MediaPlayer",
51        MediaPlayerCmd, (ClientData)NULL, (Tcl_CmdDeleteProc*)NULL);
52
53    return TCL_OK;
54}
55
56/*
57 * USAGE: MediaPlayer <file>
58 */
59static int
60MediaPlayerCmd(ClientData clientData, Tcl_Interp *interp, int objc,
61       Tcl_Obj* const *objv)
62{
63    char cmdName[64];
64    static int movieCount = 0;
65    const char *filename = Tcl_GetString(objv[1]);
66    Rappture::Outcome context;
67
68    // create a new command
69    Rappture::MediaPlayer *movie = new Rappture::MediaPlayer();
70    if (!movie->init(context, filename)) {
71        Tcl_AppendResult(interp, context.remark(), "\n",
72                         context.context(), (char*)NULL);
73        return TCL_ERROR;
74    }
75
76    sprintf(cmdName,"::movieObj%d",movieCount);
77
78    Tcl_CreateObjCommand(interp, cmdName, MediaPlayerCallCmd,
79        (ClientData)movie, (Tcl_CmdDeleteProc*)NULL);
80
81    Tcl_AppendResult(interp, cmdName, (char*)NULL);
82    return TCL_OK;
83}
84
85
86static int
87MediaPlayerCallCmd(ClientData clientData, Tcl_Interp *interp, int objc,
88           Tcl_Obj *const *objv)
89{
90    Tcl_ObjCmdProc *proc;
91
92    proc = (Tcl_ObjCmdProc *)Rp_GetOpFromObj(interp, nRpMediaPlayerOps, rpMediaPlayerOps,
93        RP_OP_ARG1, objc, objv, 0);
94
95    if (proc == NULL) {
96        return TCL_ERROR;
97    }
98    return (*proc)(clientData, interp, objc, objv);
99}
100
101/**********************************************************************/
102// FUNCTION: InitOp()
103/// initialize a movie player object
104/**
105 * Initialize a movie player object with the video located at <path>
106 * Full function call:
107 *
108 * init <path>
109 *
110 * <path> is the path of a video file.
111 */
112static int
113InitOp (ClientData clientData, Tcl_Interp *interp, int objc,
114         Tcl_Obj *const *objv)
115{
116    Rappture::Outcome context;
117    const char *path = NULL;    // path of video
118
119    path = Tcl_GetString(objv[0]);
120
121    if (! ((Rappture::MediaPlayer *) clientData)->init(context, path) ) {
122        Tcl_AppendResult(interp, context.remark(), "\n",
123                         context.context(), (char*)NULL);
124        return TCL_ERROR;
125    }
126
127    return TCL_OK;
128}
129
130/**********************************************************************/
131// FUNCTION: LoadOp()
132/// Load a new video into the movie player object
133/**
134 * Load a new video into the movie player object
135 * Full function call:
136 *
137 * load <path>
138 *
139 * <path> is the path of a video file.
140 */
141static int
142LoadOp (ClientData clientData, Tcl_Interp *interp, int objc,
143         Tcl_Obj *const *objv)
144{
145    Rappture::Outcome context;
146    const char *path = NULL;    // path of video
147
148    path = Tcl_GetString(objv[2]);
149
150    if (! ((Rappture::MediaPlayer *) clientData)->load(context, path) ) {
151        Tcl_AppendResult(interp, context.remark(), "\n",
152                         context.context(), (char*)NULL);
153        return TCL_ERROR;
154    }
155
156    return TCL_OK;
157}
158
159/**********************************************************************/
160// FUNCTION: ReadOp()
161/// Read frames from a video
162/**
163 * Read frames from a video and return them as images
164 * Full function call:
165 *
166 * read
167 *
168 */
169static int
170ReadOp (ClientData clientData, Tcl_Interp *interp, int objc,
171         Tcl_Obj *const *objv)
172{
173    Rappture::Outcome context;
174    Rappture::SimpleCharBuffer sb;
175
176    if (((Rappture::MediaPlayer *)clientData)->read(context, &sb) != 1 ) {
177        Tcl_AppendResult(interp, context.remark(), "\n",
178                         context.context(), (char*)NULL);
179        return TCL_ERROR;
180    }
181
182    Tcl_SetByteArrayObj(Tcl_GetObjResult(interp),
183                        (const unsigned char*)sb.bytes(), sb.size());
184
185    return TCL_OK;
186}
187
188/**********************************************************************/
189// FUNCTION: ReleaseOp()
190/// Clean up memory from an open video in a movie player object
191/**
192 * Close all file handles and free allocated memory associated with an
193 * open video in a movie player object.
194 * Full function call:
195 *
196 * close
197 *
198 */
199static int
200ReleaseOp (ClientData clientData, Tcl_Interp *interp, int objc,
201         Tcl_Obj *const *objv)
202{
203    Rappture::Outcome context;
204
205    if (! ((Rappture::MediaPlayer *) clientData)->release() ) {
206        Tcl_AppendResult(interp, context.remark(), "\n",
207                         context.context(), (char*)NULL);
208        return TCL_ERROR;
209    }
210
211    Tcl_ResetResult(interp);
212    return TCL_OK;
213}
214
Note: See TracBrowser for help on using the repository browser.