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 /// <since_tizen> 3 </since_tizen>
31 public sealed class MediaBufferSource : MediaSource
33 private byte[] _buffer;
36 /// Initializes a new instance of the MediaBufferSource class with an allocated buffer.
38 /// <param name="length">The value indicating the size of the buffer.</param>
39 /// <exception cref="ArgumentOutOfRangeException">
40 /// <paramref name="length"/> is zero.<br/>
42 /// <paramref name="length"/> is less than zero.
44 /// <since_tizen> 3 </since_tizen>
45 public MediaBufferSource(int length)
49 Log.Error(PlayerLog.Tag, "invalid length : " + length);
50 throw new ArgumentOutOfRangeException(nameof(length), length,
51 "length can't be equal to or less than zero.");
53 _buffer = new byte[length];
57 /// Initializes a new instance of the MediaBufferSource class from the buffer.
59 /// <param name="buffer">The source array to be copied into the buffer.</param>
60 /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is null.</exception>
61 /// <since_tizen> 3 </since_tizen>
62 public MediaBufferSource(byte[] buffer) : this(buffer, buffer == null ? 0 : buffer.Length)
66 //TODO remove default parameter.
68 /// Initializes a new instance of the MediaBufferSource class from the buffer
69 /// with the specified length and the specified offset.
71 /// <param name="buffer">The source array to be copied into the buffer.</param>
72 /// <param name="length">The value indicating the number of bytes to copy from the buffer.</param>
73 /// <param name="offset">The value indicating the offset in the buffer of the first byte to copy.</param>
74 /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is null.</exception>
75 /// <exception cref="ArgumentOutOfRangeException">
76 /// <paramref name="offset"/> is less than zero.<br/>
78 /// <paramref name="length"/> is equal to or less than zero.<br/>
80 /// <paramref name="offset"/>+<paramref name="length"/> is greater than buffer.Length.
82 /// <since_tizen> 3 </since_tizen>
83 public MediaBufferSource(byte[] buffer, int length, int offset = 0)
87 throw new ArgumentNullException(nameof(buffer));
92 throw new ArgumentOutOfRangeException(nameof(offset), offset, "offset can't be less than zero.");
96 throw new ArgumentOutOfRangeException(nameof(length), length, "length can't be equal to or less than zero.");
98 if (length + offset > buffer.Length)
100 throw new ArgumentOutOfRangeException($"length + offset can't be greater than the length of the { nameof(buffer) }.");
103 _buffer = new byte[length];
105 Array.Copy(buffer, offset, _buffer, 0, length);
108 private MediaBufferSource()
113 /// Creates a MediaBufferSource that wraps a byte array.
115 /// <param name="buffer">The array to be wrapped.</param>
116 /// <returns>A MediaBufferSource wrapping the byte array.</returns>
117 /// <since_tizen> 3 </since_tizen>
118 public static MediaBufferSource Wrap(byte[] buffer)
122 Log.Error(PlayerLog.Tag, "invalid buffer");
123 throw new ArgumentNullException(nameof(buffer));
126 MediaBufferSource source = new MediaBufferSource();
127 source._buffer = buffer;
132 /// Gets the byte array of this buffer.
134 /// <since_tizen> 3 </since_tizen>
135 public byte[] Buffer => _buffer;
137 internal override void OnAttached(Player player)
139 NativePlayer.SetMemoryBuffer(player.Handle, _buffer, _buffer.Length).
140 ThrowIfFailed(player, "Failed to set the memory buffer");