2 * Copyright 2009-2010, Stephan Aßmus <superstippi@gmx.de>.
3 * All rights reserved. Distributed under the terms of the MIT license.
7 #include "MediaWriter.h"
18 #include "PluginManager.h"
22 class MediaExtractorChunkWriter
: public ChunkWriter
{
24 MediaExtractorChunkWriter(MediaWriter
* writer
, int32 streamIndex
)
27 fStreamIndex(streamIndex
)
31 virtual status_t
WriteChunk(const void* chunkBuffer
, size_t chunkSize
,
32 media_encode_info
* encodeInfo
)
34 return fWriter
->WriteChunk(fStreamIndex
, chunkBuffer
, chunkSize
,
47 MediaWriter::MediaWriter(BDataIO
* target
, const media_file_format
& fileFormat
)
52 fFileFormat(fileFormat
)
56 gPluginManager
.CreateWriter(&fWriter
, fFileFormat
, fTarget
);
60 MediaWriter::~MediaWriter()
64 if (fWriter
!= NULL
) {
65 // free all stream cookies
68 for (fStreamInfos
.Rewind(); fStreamInfos
.GetNext(&info
);)
69 fWriter
->FreeCookie(info
->cookie
);
71 gPluginManager
.DestroyWriter(fWriter
);
74 // fTarget is owned by the BMediaFile
79 MediaWriter::InitCheck()
83 return fWriter
!= NULL
? fWriter
->Init(&fFileFormat
) : B_NO_INIT
;
88 MediaWriter::Target() const
95 MediaWriter::GetFileFormatInfo(media_file_format
* _fileFormat
) const
99 if (_fileFormat
!= NULL
)
100 *_fileFormat
= fFileFormat
;
105 MediaWriter::CreateEncoder(Encoder
** _encoder
,
106 const media_codec_info
* codecInfo
, media_format
* format
, uint32 flags
)
113 // TODO: Here we should work out a way so that if there is a setup
114 // failure we can try the next encoder.
116 status_t ret
= gPluginManager
.CreateEncoder(&encoder
, codecInfo
, flags
);
118 ERROR("MediaWriter::CreateEncoder gPluginManager.CreateEncoder "
119 "failed, codec: %s\n", codecInfo
->pretty_name
);
124 ret
= fWriter
->AllocateCookie(&info
.cookie
, format
, codecInfo
);
126 gPluginManager
.DestroyEncoder(encoder
);
130 int32 streamIndex
= fStreamInfos
.CountItems();
132 if (!fStreamInfos
.Insert(info
)) {
133 gPluginManager
.DestroyEncoder(encoder
);
134 ERROR("MediaWriter::CreateEncoder can't create StreamInfo "
135 "for stream %" B_PRId32
"\n", streamIndex
);
139 ChunkWriter
* chunkWriter
= new(std::nothrow
) MediaExtractorChunkWriter(
141 if (chunkWriter
== NULL
) {
142 gPluginManager
.DestroyEncoder(encoder
);
143 ERROR("MediaWriter::CreateEncoder can't create ChunkWriter "
144 "for stream %" B_PRId32
"\n", streamIndex
);
148 encoder
->SetChunkWriter(chunkWriter
);
156 MediaWriter::SetCopyright(const char* copyright
)
161 return fWriter
->SetCopyright(copyright
);
166 MediaWriter::SetCopyright(int32 streamIndex
, const char* copyright
)
172 if (!fStreamInfos
.Get(streamIndex
, &info
))
175 return fWriter
->SetCopyright(info
->cookie
, copyright
);
180 MediaWriter::CommitHeader()
185 return fWriter
->CommitHeader();
195 return fWriter
->Flush();
205 return fWriter
->Close();
210 MediaWriter::AddTrackInfo(int32 streamIndex
, uint32 code
,
211 const void* data
, size_t size
, uint32 flags
)
217 if (!fStreamInfos
.Get(streamIndex
, &info
))
220 return fWriter
->AddTrackInfo(info
->cookie
, code
, data
, size
, flags
);
225 MediaWriter::WriteChunk(int32 streamIndex
, const void* chunkBuffer
,
226 size_t chunkSize
, media_encode_info
* encodeInfo
)
232 if (!fStreamInfos
.Get(streamIndex
, &info
))
235 return fWriter
->WriteChunk(info
->cookie
, chunkBuffer
, chunkSize
,