BTRFS: Implement BTree::Path and change _Find.
[haiku.git] / src / apps / tv / VideoNode.cpp
bloba9aaff4f086687bb99f5be553e5c6d1dc6c97265
1 /*
2 * Copyright (c) 2004-2007 Marcus Overhagen <marcus@overhagen.de>
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:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
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.
25 #include <stdio.h>
26 #include <string.h>
27 #include <Window.h>
28 #include <TimeSource.h>
29 #include <MediaRoster.h>
30 #include <BufferGroup.h>
31 #include <Buffer.h>
32 #include <Bitmap.h>
33 #include <Locker.h>
34 #include <Debug.h>
36 #include "VideoNode.h"
37 #include "VideoView.h"
39 void
40 overlay_copy(uint32 lines, void *dst, uint32 dst_bpr, const void *src,
41 uint32 src_bpr)
43 // bigtime_t start = system_time();
44 int len = min_c(dst_bpr, src_bpr);
45 // int len4 = len / 4;
46 while (lines--) {
48 // this does not copy the last few bytes, if length is not aligned
49 // to 4 bytes
50 asm ("rep\n\t""movsl"
52 : "c" (len4), "S" (src), "D" (dst)
53 : "eax");
56 const uint32 *s4 = (const uint32 *)src;
57 uint32 *d4 = (uint32 *)dst;
58 for (int i = 0; i < len4; i++)
59 d4[i] = s4[i];
62 const uint8 *s1 = (const uint8 *)s4;
63 uint8 *d1 = (uint8 *)d4;
64 int l1 = len1;
65 while (l1--)
66 *d1++ = *s1++;
68 memcpy(dst, src, len);
69 src = (char *)src + src_bpr;
70 dst = (char *)dst + dst_bpr;
72 // printf("overlay copy: %.06f sec\n", (system_time() - start) / 1000000.0);
76 VideoNode::VideoNode(const char *name, VideoView *view)
77 : BMediaNode(name)
78 , BMediaEventLooper()
79 , BBufferConsumer(B_MEDIA_RAW_VIDEO)
80 , fVideoView(view)
81 , fInput()
82 , fOverlayEnabled(true)
83 , fOverlayActive(false)
84 , fDirectOverlayBuffer(false)
85 , fBitmap(0)
86 , fBitmapLocker(new BLocker("Video Node Locker"))
91 VideoNode::~VideoNode()
93 Quit();
94 DeleteBuffers();
95 delete fBitmapLocker;
99 BMediaAddOn *
100 VideoNode::AddOn(int32 *internal_id) const
102 *internal_id = 0;
103 return NULL;
107 void
108 VideoNode::NodeRegistered()
110 fInput.node = Node();
111 fInput.source = media_source::null;
112 fInput.destination.port = ControlPort();
113 fInput.destination.id = 0;
114 fInput.format.type = B_MEDIA_RAW_VIDEO;
115 fInput.format.u.raw_video = media_raw_video_format::wildcard;
116 strcpy(fInput.name, "video in");
118 SetPriority(B_DISPLAY_PRIORITY);
119 Run();
123 void
124 VideoNode::BufferReceived(BBuffer * buffer)
126 if (RunState() != B_STARTED) {
127 buffer->Recycle();
128 return;
130 if (fOverlayActive && fDirectOverlayBuffer) {
131 HandleBuffer(buffer);
132 } else {
133 media_timed_event event(buffer->Header()->start_time,
134 BTimedEventQueue::B_HANDLE_BUFFER,
135 buffer,
136 BTimedEventQueue::B_RECYCLE_BUFFER);
137 EventQueue()->AddEvent(event);
142 status_t
143 VideoNode::GetNextInput(int32 *cookie, media_input *out_input)
145 if (*cookie < 1) {
146 *out_input = fInput;
147 *cookie += 1;
148 return B_OK;
150 return B_ERROR;
154 void
155 VideoNode::DisposeInputCookie(int32 cookie)
157 // nothing to do
161 status_t
162 VideoNode:: HandleMessage(int32 message,
163 const void *data,
164 size_t size)
166 if (BBufferConsumer::HandleMessage(message, data, size) == B_OK
167 || BMediaEventLooper::HandleMessage(message, data, size) == B_OK)
168 return B_OK;
170 return B_ERROR;
174 void
175 VideoNode::HandleEvent(const media_timed_event *event,
176 bigtime_t lateness,
177 bool realTimeEvent)
179 switch (event->type) {
180 case BTimedEventQueue::B_START:
181 break;
182 case BTimedEventQueue::B_STOP:
183 EventQueue()->FlushEvents(event->event_time,
184 BTimedEventQueue::B_ALWAYS, true,
185 BTimedEventQueue::B_HANDLE_BUFFER);
186 break;
187 case BTimedEventQueue::B_HANDLE_BUFFER:
188 HandleBuffer((BBuffer *)event->pointer);
189 break;
190 default:
191 printf("VideoNode::HandleEvent unknown event");
192 break;
197 void
198 VideoNode::ProducerDataStatus(const media_destination &dst,
199 int32 status,
200 bigtime_t at_media_time)
202 // do nothing
206 status_t
207 VideoNode::GetLatencyFor(const media_destination &dst,
208 bigtime_t *out_latency,
209 media_node_id *out_id)
211 if (dst != fInput.destination)
212 return B_MEDIA_BAD_DESTINATION;
214 *out_latency = 10000;
215 *out_id = TimeSource()->ID();
216 return B_OK;
219 status_t
220 VideoNode::AcceptFormat(const media_destination &dst,
221 media_format *format)
223 /* The connection process:
224 * BBufferProducer::FormatProposal
225 * we are here => BBufferConsumer::AcceptFormat
226 * BBufferProducer::PrepareToConnect
227 * BBufferConsumer::Connected
228 * BBufferProducer::Connect
231 if (dst != fInput.destination)
232 return B_MEDIA_BAD_DESTINATION;
234 if (format->type == B_MEDIA_NO_TYPE)
235 format->type = B_MEDIA_RAW_VIDEO;
237 if (format->type != B_MEDIA_RAW_VIDEO)
238 return B_MEDIA_BAD_FORMAT;
241 return B_OK;
245 status_t
246 VideoNode::Connected(const media_source &src,
247 const media_destination &dst,
248 const media_format &format,
249 media_input *out_input)
251 /* The connection process:
252 * BBufferProducer::FormatProposal
253 * BBufferConsumer::AcceptFormat
254 * BBufferProducer::PrepareToConnect
255 * we are here => BBufferConsumer::Connected
256 * BBufferProducer::Connect
259 if (dst != fInput.destination)
260 return B_MEDIA_BAD_DESTINATION;
262 fInput.source = src;
263 fInput.format = format;
265 if (fInput.format.u.raw_video.field_rate < 1.0)
266 fInput.format.u.raw_video.field_rate = 25.0;
268 color_space colorspace = format.u.raw_video.display.format;
269 BRect frame(0, 0, format.u.raw_video.display.line_width - 1,
270 format.u.raw_video.display.line_count - 1);
271 status_t err;
273 DeleteBuffers();
274 err = CreateBuffers(frame, colorspace, fOverlayEnabled);
275 if (err) {
276 printf("VideoNode::Connected failed, fOverlayEnabled = %d\n",
277 fOverlayEnabled);
278 return err;
281 *out_input = fInput;
283 return B_OK;
288 void
289 VideoNode::Disconnected(const media_source &src,
290 const media_destination &dst)
292 if (src != fInput.source)
293 return;
294 if (dst != fInput.destination)
295 return;
297 DeleteBuffers();
299 // disconnect the connection
300 fInput.source = media_source::null;
304 status_t
305 VideoNode::FormatChanged(const media_source &src,
306 const media_destination &dst,
307 int32 from_change_count,
308 const media_format &format)
310 printf("VideoNode::FormatChanged enter\n");
311 if (src != fInput.source)
312 return B_MEDIA_BAD_SOURCE;
313 if (dst != fInput.destination)
314 return B_MEDIA_BAD_DESTINATION;
316 color_space colorspace = format.u.raw_video.display.format;
317 BRect frame(0, 0, format.u.raw_video.display.line_width - 1,
318 format.u.raw_video.display.line_count - 1);
319 status_t err;
321 DeleteBuffers();
322 if (fOverlayEnabled) {
323 fVideoView->RemoveOverlay();
324 err = CreateBuffers(frame, colorspace, true); // try overlay
325 if (err) {
326 printf("VideoNode::FormatChanged creating overlay buffer "
327 "failed\n");
328 err = CreateBuffers(frame, colorspace, false); // no overlay
330 } else {
331 err = CreateBuffers(frame, colorspace, false); // no overlay
333 if (err) {
334 printf("VideoNode::FormatChanged failed (lost buffer group!)\n");
335 return B_MEDIA_BAD_FORMAT;
338 fInput.format = format;
340 printf("VideoNode::FormatChanged leave\n");
341 return B_OK;
345 void
346 VideoNode::HandleBuffer(BBuffer *buffer)
348 // printf("VideoNode::HandleBuffer\n");
350 LockBitmap();
351 if (fBitmap) {
352 // bigtime_t start = system_time();
353 if (fOverlayActive) {
354 if (B_OK == fBitmap->LockBits()) {
356 // memcpy(fBitmap->Bits(), buffer->Data(), fBitmap->BitsLength());
358 // fBitmap->SetBits(buffer->Data(), fBitmap->BitsLength(), 0,
359 // fInput.format.u.raw_video.display.format);
361 overlay_copy(fBitmap->Bounds().IntegerHeight() + 1,
362 fBitmap->Bits(), fBitmap->BytesPerRow(),
363 buffer->Data(),
364 fInput.format.u.raw_video.display.bytes_per_row);
367 fBitmap->UnlockBits();
369 } else {
370 // memcpy(fBitmap->Bits(), buffer->Data(), fBitmap->BitsLength());
372 overlay_copy(fBitmap->Bounds().IntegerHeight() + 1,
373 fBitmap->Bits(), fBitmap->BytesPerRow(),
374 buffer->Data(),
375 fInput.format.u.raw_video.display.bytes_per_row);
377 // printf("overlay copy: %Ld usec\n", system_time() - start);
379 UnlockBitmap();
381 buffer->Recycle();
383 fVideoView->DrawFrame();
387 void
388 VideoNode::SetOverlayEnabled(bool yesno)
390 fOverlayEnabled = yesno;
394 void
395 VideoNode::LockBitmap()
397 fBitmapLocker->Lock();
401 BBitmap *
402 VideoNode::Bitmap()
404 return fBitmap;
408 void
409 VideoNode::UnlockBitmap()
411 fBitmapLocker->Unlock();
415 bool
416 VideoNode::IsOverlayActive()
418 return fOverlayActive;
422 status_t
423 VideoNode::CreateBuffers(BRect frame, color_space cspace, bool overlay)
425 printf("VideoNode::CreateBuffers: frame %d,%d,%d,%d colorspace 0x%08x, "
426 "overlay %d\n", int(frame.left), int(frame.top), int(frame.right),
427 int(frame.bottom), int(cspace), overlay);
429 // int32 bytesPerRow = B_ANY_BYTES_PER_ROW;
430 // if (cspace == B_YCbCr422)
431 // bytesPerRow = (int(frame.Width()) + 1) * 2;
433 // printf("overlay bitmap: requesting: bytes per row: %d\n", bytesPerRow);
435 LockBitmap();
436 ASSERT(fBitmap == 0);
437 uint32 flags = overlay ? (B_BITMAP_WILL_OVERLAY
438 | B_BITMAP_RESERVE_OVERLAY_CHANNEL) : 0;
439 // fBitmap = new BBitmap(frame, flags, cspace, bytesPerRow);
440 fBitmap = new BBitmap(frame, flags, cspace);
441 if (!(fBitmap && fBitmap->InitCheck() == B_OK && fBitmap->IsValid())) {
442 delete fBitmap;
443 fBitmap = 0;
444 fOverlayActive = false;
445 UnlockBitmap();
446 printf("VideoNode::CreateBuffers failed\n");
447 return B_ERROR;
449 printf("overlay bitmap: got: bytes per row: %" B_PRId32 "\n",
450 fBitmap->BytesPerRow());
451 fOverlayActive = overlay;
452 UnlockBitmap();
453 printf("VideoNode::CreateBuffers success\n");
454 return B_OK;
458 void
459 VideoNode::DeleteBuffers()
461 LockBitmap();
462 delete fBitmap;
463 fBitmap = NULL;
464 UnlockBitmap();