Release 4.0.0-preview1-00183
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia.StreamRecorder / StreamRecorder / StreamRecorder.Events.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 using System;
18 using System.Diagnostics;
19 using Native = Interop.StreamRecorder;
20
21 namespace Tizen.Multimedia
22 {
23     public partial class StreamRecorder
24     {
25         /// <summary>
26         /// Occurs when <see cref="StreamRecorder"/> state is changed.
27         /// </summary>
28         public event EventHandler<StreamRecorderStateChangedEventArgs> StateChanged;
29
30
31         /// <summary>
32         /// Occurs when a buffer had consumed completely.
33         /// </summary>
34         public event EventHandler<StreamRecorderBufferConsumedEventArgs> BufferConsumed;
35
36         /// <summary>
37         /// Occurs when recording status is changed.
38         /// </summary>
39         public event EventHandler<RecordingStatusChangedEventArgs> RecordingStatusChanged;
40
41         /// <summary>
42         /// Occurs when recording limit is reached.
43         /// </summary>
44         public event EventHandler<RecordingLimitReachedEventArgs> RecordingLimitReached;
45
46         /// <summary>
47         /// Occurs when an error occurred during a recorder operation.
48         /// </summary>
49         public event EventHandler<StreamRecorderErrorOccurredEventArgs> ErrorOccurred;
50
51         private Native.RecordingLimitReachedCallback _recordingLimitReachedCallback;
52         private Native.RecorderErrorCallback _recorderErrorCallback;
53         private Native.RecordingStatusCallback _recordingStatusCallback;
54         private Native.BufferConsumedCallback _bufferConsumedCallback;
55         private Native.NotifiedCallback _notifiedCallback;
56
57         private void RegisterStreamRecorderNotifiedEvent()
58         {
59             _notifiedCallback = (previous, current, notify, _) =>
60             {
61                 if (previous == 0)
62                 {
63                     return;
64                 }
65
66                 StateChanged?.Invoke(this, new StreamRecorderStateChangedEventArgs(
67                     (RecorderState)previous, (RecorderState)current));
68             };
69
70             Native.SetNotifiedCallback(_handle, _notifiedCallback).
71                 ThrowIfError("Failed to initialize state changed event.");
72         }
73
74         private void RegisterBufferConsumedEvent()
75         {
76             _bufferConsumedCallback = (lockedPacketHandle, _) =>
77             {
78                 MediaPacket packet = null;
79
80                 // Lock must be disposed here, note that the packet won't be disposed.
81                 using (MediaPacket.Lock packetLock =
82                     MediaPacket.Lock.FromHandle(lockedPacketHandle))
83                 {
84                     Debug.Assert(packetLock != null);
85
86                     packet = packetLock.MediaPacket;
87                 }
88
89                 BufferConsumed?.Invoke(this, new StreamRecorderBufferConsumedEventArgs(packet));
90             };
91
92             Native.SetBufferConsumedCallback(_handle, _bufferConsumedCallback).
93                 ThrowIfError("Failed to initialize buffer consumed event.");
94         }
95
96         private void RegisterRecordingStatusChangedEvent()
97         {
98             _recordingStatusCallback = (elapsedTime, fileSize, _) =>
99             {
100                 RecordingStatusChanged?.Invoke(this, new RecordingStatusChangedEventArgs((long)elapsedTime, (long)fileSize));
101             };
102             Native.SetStatusChangedCallback(_handle, _recordingStatusCallback).
103                 ThrowIfError("Failed to initialize status changed event.");
104         }
105
106         private void RegisterRecordingLimitReachedEvent()
107         {
108             _recordingLimitReachedCallback = (type, _) =>
109             {
110                 RecordingLimitReached?.Invoke(this, new RecordingLimitReachedEventArgs(type));
111             };
112
113             Native.SetLimitReachedCallback(_handle, _recordingLimitReachedCallback).
114                 ThrowIfError("Failed to initialize limit reached event.");
115         }
116
117         private void RegisterRecordingErrorOccurredEvent()
118         {
119             _recorderErrorCallback = (error, currentState, _) =>
120             {
121                 ErrorOccurred?.Invoke(this, new StreamRecorderErrorOccurredEventArgs(
122                     error == StreamRecorderErrorCode.OutOfStorage ?
123                     StreamRecorderError.OutOfStorage : StreamRecorderError.InternalError, currentState));
124             };
125             Native.SetErrorCallback(_handle, _recorderErrorCallback).
126                 ThrowIfError("Failed to set error callback");
127         }
128     }
129 }