2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 namespace Tizen.Multimedia
23 /// Represents a media source using memory.
26 /// The buffer has to be filled with appropriate data which means it must be well-formatted.
27 /// If you provide invalid data, you won't receive an error until <see cref="Player.Start"/> is called.
29 /// <seealso cref="Player.SetSource(MediaSource)"/>
30 public sealed class MediaBufferSource : MediaSource
32 private byte[] _buffer;
35 /// Initializes a new instance of the MediaBufferSource class with an allocated buffer.
37 /// <param name="length">The value indicating the size of the buffer.</param>
38 /// <exception cref="ArgumentOutOfRangeException">
39 /// <paramref name="length"/> is zero.\n
41 /// <paramref name="length"/> is less than zero.
43 public MediaBufferSource(int length)
47 Log.Error(PlayerLog.Tag, "invalid length : " + length);
48 throw new ArgumentOutOfRangeException(nameof(length), length,
49 "length can't be equal to or less than zero.");
51 _buffer = new byte[length];
55 /// Initializes a new instance of the MediaBufferSource class from the buffer.
57 /// <param name="buffer">The source array to be copied into the buffer.</param>
58 /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is null.</exception>
59 public MediaBufferSource(byte[] buffer) : this(buffer, buffer == null ? 0 : buffer.Length)
63 //TODO remove default parameter.
65 /// Initializes a new instance of the MediaBufferSource class from the buffer
66 /// with the specified length and the specified offset.
68 /// <param name="buffer">The source array to be copied into the buffer.</param>
69 /// <param name="length">The value indicating the number of bytes to copy from the buffer.</param>
70 /// <param name="offset">The value indicating the offset in the buffer of the first byte to copy.</param>
71 /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is null.</exception>
72 /// <exception cref="ArgumentOutOfRangeException">
73 /// <paramref name="offset"/> is less than zero.\n
75 /// <paramref name="length"/> is equal to or less than zero.\n
77 /// <paramref name="offset"/>+<paramref name="length"/> is greater than buffer.Length.
79 public MediaBufferSource(byte[] buffer, int length, int offset = 0)
83 Log.Error(PlayerLog.Tag, "invalid buffer");
84 throw new ArgumentNullException(nameof(buffer));
88 Log.Error(PlayerLog.Tag, "invalid offset : " + offset);
89 throw new ArgumentOutOfRangeException(nameof(offset), offset, "offset can't be less than zero.");
93 Log.Error(PlayerLog.Tag, "invalid length : " + length);
94 throw new ArgumentOutOfRangeException(nameof(length), length, "length can't be equal to or less than zero.");
96 if (length + offset > buffer.Length)
98 Log.Error(PlayerLog.Tag, "invalid total length : " + (int)(length + offset));
99 throw new ArgumentOutOfRangeException($"length + offset can't be greater than the length of the { nameof(buffer) }.");
102 _buffer = new byte[length];
104 Array.Copy(buffer, offset, _buffer, 0, length);
107 private MediaBufferSource()
112 /// Creates a MediaBufferSource that wraps a byte array.
114 /// <param name="buffer">The array to be wrapped.</param>
115 /// <returns>A MediaBufferSource wrapping the byte array.</returns>
116 public static MediaBufferSource Wrap(byte[] buffer)
120 Log.Error(PlayerLog.Tag, "invalid buffer");
121 throw new ArgumentNullException(nameof(buffer));
124 MediaBufferSource source = new MediaBufferSource();
125 source._buffer = buffer;
130 /// Gets the byte array of this buffer.
132 public byte[] Buffer => _buffer;
134 internal override void OnAttached(Player player)
136 NativePlayer.SetMemoryBuffer(player.Handle, _buffer, _buffer.Length).
137 ThrowIfFailed("Failed to set the memory buffer");