~ubuntu-branches/ubuntu/vivid/gpac/vivid

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
/*
 *			GPAC - Multimedia Framework C SDK
 *
 *			Copyright (c) ENST 2006-200X
 *				Authors: Jean Le Feuvre 
 *					All rights reserved
 *
 *  This file is part of GPAC / Symbian GUI player
 *
 *  GPAC is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *   
 *  GPAC is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *   
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
 *
 */


// INCLUDE FILES
#include <coemain.h>
#include <eikenv.h>

#include "osmo4_view.h"
#include "osmo4_ui.h"

#include <gpac/options.h>
/*for initial setup*/
#include <gpac/modules/service.h>


#if defined(__SERIES60_3X__)
#define GPAC_CFG_DIR	"\\private\\F01F9075\\"
#define GPAC_MODULES_DIR	"\\sys\\bin\\"
#else
#define GPAC_CFG_DIR	"\\system\\apps\\Osmo4\\"
#define GPAC_MODULES_DIR	GPAC_CFG_DIR
#endif

// ============================ MEMBER FUNCTIONS ===============================

// -----------------------------------------------------------------------------
// Cosmo4AppView::NewL()
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
COsmo4AppView* COsmo4AppView::NewL( const TRect& aRect )
{
    COsmo4AppView* self = COsmo4AppView::NewLC( aRect );
    CleanupStack::Pop( self );
    return self;
}

// -----------------------------------------------------------------------------
// COsmo4AppView::NewLC()
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
COsmo4AppView* COsmo4AppView::NewLC( const TRect& aRect )
{
    COsmo4AppView* self = new ( ELeave ) COsmo4AppView;
    CleanupStack::PushL( self );
    self->ConstructL( aRect );
    return self;
}



// -----------------------------------------------------------------------------
// COsmo4AppView::COsmo4AppView()
// C++ default constructor can NOT contain any code, that might leave.
// -----------------------------------------------------------------------------
//
COsmo4AppView::COsmo4AppView()
{
    // No implementation required
	m_pTimer = NULL;
#ifndef GPAC_GUI_ONLY
	memset(&m_user, 0, sizeof(GF_User));
	m_term = NULL;
	m_mx = NULL;
	memset(&m_rti, 0, sizeof(GF_SystemRTInfo));
#endif
	last_title_update = 0;
	show_rti = 0;
#if defined(__SERIES60_3X__)
	selector = NULL;
	target = NULL;
#endif
}



// -----------------------------------------------------------------------------
// COsmo4AppView::~COsmo4AppView()
// Destructor.
// -----------------------------------------------------------------------------
//
COsmo4AppView::~COsmo4AppView()
{
	Shutdown();
#ifndef GPAC_GUI_ONLY
	if (m_mx) gf_mx_del(m_mx);
#endif

#if defined(__SERIES60_3X__)
	if (selector) delete selector;
	//if (target) delete target;
#endif
}

void COsmo4AppView::Shutdown()
{
//	MessageBox("Osmo4 shutdown request", "");
	if (m_pTimer) {
		m_pTimer->Cancel();
		delete m_pTimer;
		m_pTimer = NULL;
	}
#ifndef GPAC_GUI_ONLY
	if (m_term) {
		GF_Terminal *t = m_term;
		m_term = NULL;
		gf_term_del(t);
	}
    if (m_user.config) {
		gf_cfg_del(m_user.config);
		m_user.config = NULL;
	}
	if (m_user.modules) {
		gf_modules_del(m_user.modules);
		m_user.modules = NULL;
	}
#endif
//	MessageBox("Osmo4 shutdown OK", "");
}

void COsmo4AppView::MessageBox(const char *text, const char *title)
{
	HBufC *msg1, *msg2;
	TInt length = User::StringLength( ( TUint8* ) text) + 1;
	msg1 = HBufC::NewL( length );
	msg1->Des().Copy( TPtrC8(( TText8* ) text) );
	
	length = User::StringLength( ( TUint8* ) title) + 1;
	msg2 = HBufC::NewL( length );
	msg2->Des().Copy( TPtrC8(( TText8* ) title) );
	
	CEikonEnv::Static()->InfoWinL(*msg2, *msg1);
	delete msg1;
	delete msg2;
}

TInt myTick(TAny* aObject)
{
	return ((COsmo4AppView*)aObject)->OnTick();
}

TInt COsmo4AppView::OnTick()
{
#ifndef GPAC_GUI_ONLY
	if (m_term) gf_term_process_step(m_term);

	/*check RTI display*/
	if (show_rti && gf_sys_get_rti(500, &m_rti, 0)) DisplayRTI();
#endif
	/*never stop...*/
	return 1;
}

void COsmo4AppView::DisplayRTI()
{
#ifndef GPAC_GUI_ONLY
	COsmo4AppUi *app = (COsmo4AppUi *) CEikonEnv::Static()->AppUi();
	char szInfo[20];
	sprintf(szInfo, "CPU %02d FPS %02.2f", m_rti.process_cpu_usage, gf_term_get_framerate(m_term, 0));
	app->SetInfo(szInfo);
#endif
}


//GPAC log function
static void on_gpac_log(void *cbk, u32 ll, u32 lm, const char *fmt, va_list list)
{
	char szMsg[2048];
	COsmo4AppView *app = (COsmo4AppView *)cbk;

#ifndef GPAC_GUI_ONLY
	gf_mx_p(app->m_mx);
	if (app->do_log) {
		FILE *logs = fopen("\\data\\gpac_logs.txt", "a+t");
		if (logs) {
			vfprintf(logs, fmt, list);
			fclose(logs);
		}
	} else {
		vsprintf(szMsg, fmt, list);
		app->MessageBox(szMsg, "Error:");
	}
	gf_mx_v(app->m_mx);
#endif
}

static Bool GPAC_EventProc(void *ptr, GF_Event *evt)
{
	COsmo4AppView *app = (COsmo4AppView *)ptr;
	return app->EventProc(evt);
}

Bool COsmo4AppView::EventProc(GF_Event *evt)
{
	TRect r;

#ifndef GPAC_GUI_ONLY
	switch (evt->type) {
	case GF_EVENT_MESSAGE:
		if (!evt->message.message) return 0;
		if (evt->message.error) {
			char err[1024];
			sprintf(err, "Error: %s", gf_error_to_string(evt->message.error));
			MessageBox(evt->message.message, err);
		} else {
			MessageBox(evt->message.message, "Info");
		}
		break;
	case GF_EVENT_SCENE_SIZE:
		r = Rect();
		gf_term_set_size(m_term, r.Width(), r.Height());
		break;
	}
#endif
	return 0;
}

void COsmo4AppView::SetupLogs()
{
	const char *opt;

#ifndef GPAC_GUI_ONLY
	gf_mx_p(m_mx);
	if (do_log) {
		gf_log_set_level(0);
		do_log = 0;
	}
	/*setup GPAC logs: log all errors*/
	opt = gf_cfg_get_key(m_user.config, "General", "LogLevel");
	if ((opt && !stricmp(opt, "debug")) /*|| 1*/) { 
		FILE *logs = fopen("\\data\\gpac_logs.txt", "wt");
		if (!logs) {
			MessageBox("Cannot open log file - disabling logs", "Warning !");
		} else {
			MessageBox("Debug log enabled in \\data\\gpac_logs.txt", "Info");
			fclose(logs);
			do_log = 1;
			gf_log_set_level(GF_LOG_DEBUG);
			gf_log_set_tools(0xFFFFFFFF);
		}
	}
	if (!do_log) {
		gf_log_set_level(GF_LOG_ERROR);
		gf_log_set_tools(0xFFFFFFFF);
	}

	gf_log_set_callback(this, on_gpac_log);
	gf_mx_v(m_mx);

	GF_LOG(GF_LOG_DEBUG, GF_LOG_CORE, ("Osmo4 logs initialized\n"));
#endif
}


static void Osmo4_progress_cbk(void *usr, char *title, u32 done, u32 total)
{
#ifndef GPAC_GUI_ONLY
	COsmo4AppView *view = (COsmo4AppView *) usr;
	COsmo4AppUi *app = (COsmo4AppUi *) CEikonEnv::Static()->AppUi();

	if (done==total) {
		app->SetInfo(NULL);
	} else if (view->last_title_update + 500 < gf_sys_clock()) {
		char szName[1024];
		view->last_title_update = gf_sys_clock();
		sprintf(szName, "%s %02d %%", title, (done*100 / total) );
		app->SetInfo(szName);
	}
#endif
}

// -----------------------------------------------------------------------------
// COsmo4AppView::ConstructL()
// Symbian 2nd phase constructor can leave.
// -----------------------------------------------------------------------------
//
void COsmo4AppView::ConstructL( const TRect& aRect )
{
	const char *opt;
	Bool first_launch = 0;

#if defined(__SERIES60_3X__)
	selector = CRemConInterfaceSelector::NewL();
	target = CRemConCoreApiTarget::NewL(*selector, *this);
	selector->OpenTargetL();
#endif

    // Create a window for this application view
    CreateWindowL();
    // Set the windows size
    SetRect( aRect );
	//draw
    ActivateL();

#ifndef GPAC_GUI_ONLY
	m_window = Window();
	m_session = CEikonEnv::Static()->WsSession();

	m_mx = gf_mx_new("Osmo4");

	//load config file
	m_user.config = gf_cfg_new(GPAC_CFG_DIR, "GPAC.cfg");
	if (!m_user.config) {
		first_launch = 1;
		FILE *ft = fopen(GPAC_CFG_DIR"GPAC.cfg", "wt");
		if (!ft) {
			MessageBox("Cannot create GPAC Config file", "Fatal Error");
			User::Leave(KErrGeneral);
		} else {
			fclose(ft);
		}
		m_user.config = gf_cfg_new(GPAC_CFG_DIR, "GPAC.cfg");
		if (!m_user.config) {
			MessageBox("GPAC Configuration file not found", "Fatal Error");
			User::Leave(KErrGeneral);
		}
	}

	SetupLogs();
	gf_set_progress_callback(this, Osmo4_progress_cbk);

	opt = gf_cfg_get_key(m_user.config, "General", "ModulesDirectory");
	if (!opt) first_launch = 2;
	
	if (first_launch) {
		/*hardcode module directory*/
		gf_cfg_set_key(m_user.config, "General", "ModulesDirectory", GPAC_MODULES_DIR);
		/*hardcode cache directory*/
		gf_cfg_set_key(m_user.config, "General", "CacheDirectory", GPAC_CFG_DIR"cache");
		gf_cfg_set_key(m_user.config, "Downloader", "CleanCache", "yes");
		/*startup file*/
		gf_cfg_set_key(m_user.config, "General", "StartupFile", GPAC_CFG_DIR"gpac.mp4");
		/*setup UDP traffic autodetect*/
		gf_cfg_set_key(m_user.config, "Network", "AutoReconfigUDP", "yes");
		gf_cfg_set_key(m_user.config, "Network", "UDPTimeout", "10000");
		gf_cfg_set_key(m_user.config, "Network", "BufferLength", "3000");
		
		gf_cfg_set_key(m_user.config, "Compositor", "TextureTextMode", "Default");

		
		/*save cfg and reload*/
		gf_cfg_del(m_user.config);
		m_user.config = gf_cfg_new(GPAC_CFG_DIR, "GPAC.cfg");
		if (!m_user.config) {
			MessageBox("Cannot save initial GPAC Config file", "Fatal Error");
			User::Leave(KErrGeneral);
		}


		MessageBox("Osmo4", "Thank you for Installing");
	}

	/*load modules*/
	opt = gf_cfg_get_key(m_user.config, "General", "ModulesDirectory");
	m_user.modules = gf_modules_new(opt, m_user.config);
	if (!m_user.modules || !gf_modules_get_count(m_user.modules)) {
		MessageBox(m_user.modules ? "No modules available" : "Cannot create module manager", "Fatal Error");
		if (m_user.modules) gf_modules_del(m_user.modules);
		gf_cfg_del(m_user.config);
		User::Leave(KErrGeneral);
	}

	if (first_launch) {
		/*first launch, register all files ext*/
		for (u32 i=0; i<gf_modules_get_count(m_user.modules); i++) {
			GF_InputService *ifce = (GF_InputService *) gf_modules_load_interface(m_user.modules, i, GF_NET_CLIENT_INTERFACE);
			if (!ifce) continue;
			if (ifce) {
				ifce->CanHandleURL(ifce, "test.test");
				gf_modules_close_interface((GF_BaseInterface *)ifce);
			}
		}
	}

	/*we don't thread the terminal, ie appart from the audio renderer, media decoding and visual rendering is 
	handled by the app process*/
	m_user.init_flags = GF_TERM_NO_VISUAL_THREAD | GF_TERM_NO_REGULATION;
	m_user.EventProc = GPAC_EventProc;
	m_user.opaque = this;
	m_user.os_window_handler = (void *) &m_window;
	m_user.os_display = (void *) &m_session;

	m_term = gf_term_new(&m_user);
	if (!m_term) {
		MessageBox("Cannot load GPAC terminal", "Fatal Error");
		gf_modules_del(m_user.modules);
		gf_cfg_del(m_user.config);
		User::Leave(KErrGeneral);
	}
	//MessageBox("GPAC terminal loaded", "Success !");

	/*ok set output size*/
	TSize s = m_window.Size();
	gf_term_set_size(m_term, s.iWidth, s.iHeight);


	/*start our callback (every ms)*/
	const TInt KTickInterval = 33000;
	m_pTimer = CPeriodic::NewL(CActive::EPriorityStandard);
	m_pTimer->Start(KTickInterval, KTickInterval, TCallBack(myTick, this));

	opt = gf_cfg_get_key(m_user.config, "General", "StartupFile");
	if (opt) gf_term_connect(m_term, opt);

#endif

}


// -----------------------------------------------------------------------------
// COsmo4AppView::Draw()
// Draws the display.
// -----------------------------------------------------------------------------
//
void COsmo4AppView::Draw( const TRect& /*aRect*/ ) const
{
#ifndef GPAC_GUI_ONLY
	if (!m_term) {
		CWindowGc& gc = SystemGc();
		TRgb black(0,0,0);
		gc.SetBrushColor(black);
		gc.Clear();
	} else {
		/*FIXME - this is just to force a screen flush, needs rework*/
		gf_term_set_option(m_term, GF_OPT_FREEZE_DISPLAY, 0);
	}
#else
		CWindowGc& gc = SystemGc();
		TRgb black(0,0,0);
	    TRect rect = Rect();
		gc.SetBrushColor(black);
		gc.Clear(rect);
#endif
}

void COsmo4AppView::ShowHide(Bool show)
{
#ifndef GPAC_GUI_ONLY
	if (show) {
		MakeVisible(ETrue);
		if (m_term) {
			gf_term_set_option(m_term, GF_OPT_VISIBLE, 1);
			DrawDeferred();
		}
	} else {
		MakeVisible(EFalse);
		if (m_term) gf_term_set_option(m_term, GF_OPT_VISIBLE, 0);
	}
#else
	MakeVisible(ETrue);
#endif
}

// -----------------------------------------------------------------------------
// COsmo4AppView::SizeChanged()
// Called by framework when the view size is changed.
// -----------------------------------------------------------------------------
//
void COsmo4AppView::SizeChanged()
{  
#ifndef GPAC_GUI_ONLY
	if (m_term) {
		TSize s = m_window.Size();
		gf_term_set_size(m_term, s.iWidth, s.iHeight);
	}
#endif
	DrawNow();
}

void COsmo4AppView::Connect(const char *url)
{
#ifndef GPAC_GUI_ONLY
	char the_url[1024];
	/*copy before removing from recent files*/
	strcpy(the_url, url);
	gf_cfg_set_key(m_user.config, "RecentFiles", the_url, NULL);
	gf_cfg_insert_key(m_user.config, "RecentFiles", the_url, "", 0);
	u32 count = gf_cfg_get_key_count(m_user.config, "RecentFiles");
	if (count > 10) gf_cfg_set_key(m_user.config, "RecentFiles", gf_cfg_get_key_name(m_user.config, "RecentFiles", count-1), NULL);

	if (m_term) gf_term_connect(m_term, the_url);
#endif
}


TKeyResponse COsmo4AppView::OfferKeyEventL(const TKeyEvent& aKeyEvent, TEventCode aType)
{
	GF_Event evt;
	u32 ret;

	evt.key.hw_code = aKeyEvent.iScanCode;
	evt.key.flags = 0;
	switch (aType) {
	case EEventKeyUp:
		evt.type = GF_EVENT_KEYUP;
		break;
	case EEventKeyDown:
	case EEventKey:
		evt.type = GF_EVENT_KEYDOWN;
		break;
	default:
		return EKeyWasNotConsumed;
	}

	switch (aKeyEvent.iCode) {
	case EKeyLeftArrow: evt.key.key_code = GF_KEY_LEFT; break;
	case EKeyRightArrow: evt.key.key_code = GF_KEY_RIGHT; break;
	case EKeyUpArrow: evt.key.key_code = GF_KEY_UP; break;
	case EKeyDownArrow: evt.key.key_code = GF_KEY_DOWN; break;
	case EKeyIncVolume: evt.key.key_code = GF_KEY_VOLUMEUP; break;
	case EKeyDecVolume: evt.key.key_code = GF_KEY_VOLUMEDOWN; break;
	default:
		switch (aKeyEvent.iScanCode) {
		case EStdKeyIncVolume: evt.key.key_code = GF_KEY_VOLUMEUP; break;
		case EStdKeyDecVolume: evt.key.key_code = GF_KEY_VOLUMEDOWN; break;
		default:
			return EKeyWasNotConsumed;
		}
	}
#ifndef GPAC_GUI_ONLY
	ret = gf_term_user_event(m_term, &evt);
	/*generate a key up*/
	if (aType==EEventKey) {
		evt.type = GF_EVENT_KEYUP;
		ret += gf_term_user_event(m_term, &evt);
	}
#else
	ret = 0;
#endif
	return ret ? EKeyWasConsumed : EKeyWasNotConsumed;
}

#if defined(__SERIES60_3X__)
void COsmo4AppView::MrccatoCommand(TRemConCoreApiOperationId aOperationId, TRemConCoreApiButtonAction aButtonAct)
{
	GF_Event e;
	switch (aOperationId) {
/*
    TRequestStatus status;
	case ERemConCoreApiPausePlayFunction:
	case ERemConCoreApiStop:
	case ERemConCoreApiRewind:
	case ERemConCoreApiForward:
	case ERemConCoreApiFastForward:
	case ERemConCoreApiBackward:
		switch (aButtonAct) {
		case ERemConCoreApiButtonPress:
			break;
		case ERemConCoreApiButtonRelease:
			break;
		case ERemConCoreApiButtonClick:
			break;
		default:
			break;
		}
*/
	case ERemConCoreApiVolumeUp:
	case ERemConCoreApiVolumeDown:
#ifndef GPAC_GUI_ONLY
		e.key.hw_code = 0;
		e.key.flags = 0;
		e.key.key_code = (aOperationId==ERemConCoreApiVolumeUp) ? GF_KEY_VOLUMEUP : GF_KEY_VOLUMEDOWN;
		switch (aButtonAct) {
		case ERemConCoreApiButtonPress: 
			e.type = GF_EVENT_KEYDOWN; 
			gf_term_user_event(m_term, &e);
			break;
		case ERemConCoreApiButtonRelease: 
			e.type = GF_EVENT_KEYUP; 
			gf_term_user_event(m_term, &e);
			break;
		default: 
			e.type = GF_EVENT_KEYDOWN; 
			gf_term_user_event(m_term, &e);
			e.type = GF_EVENT_KEYUP; 
			gf_term_user_event(m_term, &e);
			break;
		}
#endif
		break;
	default:
		break;
	}
}

#endif