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::GetFileFormatInfo(media_file_format
* _fileFormat
) const
92 if (_fileFormat
!= NULL
)
93 *_fileFormat
= fFileFormat
;
98 MediaWriter::CreateEncoder(Encoder
** _encoder
,
99 const media_codec_info
* codecInfo
, media_format
* format
, uint32 flags
)
106 // TODO: Here we should work out a way so that if there is a setup
107 // failure we can try the next encoder.
109 status_t ret
= gPluginManager
.CreateEncoder(&encoder
, codecInfo
, flags
);
111 ERROR("MediaWriter::CreateEncoder gPluginManager.CreateEncoder "
112 "failed, codec: %s\n", codecInfo
->pretty_name
);
117 ret
= fWriter
->AllocateCookie(&info
.cookie
, format
, codecInfo
);
119 gPluginManager
.DestroyEncoder(encoder
);
123 int32 streamIndex
= fStreamInfos
.CountItems();
125 if (!fStreamInfos
.Insert(info
)) {
126 gPluginManager
.DestroyEncoder(encoder
);
127 ERROR("MediaWriter::CreateEncoder can't create StreamInfo "
128 "for stream %" B_PRId32
"\n", streamIndex
);
132 ChunkWriter
* chunkWriter
= new(std::nothrow
) MediaExtractorChunkWriter(
134 if (chunkWriter
== NULL
) {
135 gPluginManager
.DestroyEncoder(encoder
);
136 ERROR("MediaWriter::CreateEncoder can't create ChunkWriter "
137 "for stream %" B_PRId32
"\n", streamIndex
);
141 encoder
->SetChunkWriter(chunkWriter
);
149 MediaWriter::SetCopyright(const char* copyright
)
154 return fWriter
->SetCopyright(copyright
);
159 MediaWriter::SetCopyright(int32 streamIndex
, const char* copyright
)
165 if (!fStreamInfos
.Get(streamIndex
, &info
))
168 return fWriter
->SetCopyright(info
->cookie
, copyright
);
173 MediaWriter::CommitHeader()
178 return fWriter
->CommitHeader();
188 return fWriter
->Flush();
198 return fWriter
->Close();
203 MediaWriter::AddTrackInfo(int32 streamIndex
, uint32 code
,
204 const void* data
, size_t size
, uint32 flags
)
210 if (!fStreamInfos
.Get(streamIndex
, &info
))
213 return fWriter
->AddTrackInfo(info
->cookie
, code
, data
, size
, flags
);
218 MediaWriter::WriteChunk(int32 streamIndex
, const void* chunkBuffer
,
219 size_t chunkSize
, media_encode_info
* encodeInfo
)
225 if (!fStreamInfos
.Get(streamIndex
, &info
))
228 return fWriter
->WriteChunk(info
->cookie
, chunkBuffer
, chunkSize
,