forked from jackburton79/bescreencapture
/
OutputView.cpp
433 lines (349 loc) · 10.5 KB
/
OutputView.cpp
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
#include "ControllerObserver.h"
#include "messages.h"
#include "OutputView.h"
#include "PreviewView.h"
#include "Settings.h"
#include "Utils.h"
#include <Alignment.h>
#include <Bitmap.h>
#include <Box.h>
#include <Button.h>
#include <CheckBox.h>
#include <GridLayoutBuilder.h>
#include <GroupLayoutBuilder.h>
#include <LayoutBuilder.h>
#include <LayoutUtils.h>
#include <MediaDefs.h>
#include <MediaFormats.h>
#include <MenuField.h>
#include <MenuItem.h>
#include <OptionPopUp.h>
#include <PopUpMenu.h>
#include <Path.h>
#include <RadioButton.h>
#include <Screen.h>
#include <SplitView.h>
#include <SplitLayoutBuilder.h>
#include <String.h>
#include <StringView.h>
#include <TextControl.h>
#include <Window.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>
const static int32 kCheckBoxAreaSelectionChanged = 'CaCh';
const static int32 kFileTypeChanged = 'FtyC';
const static int32 kCodecChanged = 'CdCh';
const static char *kCodecData = "Codec";
OutputView::OutputView(Controller *controller)
:
BView("Capture Options", B_WILL_DRAW),
fController(controller)
{
SetLayout(new BGroupLayout(B_VERTICAL));
BBox *selectBox = new BBox("selection");
selectBox->SetLabel("Selection");
selectBox->SetExplicitAlignment(BAlignment(B_ALIGN_LEFT, B_ALIGN_TOP));
AddChild(selectBox);
BBox *outputBox = new BBox("output");
outputBox->SetLabel("Output");
outputBox->SetExplicitAlignment(BAlignment(B_ALIGN_LEFT, B_ALIGN_TOP));
AddChild(outputBox);
Settings settings;
const char *kTCLabel = "File name:";
const char *fileName = NULL;
settings.GetOutputFileName(&fileName);
fFileName = new BTextControl("file name",
kTCLabel, fileName, new BMessage(kFileNameChanged));
const char *kOutputMenuLabel = "Output Format:";
fOutputFileType = new BOptionPopUp("OutFormat",
kOutputMenuLabel, new BMessage(kFileTypeChanged));
const char *kCodecMenuLabel = "Codec:";
BPopUpMenu *popUpMenu = new BPopUpMenu("Codecs");
fCodecMenu = new BMenuField("OutCodec", kCodecMenuLabel, popUpMenu);
fWholeScreen = new BRadioButton("screen frame", "Whole screen",
new BMessage(kCheckBoxAreaSelectionChanged));
fCustomArea = new BRadioButton("custom area",
"Custom Area", new BMessage(kCheckBoxAreaSelectionChanged));
fSelectArea = new BButton("select area", "Select", new BMessage(kSelectArea));
fSelectArea->SetEnabled(false);
fMinimizeOnStart = new BCheckBox("Minimize on start",
"Minimize on recording", new BMessage(kMinimizeOnRecording));
fRectView = new PreviewView();
BView *layoutView = BLayoutBuilder::Group<>()
.SetInsets(B_USE_DEFAULT_SPACING, B_USE_DEFAULT_SPACING,
B_USE_DEFAULT_SPACING, B_USE_DEFAULT_SPACING)
.AddGroup(B_VERTICAL, B_USE_DEFAULT_SPACING)
.Add(fFileName)
.Add(fOutputFileType)
.Add(fCodecMenu)
.Add(fMinimizeOnStart)
.End()
.View();
outputBox->AddChild(layoutView);
layoutView = BLayoutBuilder::Group<>()
.SetInsets(B_USE_DEFAULT_SPACING, B_USE_DEFAULT_SPACING,
B_USE_DEFAULT_SPACING, B_USE_DEFAULT_SPACING)
.AddGroup(B_VERTICAL)
.AddGroup(B_HORIZONTAL)
.AddGroup(B_VERTICAL, 0)
.Add(fWholeScreen)
.Add(fCustomArea)
.End()
.AddGroup(B_VERTICAL)
.AddGlue()
.Add(fSelectArea)
.End()
.End()
.Add(fRectView)
.End()
.View();
selectBox->AddChild(layoutView);
fMinimizeOnStart->SetValue(settings.MinimizeOnRecording() ? B_CONTROL_ON : B_CONTROL_OFF);
// fill in the list of available file formats
media_file_format mff;
int32 cookie = 0;
bool firstFound = true;
while (get_next_file_format(&cookie, &mff) == B_OK) {
if (mff.capabilities & media_file_format::B_KNOWS_ENCODED_VIDEO) {
fOutputFileType->AddOption(mff.pretty_name, mff.family);
if (firstFound) {
fOutputFileType->MenuField()->Menu()->ItemAt(0)->SetMarked(true);
firstFound = false;
}
}
}
fWholeScreen->SetValue(B_CONTROL_ON);
UpdateSettings();
fController->SetCaptureArea(BScreen(Window()).Frame());
fController->SetMediaFormatFamily(FormatFamily());
fController->SetOutputFileName(fFileName->Text());
}
void
OutputView::AttachedToWindow()
{
BView::AttachedToWindow();
SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
fMinimizeOnStart->SetTarget(this);
fFileName->SetTarget(this);
fOutputFileType->SetTarget(this);
fCustomArea->SetTarget(this);
fWholeScreen->SetTarget(this);
UpdatePreviewFromSettings();
}
void
OutputView::MessageReceived(BMessage *message)
{
switch (message->what) {
case kCheckBoxAreaSelectionChanged:
_UpdatePreview(NULL);
break;
case kMsgControllerVideoDepthChanged:
UpdateSettings();
break;
case kRebuildCodec:
case kFileTypeChanged:
fController->SetMediaFormatFamily(FormatFamily());
UpdateSettings();
break;
case kFileNameChanged:
fController->SetOutputFileName(fFileName->Text());
break;
case kMinimizeOnRecording:
Settings().SetMinimizeOnRecording(fMinimizeOnStart->Value() == B_CONTROL_ON);
break;
case kCodecChanged:
{
media_codec_info *info;
ssize_t size;
if (message->FindData(kCodecData, B_SIMPLE_DATA,
(const void **)&info, &size) == B_OK)
fController->SetMediaCodecInfo(*info);
break;
}
case B_OBSERVER_NOTICE_CHANGE:
{
int32 code;
message->FindInt32("be:observe_change_what", &code);
switch (code) {
case kSelectionWindowClosed:
case kMsgControllerTargetFrameChanged:
case kClipSizeChanged:
_UpdatePreview(message);
break;
default:
break;
}
break;
}
default:
BView::MessageReceived(message);
break;
}
}
bool
OutputView::MinimizeOnStart() const
{
return fMinimizeOnStart->Value() == B_CONTROL_ON;
}
void
OutputView::UpdateSettings()
{
Settings settings;
if (fWholeScreen->Value() == B_CONTROL_ON)
settings.SetCaptureArea(BScreen().Frame());
//BRect captureRect = settings.CaptureArea();
//const float factor = 100;
// TODO: set the frame
//settings.SetClipFrame(GetScaledRect(captureRect, factor));
UpdatePreviewFromSettings();
BuildCodecMenu(FormatFamily());
fController->SetMediaFormat(fFormat);
}
BPath
OutputView::OutputFileName() const
{
BPath path = fFileName->Text();
return path;
}
void
OutputView::UpdatePreviewFromSettings()
{
const BRect rect = Settings().CaptureArea();
fRectView->SetRect(rect);
}
void
OutputView::BuildCodecMenu(const media_format_family &family)
{
Settings settings;
BRect rect = settings.CaptureArea();//ClipFrame();
rect.right++;
rect.bottom++;
GetCodecsForFamily(family, (const int32 &)rect.IntegerWidth(),
(const int32 &)rect.IntegerHeight(), fCodecMenu->Menu(), fFormat);
// Make the app object the menu's message target
fCodecMenu->Menu()->SetTargetForItems(this);
}
status_t
OutputView::GetCodecsForFamily(const media_format_family &family,
const int32 &width, const int32 &height,
BMenu *codecs, media_format &initialFormat)
{
SetInitialFormat(width, height, Settings().ClipDepth(),
10, initialFormat);
// find the full media_file_format corresponding to
// the given format family (e.g. AVI)
media_file_format fileFormat;
if (!GetMediaFileFormat(family, fileFormat))
return B_ERROR;
BString currentCodec;
BMenuItem *marked = codecs->FindMarked();
if (marked != NULL)
currentCodec = marked->Label();
// suspend updates while we're rebuilding this menu in order to
// reduce window flicker and other annoyances
Window()->BeginViewTransaction();
codecs->RemoveItems(0, codecs->CountItems(), true);
int32 cookie = 0;
media_codec_info codec;
media_format dummyFormat;
while (get_next_encoder(&cookie, &fileFormat, &initialFormat,
&dummyFormat, &codec) == B_OK) {
BMenuItem *item = CreateCodecMenuItem(codec);
if (item != NULL)
codecs->AddItem(item);
if (codec.pretty_name == currentCodec)
item->SetMarked(true);
}
if (codecs->FindMarked() == NULL) {
BMenuItem *item = codecs->ItemAt(0);
if (item != NULL)
item->SetMarked(true);
}
Window()->EndViewTransaction();
marked = codecs->FindMarked();
BMessage *message = marked->Message();
media_codec_info *info;
ssize_t size;
if (message->FindData(kCodecData, B_SIMPLE_DATA,
(const void **)&info, &size) == B_OK)
fController->SetMediaCodecInfo(*info);
return B_OK;
}
/* virtual */
BSize
OutputView::MinSize()
{
return BLayoutUtils::ComposeSize(ExplicitMinSize(), BSize(620, 300));
}
media_format_family
OutputView::FormatFamily() const
{
return (media_format_family)fOutputFileType->Value();
}
void
OutputView::_UpdatePreview(BMessage* message)
{
fSelectArea->SetEnabled(fCustomArea->Value() == B_CONTROL_ON);
BRect screenFrame = BScreen(Window()).Frame();
Settings settings;
//BRect captureArea;
//settings.GetCaptureArea(captureArea);
//if (captureArea == screenFrame)
// return;
if (fWholeScreen->Value() == B_CONTROL_ON)
settings.SetCaptureArea(screenFrame);
//UpdatePreview();
if (message != NULL) {
BRect rect;
message->FindRect("selection", &rect);
fRectView->SetRect(rect);
BBitmap* bitmap = NULL;
if (message->FindPointer("bitmap", (void**)&bitmap) == B_OK) {
fRectView->UpdateBitmap(bitmap);
delete bitmap;
}
} else {
BRect captureArea;
settings.GetCaptureArea(captureArea);
fRectView->SetRect(captureArea);
fRectView->UpdateBitmap(NULL);
}
// the size of the destination
// clip maybe isn't supported by the codec
UpdateSettings();
}
/* static */
void
OutputView::SetInitialFormat(const int32 &width, const int32 &height,
const color_space &colorSpace, const int32 &fieldRate,
media_format &initialFormat)
{
memset(&initialFormat, 0, sizeof(media_format));
initialFormat.type = B_MEDIA_RAW_VIDEO;
initialFormat.u.raw_video.display.line_width = width;
initialFormat.u.raw_video.display.line_count = height;
initialFormat.u.raw_video.last_active = initialFormat.u.raw_video.display.line_count - 1;
size_t pixelChunk;
size_t rowAlign;
size_t pixelPerChunk;
get_pixel_size_for(colorSpace, &pixelChunk, &rowAlign, &pixelPerChunk);
initialFormat.u.raw_video.display.bytes_per_row = width * rowAlign;
initialFormat.u.raw_video.display.format = colorSpace;
initialFormat.u.raw_video.interlace = 1;
// TODO: Calculate this in some way
initialFormat.u.raw_video.field_rate = fieldRate; //Frames per second
initialFormat.u.raw_video.pixel_width_aspect = 1; // square pixels
initialFormat.u.raw_video.pixel_height_aspect = 1;
}
/* static */
BMenuItem *
OutputView::CreateCodecMenuItem(const media_codec_info &codec)
{
BMessage *message = new BMessage(kCodecChanged);
if (message == NULL)
return NULL;
message->AddData(kCodecData, B_SIMPLE_DATA, &codec, sizeof(media_codec_info));
BMenuItem *item = new BMenuItem(codec.pretty_name, message);
return item;
}