1 /* 2 * Copyright 2017, Adrien Destugues, pulkomandy@pulkomandy.tk 3 * Distributed under terms of the MIT license. 4 */ 5 6 7 #include <Application.h> 8 #include <Bitmap.h> 9 #include <File.h> 10 #include <MediaFile.h> 11 #include <MediaTrack.h> 12 #include <Window.h> 13 14 #include <stdio.h> 15 16 17 #pragma mark - VideoView 18 19 20 class VideoView: public BView 21 { 22 public: 23 VideoView(BMediaTrack* track, int32 width, int32 height); 24 void Draw(BRect); 25 void KeyDown(const char*, int32); 26 27 private: 28 BMediaTrack* fMediaTrack; 29 BBitmap fBitmap; 30 }; 31 32 33 VideoView::VideoView(BMediaTrack* track, int32 width, int32 height) 34 : BView(BRect(0, 0, width, height), "Video", B_FOLLOW_NONE, 35 B_WILL_DRAW) 36 , fMediaTrack(track) 37 , fBitmap(BRect(0, 0, width - 1, height - 1), B_RGB32) 38 { 39 } 40 41 42 void 43 VideoView::Draw(BRect r) 44 { 45 DrawBitmap(&fBitmap); 46 } 47 48 49 void 50 VideoView::KeyDown(const char*, int32) 51 { 52 puts("Next frame"); 53 int64 count = 1; 54 fMediaTrack->ReadFrames(fBitmap.Bits(), &count); 55 Invalidate(); 56 } 57 58 59 #pragma mark - VideoWindow 60 61 62 class VideoWindow: public BWindow 63 { 64 public: 65 VideoWindow(const char* videoFile); 66 ~VideoWindow(); 67 68 private: 69 BFile* fFile; 70 BMediaFile* fMediaFile; 71 BMediaTrack* fMediaTrack; 72 }; 73 74 75 VideoWindow::VideoWindow(const char* path) 76 : BWindow(BRect(60, 120, 700, 600), "Video Decoder", 77 B_DOCUMENT_WINDOW, B_QUIT_ON_WINDOW_CLOSE) 78 , fMediaTrack(NULL) 79 { 80 fFile = new BFile(path, B_READ_ONLY); 81 fMediaFile = new BMediaFile(fFile); 82 media_format format; 83 84 int i; 85 for (i = fMediaFile->CountTracks(); --i >= 0;) { 86 BMediaTrack* mediaTrack = fMediaFile->TrackAt(i); 87 88 mediaTrack->EncodedFormat(&format); 89 if (format.IsVideo()) { 90 fMediaTrack = mediaTrack; 91 fMediaTrack->DecodedFormat(&format); 92 break; 93 } 94 } 95 96 if (fMediaTrack) { 97 printf("Found video track\n%ld x %ld ; %lld frames, %f seconds\n", 98 format.Width(), format.Height(), fMediaTrack->CountFrames(), 99 fMediaTrack->Duration() / 1000000.f); 100 } else { 101 return; 102 } 103 104 BView* view = new VideoView(fMediaTrack, format.Width(), format.Height()); 105 AddChild(view); 106 view->MakeFocus(); 107 } 108 109 110 VideoWindow::~VideoWindow() 111 { 112 delete fMediaFile; // Also deletes the track 113 delete fFile; 114 } 115 116 117 #pragma mark - Main 118 119 120 int main(int argc, char* argv[]) 121 { 122 if (argc < 2) 123 return -1; 124 125 BApplication app("application/x-vnd.Haiku-VideoDecoder"); 126 127 BWindow* window = new VideoWindow(argv[1]); 128 window->Show(); 129 130 app.Run(); 131 } 132