Ticket #18160: main.cpp

File main.cpp, 2.4 KB (added by jackburton, 17 months ago)

Test case. Compile with gcc -lbe -lmedia -o mediacrashtest main.cpp

Line 
1#include <Application.h>
2#include <Bitmap.h>
3#include <MediaDefs.h>
4#include <MediaFile.h>
5#include <MediaTrack.h>
6
7#include <iostream>
8
9int main()
10{
11 BApplication app("application/media-test");
12 int32 width = 640;
13 int32 height = 480;
14
15 media_format mediaFormat;
16 mediaFormat.type = B_MEDIA_RAW_VIDEO;
17 mediaFormat.u.raw_video.display.line_width = width;
18 mediaFormat.u.raw_video.display.line_count = height;
19 mediaFormat.u.raw_video.last_active = mediaFormat.u.raw_video.display.line_count - 1;
20
21 size_t pixelChunk;
22 size_t rowAlign;
23 size_t pixelPerChunk;
24 status_t status = get_pixel_size_for(B_RGB32, &pixelChunk,
25 &rowAlign, &pixelPerChunk);
26 if (status != B_OK)
27 return int(status);
28
29 mediaFormat.u.raw_video.display.bytes_per_row = width * rowAlign;
30 mediaFormat.u.raw_video.display.format = B_RGB32;
31 mediaFormat.u.raw_video.interlace = 1;
32 mediaFormat.u.raw_video.field_rate = 30;
33 mediaFormat.u.raw_video.pixel_width_aspect = 1;
34 mediaFormat.u.raw_video.pixel_height_aspect = 1;
35
36 media_file_format mediaFileFormat;
37 int32 cookie = 0;
38 while (get_next_file_format(&cookie, &mediaFileFormat) == B_OK) {
39 if (strcmp(mediaFileFormat.pretty_name, "MPEG (Motion Picture Experts Group) format 4") == 0)
40 break;
41 }
42
43 //std::cout << mediaFileFormat.short_name << std::endl;
44 media_codec_info codec;
45 media_format dummyFormat;
46 cookie = 0;
47 while (get_next_encoder(&cookie, &mediaFileFormat, &mediaFormat,
48 &dummyFormat, &codec) == B_OK) {
49 if (strcmp(codec.pretty_name, "MPEG-4 video") == 0)
50 break;
51 }
52
53 entry_ref ref;
54 const char* path = "/boot/home/test-crash.mpg";
55 status = get_ref_for_path(path, &ref);
56 if (status != B_OK)
57 return int(status);
58
59 for (int i = 0; i < 10000; i++) {
60 BMediaFile* file = new (std::nothrow) BMediaFile(&ref, &mediaFileFormat);
61 if (file == NULL)
62 return int(B_NO_MEMORY);
63
64 status = file->InitCheck();
65 if (status != B_OK)
66 return int(status);
67
68 BMediaTrack* mediaTrack = file->CreateTrack(const_cast<media_format*>(&mediaFormat), &codec);
69 if (mediaTrack == NULL)
70 return int(B_ERROR);
71
72 file->CommitHeader();
73
74 BRect rect(0, 0, width - 1, height - 1);
75 BBitmap* bitmap = new BBitmap(rect, B_RGB32);
76 status = mediaTrack->WriteFrames(bitmap->Bits(), 1, B_MEDIA_KEY_FRAME);
77 if (status != B_OK)
78 return int(status);
79
80 delete bitmap;
81
82 file->ReleaseAllTracks();
83 status = file->CloseFile();
84
85 delete file; // deletes the track, too
86 }
87 return 0;
88}