/
Location.cs
194 lines (163 loc) · 7.15 KB
/
Location.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Rtc.Signaling;
using Microsoft.Rtc.Collaboration;
using Microsoft.Rtc.Collaboration.ConferenceManagement;
using Microsoft.Rtc.Collaboration.Conferencing;
using Microsoft.Rtc.Collaboration.AudioVideo;
namespace Locations
{
public class Location
{
int _id;
string _Name;
string _FileName;
LocalEndpoint _Endpoint;
Conference _conference;
Conversation _conversation;
AudioVideoCall _avCall;
public string Name { get { return _Name; } }
public Conference Conference { get { return _conference; } }
public Conversation Conversation { get { return _conversation; } }
public int Id { get { return _id; } }
void Log(string data)
{
Console.WriteLine("Location \"{0}\" {1}", _Name, data);
}
public Location(int id, string name, string fileName, LocalEndpoint endpoint)
{
if (!File.Exists(fileName))
throw new FileNotFoundException(fileName);
_id = id;
_Name = name;
_FileName = fileName;
_Endpoint = endpoint;
ConferenceScheduleInformation csi = new ConferenceScheduleInformation()
{
AccessLevel = ConferenceAccessLevel.Everyone,
Description = _Name,
ExpiryTime = DateTime.Now.AddYears(5),
AutomaticLeaderAssignment = AutomaticLeaderAssignment.Everyone
};
csi.Mcus.Add(new ConferenceMcuInformation(McuType.AudioVideo));
_Endpoint.ConferenceServices.BeginScheduleConference(csi,
ar =>
{
try
{
_conference = _Endpoint.ConferenceServices.EndScheduleConference(ar);
Log("Conference " + _conference.ConferenceId + " scheduled. Starting music...");
Log(_conference.ConferenceUri);
ConversationSettings cs = new ConversationSettings()
{
Subject = _Name
};
_conversation = new Conversation(_Endpoint, cs);
ConferenceJoinOptions cjo = new ConferenceJoinOptions()
{
JoinMode = JoinMode.TrustedParticipant
};
_conversation.ConferenceSession.BeginJoin(_conference.ConferenceUri,
cjo,
ar1 =>
{
try
{
_conversation.ConferenceSession.EndJoin(ar1);
_avCall = new AudioVideoCall(_conversation);
_avCall.AudioVideoFlowConfigurationRequested +=
new EventHandler<AudioVideoFlowConfigurationRequestedEventArgs>(_avCall_AudioVideoFlowConfigurationRequested);
AudioVideoCallEstablishOptions options = new AudioVideoCallEstablishOptions()
{
UseGeneratedIdentityForTrustedConference = true,
SupportsReplaces = CapabilitySupport.Supported
};
_avCall.BeginEstablish(
options,
ar2 =>
{
try
{
_avCall.EndEstablish(ar2);
}
catch (Exception ex)
{
Log(ex.ToString());
}
},
null);
}
catch (Exception ex)
{
Log(ex.ToString());
}
},
null);
}
catch (Exception ex)
{
Log(ex.ToString());
}
},
null);
}
void _avCall_AudioVideoFlowConfigurationRequested(object sender, AudioVideoFlowConfigurationRequestedEventArgs e)
{
Log("AudioVideoFlowConfigurationRequested");
e.Flow.StateChanged += new EventHandler<MediaFlowStateChangedEventArgs>(Flow_StateChanged);
}
void Flow_StateChanged(object sender, MediaFlowStateChangedEventArgs e)
{
Log("Flow_StateChanged PreviousState=" + e.PreviousState + " State=" + e.State);
AudioVideoFlow avFlow = (AudioVideoFlow)sender;
if (avFlow.State == MediaFlowState.Active)
{
Player player = new Player();
player.StateChanged += new EventHandler<PlayerStateChangedEventArgs>(player_StateChanged);
player.AttachFlow(avFlow);
WmaFileSource src = new WmaFileSource(_FileName);
src.BeginPrepareSource(MediaSourceOpenMode.Buffered,
ar=>
{
try
{
src.EndPrepareSource(ar);
player.SetSource(src);
// For some reason, PlayerMode.Automatic does not loop audio
player.SetMode(PlayerMode.Manual);
player.Start();
Log("Playing \"" + _FileName + "\"");
}
catch (Exception ex)
{
Log(ex.ToString());
}
},
null);
}
else if (avFlow.State == MediaFlowState.Terminated)
{
if (avFlow.Player != null)
{
if (avFlow.Player.Source != null)
{
avFlow.Player.Source.Close();
}
avFlow.Player.DetachFlow(avFlow);
}
}
}
void player_StateChanged(object sender, PlayerStateChangedEventArgs e)
{
Log("Player StateChanged PreviousState=" + e.PreviousState + " State=" + e.State + " TransitionReason=" + e.TransitionReason);
if (e.PreviousState == PlayerState.Started && e.State == PlayerState.Stopped)
{
Player player = (Player)sender;
player.Start();
}
}
}
}