1 /*
2 * Copyright (c) 2004-2007 Marcus Overhagen <marcus@overhagen.de>
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify,
8 * merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 #include <stdio.h>
26 #include <string.h>
27 #include <Debug.h>
28 #include <ParameterWeb.h>
29 #include <TimeSource.h>
30
31 #include "Controller.h"
32 #include "DeviceRoster.h"
33 #include "VideoView.h"
34 #include "VideoNode.h"
35
36 extern bool gOverlayDisabled;
37
38 status_t MediaRoster_Disconnect(const media_output &output, const media_input &input);
39
40
41
42 media_node dvb_node;
43 media_node audio_mixer_node;
44 media_node video_window_node;
45 media_node time_node;
46
47 media_input audio_input;
48 media_output audio_output;
49 media_input video_input;
50 media_output video_output;
51
52 BMediaRoster *gMediaRoster;
53
54 void
HandleError(const char * text,status_t err)55 HandleError(const char *text, status_t err)
56 {
57 if (err != B_OK) {
58 printf("%s. error 0x%08x (%s)\n",text, (int)err, strerror(err));
59 fflush(NULL);
60 exit(1);
61 }
62 }
63
64
Controller()65 Controller::Controller()
66 : fCurrentInterface(-1)
67 , fCurrentChannel(-1)
68 , fVideoView(NULL)
69 , fVideoNode(NULL)
70 , fWeb(NULL)
71 , fChannelParam(NULL)
72 , fConnected(false)
73 , fInput()
74 , fOutput()
75 {
76 gMediaRoster = BMediaRoster::Roster();
77 }
78
79
~Controller()80 Controller::~Controller()
81 {
82 delete fWeb;
83 }
84
85
86 void
SetVideoView(VideoView * view)87 Controller::SetVideoView(VideoView *view)
88 {
89 fVideoView = view;
90 }
91
92
93 void
SetVideoNode(VideoNode * node)94 Controller::SetVideoNode(VideoNode *node)
95 {
96 fVideoNode = node;
97 }
98
99
100 void
DisconnectInterface()101 Controller::DisconnectInterface()
102 {
103 DisconnectNodes();
104 fCurrentInterface = -1;
105 fCurrentChannel = -1;
106 delete fWeb;
107 fWeb = 0;
108 fChannelParam = 0;
109 }
110
111
112 status_t
ConnectInterface(int i)113 Controller::ConnectInterface(int i)
114 {
115 if (i < 0) {
116 printf("Controller::ConnectInterface: wrong index\n");
117 return B_ERROR;
118 }
119 if (fCurrentInterface != -1) {
120 printf("Controller::ConnectInterface: already connected\n");
121 return B_ERROR;
122 }
123
124 BParameterWeb *web;
125 status_t err;
126
127 err = gDeviceRoster->MediaRoster()->GetParameterWebFor(gDeviceRoster->DeviceNode(i), &web);
128 if (err != B_OK) {
129 printf("Controller::ConnectInterface: can't get parameter web\n");
130 return B_ERROR;
131 }
132
133 delete fWeb;
134 fWeb = web;
135 fCurrentInterface = i;
136
137 // XXX we may need to monitor for parameter web changes
138 // and reassing fWeb and fChannelParam on demand.
139
140 // find the channel control and assign it to fChannelParam
141 fChannelParam = NULL;
142 int count = fWeb->CountParameters();
143 for (int i = 0; i < count; i++) {
144 BParameter *parameter = fWeb->ParameterAt(i);
145
146 printf("parameter %d\n", i);
147 printf(" name '%s'\n", parameter->Name());
148 printf(" kind '%s'\n", parameter->Kind());
149 printf(" unit '%s'\n", parameter->Unit());
150 printf(" flags 0x%08" B_PRIx32 "\n", parameter->Flags());
151
152 // XXX TODO: matching on Name is weak
153 if (strcmp(parameter->Name(), "Channel") == 0 || strcmp(parameter->Kind(), B_TUNER_CHANNEL) == 0) {
154 fChannelParam = dynamic_cast<BDiscreteParameter *>(parameter);
155 if (fChannelParam)
156 break;
157 }
158 }
159 if (!fChannelParam) {
160 printf("Controller::ConnectInterface: can't find channel parameter control\n");
161 fCurrentChannel = -1;
162 } else {
163 if (fChannelParam->CountItems() == 0) {
164 fCurrentChannel = -1;
165 printf("Controller::ConnectInterface: channel control has 0 items\n");
166 } else {
167 int32 index;
168 size_t size;
169 status_t err;
170 bigtime_t when;
171 size = sizeof(index);
172 err = fChannelParam->GetValue(&index, &size, &when);
173 if (err == B_OK && size == sizeof(index)) {
174 fCurrentChannel = index;
175 printf("Controller::ConnectInterface: selected channel is %d\n", fCurrentChannel);
176 } else {
177 fCurrentChannel = -1;
178 printf("Controller::ConnectInterface: can't get channel control value\n");
179 }
180 }
181 }
182
183 ConnectNodes();
184
185 return B_OK;
186 }
187
188
189 bool
IsInterfaceAvailable(int i)190 Controller::IsInterfaceAvailable(int i)
191 {
192 return gDeviceRoster->IsRawAudioOutputFree(i) && gDeviceRoster->IsRawVideoOutputFree(i);
193 }
194
195
196 int
CurrentInterface()197 Controller::CurrentInterface()
198 {
199 return fCurrentInterface;
200 }
201
202
203 int
CurrentChannel()204 Controller::CurrentChannel()
205 {
206 return fCurrentChannel;
207 }
208
209
210 status_t
SelectChannel(int i)211 Controller::SelectChannel(int i)
212 {
213 if (!fChannelParam)
214 return B_ERROR;
215
216 int32 index;
217 status_t err;
218 index = i;
219 err = fChannelParam->SetValue(&index, sizeof(index), 0);
220 if (err != B_OK) {
221 printf("Controller::SelectChannel %d failed\n", i);
222 return err;
223 }
224
225 fCurrentChannel = i;
226 return B_OK;
227 }
228
229
230 int
ChannelCount()231 Controller::ChannelCount()
232 {
233 if (fCurrentInterface == -1)
234 return 0;
235
236 if (!fChannelParam)
237 return 0;
238
239 return fChannelParam->CountItems();
240 }
241
242
243 const char *
ChannelName(int i)244 Controller::ChannelName(int i)
245 {
246 if (fCurrentInterface == -1)
247 return NULL;
248
249 if (!fChannelParam)
250 return NULL;
251
252 return fChannelParam->ItemNameAt(i);
253 }
254
255
256 void
VolumeUp()257 Controller::VolumeUp()
258 {
259 }
260
261
262 void
VolumeDown()263 Controller::VolumeDown()
264 {
265 }
266
267
268 status_t
ConnectNodes()269 Controller::ConnectNodes()
270 {
271 status_t err;
272
273 // dvb_node = gDeviceRoster->DeviceNode(fCurrentInterface);
274
275 err = gMediaRoster->GetNodeFor(gDeviceRoster->DeviceNode(fCurrentInterface).node, &dvb_node);
276 HandleError("GetNodeFor failed", err);
277
278 video_window_node = fVideoNode->Node();
279
280 err = gMediaRoster->GetAudioMixer(&audio_mixer_node);
281 HandleError("GetAudioMixer failed", err);
282
283 media_input input;
284 media_output output;
285 media_format fmt;
286 int32 count;
287
288 // Connect audio
289
290 err = gMediaRoster->GetFreeOutputsFor(dvb_node, &output, 1, &count, B_MEDIA_RAW_AUDIO);
291 HandleError("Can't find free audio output", err);
292 if (count < 1)
293 HandleError("No free audio output", -1);
294
295 err = gMediaRoster->GetFreeInputsFor(audio_mixer_node, &input, 1, &count, B_MEDIA_RAW_AUDIO);
296 HandleError("Can't find free audio input", err);
297 if (count < 1)
298 HandleError("No free audio input", -1);
299
300 err = gMediaRoster->Connect(output.source, input.destination, &fmt, &audio_output, &audio_input);
301 HandleError("Can't connect audio", err);
302
303 // Connect video
304
305 err = gMediaRoster->GetFreeOutputsFor(dvb_node, &output, 1, &count, B_MEDIA_RAW_VIDEO);
306 HandleError("Can't find free video output", err);
307 if (count < 1)
308 HandleError("No free video output", -1);
309
310 err = gMediaRoster->GetFreeInputsFor(video_window_node, &input, 1, &count, B_MEDIA_RAW_VIDEO);
311 HandleError("Can't find free video input", err);
312 if (count < 1)
313 HandleError("No free video input", -1);
314
315 color_space cspaces_overlay[] = { B_YCbCr422, B_RGB32, B_NO_COLOR_SPACE };
316 color_space cspaces_bitmap[] = { B_RGB32, B_NO_COLOR_SPACE };
317
318 if (gOverlayDisabled) {
319 err = B_ERROR;
320 } else {
321 fVideoNode->SetOverlayEnabled(true);
322 for (int i = 0; cspaces_overlay[i] != B_NO_COLOR_SPACE; i++) {
323 printf("trying connect with colorspace 0x%08x\n", cspaces_overlay[i]);
324 fmt.Clear();
325 fmt.type = B_MEDIA_RAW_VIDEO;
326 fmt.u.raw_video.display.format = cspaces_overlay[i];
327 err = gMediaRoster->Connect(output.source, input.destination, &fmt, &video_output, &video_input);
328 if (err == B_OK)
329 break;
330 }
331 }
332 if (err) {
333 fVideoNode->SetOverlayEnabled(false);
334 for (int i = 0; cspaces_bitmap[i] != B_NO_COLOR_SPACE; i++) {
335 printf("trying connect with colorspace 0x%08x\n", cspaces_bitmap[i]);
336 fmt.Clear();
337 fmt.type = B_MEDIA_RAW_VIDEO;
338 fmt.u.raw_video.display.format = cspaces_bitmap[i];
339 err = gMediaRoster->Connect(output.source, input.destination, &fmt, &video_output, &video_input);
340 if (err == B_OK)
341 break;
342 }
343 }
344 HandleError("Can't connect video", err);
345
346 // set time sources
347
348 err = gMediaRoster->GetTimeSource(&time_node);
349 HandleError("Can't get time source", err);
350
351 BTimeSource *ts = gMediaRoster->MakeTimeSourceFor(time_node);
352
353 err = gMediaRoster->SetTimeSourceFor(dvb_node.node, time_node.node);
354 HandleError("Can't set dvb time source", err);
355
356 err = gMediaRoster->SetTimeSourceFor(audio_mixer_node.node, time_node.node);
357 HandleError("Can't set audio mixer time source", err);
358
359 err = gMediaRoster->SetTimeSourceFor(video_window_node.node, time_node.node);
360 HandleError("Can't set video window time source", err);
361
362 // Add a delay of (2 video frames) to the buffers send by the DVB node,
363 // because as a capture device in B_RECORDING run mode it's supposed to
364 // deliver buffers that were captured in the past (and does so).
365 // It is important that the audio buffer size used by the connection with
366 // the DVB node is smaller than this, optimum is the same length as one
367 // video frame (40 ms). However, this is not yet guaranteed.
368 err = gMediaRoster->SetProducerRunModeDelay(dvb_node, 80000);
369 HandleError("Can't set DVB producer delay", err);
370
371 bigtime_t start_time = ts->Now() + 50000;
372
373 ts->Release();
374
375 // start nodes
376
377 err = gMediaRoster->StartNode(dvb_node, start_time);
378 HandleError("Can't start dvb node", err);
379
380 err = gMediaRoster->StartNode(audio_mixer_node, start_time);
381 HandleError("Can't start audio mixer node", err);
382
383 err = gMediaRoster->StartNode(video_window_node, start_time);
384 HandleError("Can't start video window node", err);
385
386 printf("running...\n");
387
388 fConnected = true;
389
390 return B_OK;
391 }
392
393
394 status_t
DisconnectNodes()395 Controller::DisconnectNodes()
396 {
397 printf("stopping...\n");
398
399 if (!fConnected)
400 return B_OK;
401
402 status_t err;
403
404 // stop nodes
405
406 err = gMediaRoster->StopNode(dvb_node, 0, true);
407 HandleError("Can't stop dvb node", err);
408
409 err = gMediaRoster->StopNode(audio_mixer_node, 0, true);
410 HandleError("Can't stop audio mixer node", err);
411
412 err = gMediaRoster->StopNode(video_window_node, 0, true);
413 HandleError("Can't stop video window node", err);
414
415 // disconnect nodes
416
417 err = MediaRoster_Disconnect(video_output, video_input);
418 HandleError("Can't disconnect video", err);
419
420 err = MediaRoster_Disconnect(audio_output, audio_input);
421 HandleError("Can't disconnect audio", err);
422
423 // disable overlay, or erase image
424
425 fVideoView->RemoveVideoDisplay();
426
427 // release other nodes
428
429 err = gMediaRoster->ReleaseNode(audio_mixer_node);
430 HandleError("Can't release audio mixer node", err);
431
432 // err = gMediaRoster->ReleaseNode(video_window_node);
433 // HandleError("Can't release video window node", err);
434
435 // err = gMediaRoster->ReleaseNode(time_node);
436 // HandleError("Can't release time source node", err);
437
438 // release dvb
439
440 err = gMediaRoster->ReleaseNode(dvb_node);
441 HandleError("Can't release DVB node", err);
442
443 fConnected = false;
444
445 return B_OK;
446 }
447
448
449 status_t
MediaRoster_Disconnect(const media_output & output,const media_input & input)450 MediaRoster_Disconnect(const media_output &output, const media_input &input)
451 {
452 if (output.node.node <= 0) {
453 printf("MediaRoster_Disconnect: output.node.node %d invalid\n",
454 (int)output.node.node);
455 return B_MEDIA_BAD_NODE;
456 }
457 if (input.node.node <= 0) {
458 printf("MediaRoster_Disconnect: input.node.node %d invalid\n",
459 (int)input.node.node);
460 return B_MEDIA_BAD_NODE;
461 }
462 if (!(output.node.kind & B_BUFFER_PRODUCER)) {
463 printf("MediaRoster_Disconnect: output.node.kind 0x%x is no B_BUFFER_PRODUCER\n",
464 (int)output.node.kind);
465 return B_MEDIA_BAD_NODE;
466 }
467 if (!(input.node.kind & B_BUFFER_CONSUMER)) {
468 printf("MediaRoster_Disconnect: input.node.kind 0x%x is no B_BUFFER_PRODUCER\n",
469 (int)input.node.kind);
470 return B_MEDIA_BAD_NODE;
471 }
472 if (input.source.port != output.source.port) {
473 printf("MediaRoster_Disconnect: input.source.port %d doesn't match output.source.port %d\n",
474 (int)input.source.port, (int)output.source.port);
475 return B_MEDIA_BAD_NODE;
476 }
477 if (input.source.id != output.source.id) {
478 printf("MediaRoster_Disconnect: input.source.id %d doesn't match output.source.id %d\n",
479 (int)input.source.id, (int)output.source.id);
480 return B_MEDIA_BAD_NODE;
481 }
482 if (input.destination.port != output.destination.port) {
483 printf("MediaRoster_Disconnect: input.destination.port %d doesn't match output.destination.port %d\n",
484 (int)input.destination.port, (int)output.destination.port);
485 return B_MEDIA_BAD_NODE;
486 }
487 if (input.destination.id != output.destination.id) {
488 printf("MediaRoster_Disconnect: input.destination.id %d doesn't match output.destination.id %d\n",
489 (int)input.destination.id, (int)output.destination.id);
490 return B_MEDIA_BAD_NODE;
491 }
492 return BMediaRoster::Roster()->Disconnect(output.node.node, output.source, input.node.node, input.destination);
493 }
494