This repository was archived by the owner on Aug 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathMediaFormatView.cpp
More file actions
308 lines (261 loc) · 8.09 KB
/
MediaFormatView.cpp
File metadata and controls
308 lines (261 loc) · 8.09 KB
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
/*
* Copyright 2017-2023 Stefano Ceccherini <stefano.ceccherini@gmail.com>
* All rights reserved. Distributed under the terms of the MIT license.
*/
#include "MediaFormatView.h"
#include "BSCApp.h"
#include "ControllerObserver.h"
#include "Utils.h"
#include <Button.h>
#include <Catalog.h>
#include <LayoutBuilder.h>
#include <MenuItem.h>
#include <MenuField.h>
#include <PopUpMenu.h>
#include <TextControl.h>
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "MediaFormatView"
const static int32 kLocalCodecChanged = 'CdCh';
const static int32 kLocalFileTypeChanged = 'FtyC';
class MediaFileFormatMenuItem : public BMenuItem {
public:
MediaFileFormatMenuItem(const media_file_format& fileFormat);
media_file_format MediaFileFormat() const;
private:
media_file_format fFileFormat;
};
MediaFormatView::MediaFormatView()
:
BView("media_options", B_WILL_DRAW),
fOutputFileType(NULL),
fCodecMenu(NULL)
{
const char *kOutputMenuLabel = B_TRANSLATE("File format:");
BPopUpMenu *fileFormatPopUp = new BPopUpMenu("format");
fOutputFileType = new BMenuField("outformat",
kOutputMenuLabel, fileFormatPopUp);
const char *kCodecMenuLabel = B_TRANSLATE("Media codec:");
BPopUpMenu *popUpMenu = new BPopUpMenu("codecs");
fCodecMenu = new BMenuField("outcodec", kCodecMenuLabel, popUpMenu);
BLayoutBuilder::Grid<>(this, B_USE_DEFAULT_SPACING, B_USE_DEFAULT_SPACING)
.Add(fOutputFileType->CreateLabelLayoutItem(), 0, 0)
.Add(fOutputFileType->CreateMenuBarLayoutItem(), 1, 0)
.Add(fCodecMenu->CreateLabelLayoutItem(), 0, 1)
.Add(fCodecMenu->CreateMenuBarLayoutItem(), 1, 1)
.AddGlue(0, 3)
.SetInsets(0, 0);
}
MediaFormatView::~MediaFormatView()
{
}
void
MediaFormatView::AttachedToWindow()
{
BView::AttachedToWindow();
SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
BSCApp* app = dynamic_cast<BSCApp*>(be_app);
// Watch for these from Controller
if (be_app->LockLooper()) {
be_app->StartWatching(this, kMsgControllerEncodeStarted);
be_app->StartWatching(this, kMsgControllerEncodeFinished);
be_app->StartWatching(this, kMsgControllerSourceFrameChanged);
be_app->StartWatching(this, kMsgControllerTargetFrameChanged);
be_app->StartWatching(this, kMsgControllerCodecListUpdated);
be_app->StartWatching(this, kMsgControllerMediaFileFormatChanged);
be_app->StartWatching(this, kMsgControllerVideoDepthChanged);
be_app->StartWatching(this, kMsgControllerCodecChanged);
be_app->UnlockLooper();
}
_BuildFileFormatsMenu();
BString currentFileFormat = app->MediaFileFormatName();
BMenuItem* item = NULL;
if (currentFileFormat != "")
item = fOutputFileType->Menu()->FindItem(currentFileFormat.String());
if (item != NULL)
item->SetMarked(true);
else {
// TODO: This means there is no working media encoder.;
// do something smart (like showing an alert and disable
// the "Start" button
// TODO: this should be done at the Controller level
}
fOutputFileType->Menu()->SetTargetForItems(this);
BString codecName = app->MediaCodecName();
if (codecName != "") {
BMenuItem* codecItem = fCodecMenu->Menu()->FindItem(codecName);
if (codecItem != NULL)
codecItem->SetMarked(true);
}
_RebuildCodecsMenu(app->MediaCodecName());
}
void
MediaFormatView::MessageReceived(BMessage *message)
{
BSCApp* app = dynamic_cast<BSCApp*>(be_app);
switch (message->what) {
case kLocalFileTypeChanged:
{
app->SetMediaFileFormat(_MediaFileFormat());
app->SetMediaFormatFamily(_MediaFileFormat().family);
app->UpdateMediaFormatAndCodecsForCurrentFamily();
break;
}
case kLocalCodecChanged:
{
BMenuItem* marked = fCodecMenu->Menu()->FindMarked();
if (marked != NULL)
app->SetMediaCodec(marked->Label());
break;
}
case B_OBSERVER_NOTICE_CHANGE:
{
int32 code;
message->FindInt32(B_OBSERVE_WHAT_CHANGE, &code);
switch (code) {
case kMsgControllerCodecListUpdated:
_RebuildCodecsMenu();
break;
case kMsgControllerMediaFileFormatChanged:
{
const char* formatName = NULL;
message->FindString("format_name", &formatName);
_SelectFileFormatMenuItem(formatName);
break;
}
case kMsgControllerCodecChanged:
{
const char* codecName = NULL;
message->FindString("codec_name", &codecName);
for (int32 i = 0; i < fCodecMenu->Menu()->CountItems(); i++) {
BMenuItem* item = fCodecMenu->Menu()->ItemAt(i);
if (::strcmp(item->Label(), codecName) == 0) {
item->SetMarked(true);
break;
}
}
break;
}
case kMsgControllerVideoDepthChanged:
case kMsgControllerTargetFrameChanged:
app->UpdateMediaFormatAndCodecsForCurrentFamily();
break;
case kMsgControllerEncodeStarted:
fCodecMenu->SetEnabled(false);
fOutputFileType->SetEnabled(false);
break;
case kMsgControllerEncodeFinished:
fCodecMenu->SetEnabled(true);
fOutputFileType->SetEnabled(true);
break;
default:
break;
}
break;
}
default:
BView::MessageReceived(message);
break;
}
}
void
MediaFormatView::_BuildFileFormatsMenu()
{
BMenu* menu = fOutputFileType->Menu();
if (menu == NULL)
return;
const int32 numItems = menu->CountItems();
if (numItems > 0)
menu->RemoveItems(0, numItems);
media_file_format mediaFileFormat;
int32 cookie = 0;
while (get_next_file_format(&cookie, &mediaFileFormat) == B_OK) {
if (IsFileFormatUsable(mediaFileFormat)) {
MediaFileFormatMenuItem* item = new MediaFileFormatMenuItem(
mediaFileFormat);
menu->AddItem(item);
}
}
media_file_format nullFormat;
MakeNULLMediaFileFormat(nullFormat);
MediaFileFormatMenuItem* nullItem = new MediaFileFormatMenuItem(nullFormat);
menu->AddItem(nullItem);
// TODO: Maybe Haiku could support this by enabling this in the ffmpeg addon ?
if (IsFFMPEGAvailable()) {
media_file_format gifFormat;
MakeGIFMediaFileFormat(gifFormat);
MediaFileFormatMenuItem* gifItem = new MediaFileFormatMenuItem(gifFormat);
menu->AddItem(gifItem);
}
}
void
MediaFormatView::_RebuildCodecsMenu(const char* currentCodec)
{
BSCApp* app = dynamic_cast<BSCApp*>(be_app);
BMenu* codecsMenu = fCodecMenu->Menu();
BString currentCodecString;
if (currentCodec != NULL)
currentCodecString = currentCodec;
else {
BMenuItem* item = codecsMenu->FindMarked();
if (item != NULL)
currentCodecString = item->Label();
}
Window()->BeginViewTransaction();
codecsMenu->RemoveItems(0, codecsMenu->CountItems(), true);
media_codec_list codecList;
if (app->GetCodecsList(codecList) == B_OK) {
for (size_t i = 0; i < codecList.size(); i++) {
const media_codec_info& codec = codecList.at(i);
BMenuItem* item = new BMenuItem(codec.pretty_name, new BMessage(kLocalCodecChanged));
codecsMenu->AddItem(item);
if (codec.pretty_name == currentCodecString)
item->SetMarked(true);
}
// Make the app object the menu's message target
fCodecMenu->Menu()->SetTargetForItems(this);
}
if (codecsMenu->FindMarked() == NULL) {
BMenuItem *item = codecsMenu->ItemAt(0);
if (item != NULL)
item->SetMarked(true);
}
Window()->EndViewTransaction();
if (codecsMenu->FindMarked() == NULL) {
codecsMenu->SetEnabled(false);
} else {
if (currentCodecString != codecsMenu->FindMarked()->Label())
app->SetMediaCodec(codecsMenu->FindMarked()->Label());
codecsMenu->SetEnabled(true);
}
}
// convenience method
media_file_format
MediaFormatView::_MediaFileFormat() const
{
MediaFileFormatMenuItem* item = static_cast<MediaFileFormatMenuItem*>(
fOutputFileType->Menu()->FindMarked());
return item->MediaFileFormat();
}
void
MediaFormatView::_SelectFileFormatMenuItem(const char* formatName)
{
if (formatName != NULL) {
for (int32 i = 0; i < fOutputFileType->Menu()->CountItems(); i++) {
MediaFileFormatMenuItem* item = static_cast<MediaFileFormatMenuItem*>(fOutputFileType->Menu()->ItemAt(i));
if (::strcmp(item->MediaFileFormat().pretty_name, formatName) == 0)
item->SetMarked(true);
}
}
}
// MediaFileFormatMenuItem
MediaFileFormatMenuItem::MediaFileFormatMenuItem(const media_file_format& fileFormat)
:
BMenuItem(fileFormat.pretty_name, new BMessage(kLocalFileTypeChanged)),
fFileFormat(fileFormat)
{
}
media_file_format
MediaFileFormatMenuItem::MediaFileFormat() const
{
return fFileFormat;
}