1 /*---------------------------------------------------------------------------*
2 Project: Horizon
3 File: FsSampleStreaming.cpp
4
5 Copyright (C)2009-2012 Nintendo Co., Ltd. All rights reserved.
6
7 These coded instructions, statements, and computer programs contain
8 proprietary information of Nintendo of America Inc. and/or Nintendo
9 Company Ltd., and are protected by Federal copyright law. They may
10 not be disclosed to third parties or copied or duplicated in any form,
11 in whole or in part, without the prior written consent of Nintendo.
12
13 $Rev: 46404 $
14 *---------------------------------------------------------------------------*/
15
16 #include <nn.h>
17 #include "demo.h"
18
19 #include "../Common/FsSampleCommon.h"
20 #include "FsSampleStreaming.h"
21
22 using namespace nn;
23
24 /*
25 * Overview
26 *
27 * This sample illustrates an implementation for file access that demands realtime characteristics, such as for streaming playback of video or audio.
28 *
29 *
30 * This sample demonstrates real-time streaming. It parses drawings made on the touch panel as raw binary bitmaps, and saves and plays them frame by frame.
31 *
32 *
33 */
34
35 /*
36 * Basic Flow
37 *
38 * Opens FileStream, and sets nn::fs::PRIORITY_APP_REALTIME using the TrySetPriority function.
39 * Prepares one thread with a higher priority than the main thread, and performs reading/writing of an open file in that thread.
40 *
41 * Note that the content of the buffer allocated for writing should not be changed during the write.
42 */
43
44 /*
45 * Implementing this sample
46 *
47 * All of the above basic flow is performed with RealTimeFileReader and RealTimeFileWriter.
48 * With this class, to receive or send data via nn::os::BlockingQueue, the main thread does not pay attention to the behavior of the read/write thread.
49 *
50 *
51 * On the main thread side, prepared packets are received from BlockingQueue, and when the packet process is finished in the main thread, processed packets are packed into the BlockingQueue for returns.
52 *
53 *
54 *
55 * On the read/write thread side, when a sufficient amount of buffers have been packed into Queue, data is read/written in batch.
56 *
57 */
58
59 namespace sample { namespace fs { namespace streaming {
60
61 // Saves data in the background.
BgSave(const s32 data)62 Result BgSave(const s32 data)
63 {
64 // When mounting an archive, nn::fs::PRIORITY_APP_NORMAL is applied if no priority has been specified in particular.
65 RETURN_IF_FAILED(nn::fs::MountSaveData(SAVE_DATA_ARCHIVE));
66
67 SaveData allData;
68 allData.data[0] = data;
69 {
70 // When opening a file, the archive priority is inherited if no priority has been specified in particular.
71 nn::fs::FileOutputStream file(DATA_FILE_NAME, false);
72 file.Write(&allData, sizeof(allData), true);
73 }
74
75 Result result = nn::fs::CommitSaveData(SAVE_DATA_ARCHIVE);
76
77 nn::fs::Unmount(SAVE_DATA_ARCHIVE);
78
79 return result;
80 }
81
82 // Load the data in the background.
BgLoad(s32 & data)83 Result BgLoad(s32 &data)
84 {
85 // When mounting an archive, nn::fs::PRIORITY_APP_NORMAL is applied if no priority has been specified in particular.
86 RETURN_IF_FAILED(nn::fs::MountSaveData(SAVE_DATA_ARCHIVE));
87
88 SaveData allData;
89 {
90 // When opening a file, the archive priority is inherited if no priority has been specified in particular.
91 nn::fs::FileInputStream file(DATA_FILE_NAME);
92 file.Read(&allData, sizeof(allData));
93 }
94
95 nn::fs::Unmount(SAVE_DATA_ARCHIVE);
96
97 data = allData.data[0];
98 return ResultSuccess();
99 }
100
101 }}}
102